Skip to main content

kovi_onebot/
event.rs

1use kovi::bot::SendApi;
2use serde::{Deserialize, Serialize};
3
4pub use admin_msg_event::AdminMsgEvent;
5pub use group_msg_event::GroupMsgEvent;
6use kovi::message::Message as KoviMessage;
7pub use msg_event::MsgEvent;
8pub use msg_send_from_kovi_event::{MsgSendFromKoviEvent, MsgSendFromKoviType};
9pub use msg_send_from_server_event::MsgSendFromServerEvent;
10pub use notice_event::NoticeEvent;
11pub use private_msg_event::PrivateMsgEvent;
12pub use request_event::RequestEvent;
13
14#[cfg(not(feature = "cqstring"))]
15use crate::onebot_message::OneBotMessage;
16
17pub mod admin_msg_event;
18pub mod group_msg_event;
19pub mod lifecycle_event;
20pub mod msg_event;
21pub mod msg_send_from_kovi_event;
22pub mod msg_send_from_server_event;
23pub mod notice_event;
24pub mod private_msg_event;
25pub mod request_event;
26
27#[derive(Debug, Copy, Clone)]
28pub enum Sex {
29    Male,
30    Female,
31}
32
33#[derive(Debug, Clone)]
34pub struct Sender {
35    pub user_id: i64,
36    pub nickname: Option<String>,
37    pub card: Option<String>,
38    pub sex: Option<Sex>,
39    pub age: Option<i32>,
40    pub area: Option<String>,
41    pub level: Option<String>,
42    pub role: Option<String>,
43    pub title: Option<String>,
44}
45#[derive(Debug, Deserialize, Serialize, Clone)]
46pub struct Anonymous {
47    pub id: i64,
48    pub name: String,
49    pub flag: String,
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub enum PostType {
54    Message,
55    Notice,
56    Request,
57    MetaEvent,
58    MessageSent,
59
60    Other(String),
61}
62
63impl<'de> Deserialize<'de> for PostType {
64    fn deserialize<D>(deserializer: D) -> Result<PostType, D::Error>
65    where
66        D: serde::Deserializer<'de>,
67    {
68        let s = String::deserialize(deserializer)?;
69        let post_type = match s.as_str() {
70            "message" => PostType::Message,
71            "notice" => PostType::Notice,
72            "request" => PostType::Request,
73            "meta_event" => PostType::MetaEvent,
74            "message_sent" => PostType::MessageSent,
75            _ => PostType::Other(s),
76        };
77        Ok(post_type)
78    }
79}
80
81/// 满足此 trait 即可判断消息来源
82pub trait UniversalMessage {
83    fn is_group(&self) -> bool;
84
85    fn is_private(&self) -> bool;
86}
87
88/// 满足此 trait 即可被回复
89pub trait RepliableEvent {
90    fn reply_builder<M>(&self, msg: M, auto_escape: bool) -> SendApi
91    where
92        M: Into<OneBotMessage>;
93
94    #[cfg(not(feature = "cqstring"))]
95    /// 快速回复消息
96    fn reply<T>(&self, msg: T)
97    where
98        KoviMessage: From<T>,
99        T: Serialize;
100
101    /// 快速回复消息并且**引用**
102    fn reply_and_quote<T>(&self, msg: T)
103    where
104        KoviMessage: From<T>,
105        T: Serialize;
106
107    /// 便捷获取文本,如果没有文本则会返回空字符串,如果只需要借用,请使用 `borrow_text()`
108    fn get_text(&self) -> String;
109
110    /// 便捷获取发送者昵称,如果无名字,此处为空字符串
111    fn get_sender_nickname(&self) -> String;
112
113    /// 借用 event 的 text,只是做了一下self.text.as_deref()的包装
114    fn borrow_text(&self) -> Option<&str>;
115}
116
117#[test]
118fn post_type_is_ok() {
119    use serde_json::json;
120
121    assert_eq!(
122        PostType::Message,
123        serde_json::from_value::<PostType>(json!("message")).unwrap()
124    );
125    assert_eq!(
126        PostType::Notice,
127        serde_json::from_value::<PostType>(json!("notice")).unwrap()
128    );
129    assert_eq!(
130        PostType::Request,
131        serde_json::from_value::<PostType>(json!("request")).unwrap()
132    );
133    assert_eq!(
134        PostType::MetaEvent,
135        serde_json::from_value::<PostType>(json!("meta_event")).unwrap()
136    );
137}