1use std::sync::Arc;
2
3use async_trait::async_trait;
4use serde::Deserialize;
5
6use crate::base::{context::BotContext, extract::FromEvent};
7
8pub mod message;
9pub mod meta_event;
10pub mod notice;
11pub mod request;
12
13#[derive(Deserialize, Debug, Clone)]
14#[serde(tag = "post_type")]
15#[serde(rename_all = "snake_case")]
16pub enum TypedEvent {
17 Message(Box<message::Message>),
19 Notice(notice::Notice),
20 Request(request::Request),
21 MetaEvent(meta_event::MetaEvent),
22 #[serde(untagged)]
23 Unknown(serde_json::Value),
24}
25
26impl TypedEvent {
27 pub fn get_type(&self) -> &str {
28 match self {
29 TypedEvent::Message(..) => "message",
30 TypedEvent::Notice(..) => "notice",
31 TypedEvent::Request(..) => "request",
32 TypedEvent::MetaEvent(..) => "meta_event",
33 TypedEvent::Unknown(..) => "unknown",
34 }
35 }
36}
37
38#[derive(Deserialize, Debug, Clone)]
39pub struct Event {
40 pub time: i64,
41 pub self_id: i64,
42 #[serde(flatten)]
43 pub event: TypedEvent,
44}
45
46pub type BotEvent = Arc<Event>;
47
48#[async_trait]
49impl FromEvent for BotEvent {
50 async fn from_event(_: BotContext, event: BotEvent) -> Option<Self> {
51 Some(event)
52 }
53}