Skip to main content

kovi/bot/message/
add.rs

1use serde::Serialize;
2use serde_json::{json, Value};
3use std::fmt::Display;
4
5use super::{Message, Segment};
6
7#[cfg(feature = "cqstring")]
8use super::CQMessage;
9
10impl Message {
11    /// 在消息加上文字
12    pub fn add_text<T>(mut self, text: T) -> Self
13    where
14        String: From<T>,
15        T: Serialize + Display,
16    {
17        self.push(Segment {
18            type_: "text".to_string(),
19            data: json!({ "text": text }),
20        });
21        self
22    }
23
24    /// 消息加上at
25    pub fn add_at(mut self, id: &str) -> Self {
26        self.0.push(Segment {
27            type_: "at".to_string(),
28            data: json!({ "qq": id }),
29        });
30        self
31    }
32
33    /// 消息加上引用
34    pub fn add_reply(mut self, message_id: i32) -> Self {
35        self.0.insert(0, Segment {
36            type_: "reply".to_string(),
37            data: json!({ "id": message_id.to_string() }),
38        });
39        self
40    }
41
42    /// 消息加上表情, 具体 id 请看服务端文档, 本框架不提供
43    pub fn add_face(mut self, id: i64) -> Self {
44        self.0.push(Segment {
45            type_: "face".to_string(),
46            data: json!({ "id": id.to_string() }),
47        });
48        self
49    }
50
51    /// 消息加上图片
52    pub fn add_image(mut self, file: &str) -> Self {
53        self.0.push(Segment {
54            type_: "image".to_string(),
55            data: json!({ "file": file }),
56        });
57        self
58    }
59
60    /// 消息加上 segment
61    pub fn add_segment<T>(mut self, segment: T) -> Self
62    where
63        Value: From<T>,
64        T: Serialize,
65    {
66        let value = Value::from(segment);
67        if let Ok(segment) = serde_json::from_value(value) {
68            self.0.push(segment);
69        }
70        self
71    }
72}
73
74impl Message {
75    /// 在消息加上文字
76    pub fn push_text<T>(&mut self, text: T)
77    where
78        String: From<T>,
79        T: Serialize + Display,
80    {
81        self.push(Segment {
82            type_: "text".to_string(),
83            data: json!({ "text": text }),
84        });
85    }
86
87    /// 消息加上at
88    pub fn push_at(&mut self, id: &str) {
89        self.0.push(Segment {
90            type_: "at".to_string(),
91            data: json!({ "qq": id }),
92        });
93    }
94
95    /// 消息加上引用
96    pub fn push_reply(&mut self, message_id: i32) {
97        self.0.insert(0, Segment {
98            type_: "reply".to_string(),
99            data: json!({ "id": message_id.to_string() }),
100        });
101    }
102
103    /// 消息加上表情, 具体 id 请看服务端文档, 本框架不提供
104    pub fn push_face(&mut self, id: i64) {
105        self.0.push(Segment {
106            type_: "face".to_string(),
107            data: json!({ "id": id.to_string() }),
108        });
109    }
110
111    /// 消息加上图片
112    pub fn push_image(&mut self, file: &str) {
113        self.0.push(Segment {
114            type_: "image".to_string(),
115            data: json!({ "file": file }),
116        });
117    }
118
119    pub fn push(&mut self, s: Segment) {
120        self.0.push(s);
121    }
122}
123
124#[cfg(feature = "cqstring")]
125impl CQMessage {
126    /// 在消息加上文字
127    pub fn add_text<T>(mut self, text: T) -> Self
128    where
129        String: From<T>,
130        T: Serialize + Display,
131    {
132        self.0.push_str(&format!("[CQ:text,text={text}]"));
133        self
134    }
135
136    /// 消息加上at
137    pub fn add_at(mut self, id: &str) -> Self {
138        self.0.push_str(&format!("[CQ:at,qq={id}]"));
139        self
140    }
141
142    /// 消息加上引用
143    pub fn add_reply(mut self, message_id: i32) -> Self {
144        self.0
145            .insert_str(0, &format!("[CQ:reply,id={message_id}]"));
146        self
147    }
148
149    /// 消息加上表情
150    pub fn add_face(mut self, id: i64) -> Self {
151        self.0.push_str(&format!("[CQ:face,id={id}]"));
152        self
153    }
154
155    /// 消息加上图片
156    pub fn add_image(mut self, file: &str) -> Self {
157        self.0.push_str(&format!("[CQ:image,file={file}]"));
158        self
159    }
160
161    /// 消息加上 segment
162    pub fn add_segment<T>(mut self, segment: T) -> Self
163    where
164        Value: From<T>,
165        T: Serialize,
166    {
167        let value = Value::from(segment);
168        if let Ok(segment) = serde_json::from_value::<Segment>(value) {
169            self.0.push_str(&super::parse_cq_code(&segment));
170        }
171        self
172    }
173}
174
175#[cfg(feature = "cqstring")]
176impl CQMessage {
177    /// 在消息加上文字
178    pub fn push_text<T>(&mut self, text: T)
179    where
180        String: From<T>,
181        T: Serialize + Display,
182    {
183        self.0.push_str(&format!("[CQ:text,text={text}]"));
184    }
185
186    /// 消息加上at
187    pub fn push_at(&mut self, id: &str) {
188        self.0.push_str(&format!("[CQ:at,qq={id}]"));
189    }
190
191    /// 消息加上引用
192    pub fn push_reply(&mut self, message_id: i32) {
193        self.0
194            .insert_str(0, &format!("[CQ:reply,id={message_id}]"));
195    }
196
197    /// 消息加上表情
198    pub fn push_face(&mut self, id: i64) {
199        self.0.push_str(&format!("[CQ:face,id={id}]"));
200    }
201
202    /// 消息加上图片
203    pub fn push_image(&mut self, file: &str) {
204        self.0.push_str(&format!("[CQ:image,file={file}]"));
205    }
206}