Skip to main content

onebot_api/event/
message.rs

1use crate::api::APISender;
2use crate::error::{APIRequestError, APIResult};
3use crate::message::receive_segment::ReceiveSegment;
4use crate::message::send_segment::SendSegment;
5#[cfg(feature = "selector")]
6use crate::selector::Selector;
7use async_trait::async_trait;
8use serde::{Deserialize, Serialize};
9use strum::{Display, EnumIs};
10
11#[cfg(feature = "quick_operation")]
12use crate::quick_operation::QuickSendMsg;
13
14#[derive(Deserialize, Debug, Copy, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
15pub enum Sex {
16	#[serde(rename = "male")]
17	Male,
18	#[serde(rename = "female")]
19	Female,
20	#[serde(rename = "unknown")]
21	Unknown,
22}
23
24#[derive(Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
25pub struct PrivateMessageSender {
26	pub user_id: Option<i64>,
27	pub nickname: Option<String>,
28	pub sex: Option<Sex>,
29	pub age: Option<i32>,
30}
31
32#[cfg(feature = "quick_operation")]
33#[async_trait]
34impl<T: APISender + Send + Sync> QuickSendMsg<T> for PrivateMessageSender {
35	async fn send_msg(
36		&self,
37		api: &T,
38		msg: Vec<SendSegment>,
39		auto_escape: Option<bool>,
40	) -> APIResult<i32> {
41		api
42			.send_private_msg(
43				self.user_id.ok_or(APIRequestError::MissingParameters)?,
44				msg,
45				auto_escape,
46			)
47			.await
48	}
49}
50
51#[derive(Deserialize, Debug, Copy, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
52pub enum PrivateMessageSubType {
53	#[serde(rename = "friend")]
54	Friend,
55	#[serde(rename = "group")]
56	Group,
57	#[serde(rename = "other")]
58	Other,
59}
60
61#[derive(Deserialize, Serialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
62pub struct GroupMessageAnonymous {
63	id: i64,
64	name: String,
65	flag: String,
66}
67
68#[derive(Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
69pub struct GroupMessageSender {
70	pub user_id: Option<i64>,
71	pub nickname: Option<String>,
72	pub card: Option<String>,
73	pub sex: Option<Sex>,
74	pub age: Option<i32>,
75	pub area: Option<String>,
76	pub level: Option<String>,
77	pub role: Option<GroupMessageSenderRole>,
78	pub title: Option<String>,
79}
80
81#[cfg(feature = "quick_operation")]
82#[async_trait]
83impl<T: APISender + Send + Sync> QuickSendMsg<T> for GroupMessageSender {
84	async fn send_msg(
85		&self,
86		api: &T,
87		msg: Vec<SendSegment>,
88		auto_escape: Option<bool>,
89	) -> APIResult<i32> {
90		api
91			.send_private_msg(
92				self.user_id.ok_or(APIRequestError::MissingParameters)?,
93				msg,
94				auto_escape,
95			)
96			.await
97	}
98}
99
100#[derive(Deserialize, Debug, Copy, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
101pub enum GroupMessageSenderRole {
102	#[serde(rename = "owner")]
103	Owner,
104	#[serde(rename = "admin")]
105	Admin,
106	#[serde(rename = "member")]
107	Member,
108}
109
110#[derive(Deserialize, Debug, Copy, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
111pub enum GroupMessageSubType {
112	#[serde(rename = "normal")]
113	Normal,
114	#[serde(rename = "anonymous")]
115	Anonymous,
116	#[serde(rename = "notice")]
117	Notice,
118}
119
120#[derive(Deserialize, Debug, Clone)]
121pub struct MessageEventPrivate {
122	pub sub_type: PrivateMessageSubType,
123	pub message_id: i32,
124	pub user_id: i64,
125	pub message: Vec<ReceiveSegment>,
126	pub raw_message: String,
127	pub font: i32,
128	pub sender: PrivateMessageSender,
129}
130
131impl MessageEventPrivate {
132	#[cfg(feature = "selector")]
133	pub fn selector(&'_ self) -> Selector<'_, Self> {
134		Selector { data: Some(self) }
135	}
136}
137
138#[cfg(feature = "selector")]
139impl<'a> Selector<'a, MessageEventPrivate> {
140	pub fn filter(&mut self, f: impl FnOnce(&MessageEventPrivate) -> bool) {
141		if let Some(data) = self.data
142			&& !f(data)
143		{
144			self.data = None
145		}
146	}
147
148	pub fn and_filter(mut self, f: impl FnOnce(&MessageEventPrivate) -> bool) -> Self {
149		self.filter(f);
150		self
151	}
152
153	pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&MessageEventPrivate) -> bool) {
154		if let Some(data) = self.data
155			&& !f(data).await
156		{
157			self.data = None
158		}
159	}
160
161	pub async fn and_filter_async(
162		mut self,
163		f: impl AsyncFnOnce(&MessageEventPrivate) -> bool,
164	) -> Self {
165		self.filter_async(f).await;
166		self
167	}
168
169	pub fn filter_sub_type(&mut self, f: impl FnOnce(PrivateMessageSubType) -> bool) {
170		if let Some(data) = self.data
171			&& !f(data.sub_type)
172		{
173			self.data = None
174		}
175	}
176
177	pub fn and_filter_sub_type(mut self, f: impl FnOnce(PrivateMessageSubType) -> bool) -> Self {
178		self.filter_sub_type(f);
179		self
180	}
181
182	pub async fn filter_sub_type_async(
183		&mut self,
184		f: impl AsyncFnOnce(PrivateMessageSubType) -> bool,
185	) {
186		if let Some(data) = self.data
187			&& !f(data.sub_type).await
188		{
189			self.data = None
190		}
191	}
192
193	pub async fn and_filter_sub_type_async(
194		mut self,
195		f: impl AsyncFnOnce(PrivateMessageSubType) -> bool,
196	) -> Self {
197		self.filter_sub_type_async(f).await;
198		self
199	}
200
201	pub fn filter_message_id(&mut self, f: impl FnOnce(i32) -> bool) {
202		if let Some(data) = self.data
203			&& !f(data.message_id)
204		{
205			self.data = None
206		}
207	}
208
209	pub fn and_filter_message_id(mut self, f: impl FnOnce(i32) -> bool) -> Self {
210		self.filter_message_id(f);
211		self
212	}
213
214	pub async fn filter_message_id_async(&mut self, f: impl AsyncFnOnce(i32) -> bool) {
215		if let Some(data) = self.data
216			&& !f(data.message_id).await
217		{
218			self.data = None
219		}
220	}
221
222	pub async fn and_filter_message_id_async(mut self, f: impl AsyncFnOnce(i32) -> bool) -> Self {
223		self.filter_message_id_async(f).await;
224		self
225	}
226
227	pub fn filter_user_id(&mut self, f: impl FnOnce(i64) -> bool) {
228		if let Some(data) = self.data
229			&& !f(data.user_id)
230		{
231			self.data = None
232		}
233	}
234
235	pub fn and_filter_user_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
236		self.filter_user_id(f);
237		self
238	}
239
240	pub async fn filter_user_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
241		if let Some(data) = self.data
242			&& !f(data.user_id).await
243		{
244			self.data = None
245		}
246	}
247
248	pub async fn and_filter_user_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
249		self.filter_user_id_async(f).await;
250		self
251	}
252
253	pub fn filter_message(&mut self, f: impl FnOnce(&Vec<ReceiveSegment>) -> bool) {
254		if let Some(data) = self.data
255			&& !f(&data.message)
256		{
257			self.data = None
258		}
259	}
260
261	pub fn and_filter_message(mut self, f: impl FnOnce(&Vec<ReceiveSegment>) -> bool) -> Self {
262		self.filter_message(f);
263		self
264	}
265
266	pub async fn filter_message_async(&mut self, f: impl AsyncFnOnce(&Vec<ReceiveSegment>) -> bool) {
267		if let Some(data) = self.data
268			&& !f(&data.message).await
269		{
270			self.data = None
271		}
272	}
273
274	pub async fn and_filter_message_async(
275		mut self,
276		f: impl AsyncFnOnce(&Vec<ReceiveSegment>) -> bool,
277	) -> Self {
278		self.filter_message_async(f).await;
279		self
280	}
281
282	pub fn filter_raw_message(&mut self, f: impl FnOnce(&str) -> bool) {
283		if let Some(data) = self.data
284			&& !f(&data.raw_message)
285		{
286			self.data = None
287		}
288	}
289
290	pub fn and_filter_raw_message(mut self, f: impl FnOnce(&str) -> bool) -> Self {
291		self.filter_raw_message(f);
292		self
293	}
294
295	pub async fn filter_raw_message_async(&mut self, f: impl AsyncFnOnce(&str) -> bool) {
296		if let Some(data) = self.data
297			&& !f(&data.raw_message).await
298		{
299			self.data = None
300		}
301	}
302
303	pub async fn and_filter_raw_message_async(mut self, f: impl AsyncFnOnce(&str) -> bool) -> Self {
304		self.filter_raw_message_async(f).await;
305		self
306	}
307
308	pub fn filter_font(&mut self, f: impl FnOnce(i32) -> bool) {
309		if let Some(data) = self.data
310			&& !f(data.font)
311		{
312			self.data = None
313		}
314	}
315
316	pub fn and_filter_font(mut self, f: impl FnOnce(i32) -> bool) -> Self {
317		self.filter_font(f);
318		self
319	}
320
321	pub async fn filter_font_async(&mut self, f: impl AsyncFnOnce(i32) -> bool) {
322		if let Some(data) = self.data
323			&& !f(data.font).await
324		{
325			self.data = None
326		}
327	}
328
329	pub async fn and_filter_font_async(mut self, f: impl AsyncFnOnce(i32) -> bool) -> Self {
330		self.filter_font_async(f).await;
331		self
332	}
333
334	pub fn filter_sender(&mut self, f: impl FnOnce(&PrivateMessageSender) -> bool) {
335		if let Some(data) = self.data
336			&& !f(&data.sender)
337		{
338			self.data = None
339		}
340	}
341
342	pub fn and_filter_sender(mut self, f: impl FnOnce(&PrivateMessageSender) -> bool) -> Self {
343		self.filter_sender(f);
344		self
345	}
346
347	pub async fn filter_sender_async(&mut self, f: impl AsyncFnOnce(&PrivateMessageSender) -> bool) {
348		if let Some(data) = self.data
349			&& !f(&data.sender).await
350		{
351			self.data = None
352		}
353	}
354
355	pub async fn and_filter_sender_async(
356		mut self,
357		f: impl AsyncFnOnce(&PrivateMessageSender) -> bool,
358	) -> Self {
359		self.filter_sender_async(f).await;
360		self
361	}
362
363	pub fn friend(&mut self) {
364		if let Some(data) = self.data
365			&& !data.sub_type.is_friend()
366		{
367			self.data = None
368		}
369	}
370
371	pub fn and_friend(mut self) -> Self {
372		self.friend();
373		self
374	}
375
376	pub fn not_friend(&mut self) {
377		if let Some(data) = self.data
378			&& data.sub_type.is_friend()
379		{
380			self.data = None
381		}
382	}
383
384	pub fn and_not_friend(mut self) -> Self {
385		self.not_friend();
386		self
387	}
388
389	pub fn group(&mut self) {
390		if let Some(data) = self.data
391			&& !data.sub_type.is_group()
392		{
393			self.data = None
394		}
395	}
396
397	pub fn and_group(mut self) -> Self {
398		self.group();
399		self
400	}
401
402	pub fn not_group(&mut self) {
403		if let Some(data) = self.data
404			&& data.sub_type.is_group()
405		{
406			self.data = None
407		}
408	}
409
410	pub fn and_not_group(mut self) -> Self {
411		self.not_group();
412		self
413	}
414
415	pub fn other(&mut self) {
416		if let Some(data) = self.data
417			&& !data.sub_type.is_other()
418		{
419			self.data = None
420		}
421	}
422
423	pub fn and_other(mut self) -> Self {
424		self.other();
425		self
426	}
427
428	pub fn not_other(&mut self) {
429		if let Some(data) = self.data
430			&& data.sub_type.is_other()
431		{
432			self.data = None
433		}
434	}
435
436	pub fn and_not_other(mut self) -> Self {
437		self.not_other();
438		self
439	}
440}
441
442#[cfg(feature = "quick_operation")]
443#[async_trait]
444impl<T: APISender + Send + Sync> QuickSendMsg<T> for MessageEventPrivate {
445	async fn send_msg(
446		&self,
447		api: &T,
448		msg: Vec<SendSegment>,
449		auto_escape: Option<bool>,
450	) -> APIResult<i32> {
451		api.send_private_msg(self.user_id, msg, auto_escape).await
452	}
453}
454
455#[derive(Deserialize, Debug, Clone)]
456pub struct MessageEventGroup {
457	pub sub_type: GroupMessageSubType,
458	pub message_id: i32,
459	pub group_id: i64,
460	pub user_id: i64,
461	pub anonymous: Option<GroupMessageAnonymous>,
462	pub message: Vec<ReceiveSegment>,
463	pub raw_message: String,
464	pub font: i32,
465	pub sender: GroupMessageSender,
466}
467
468impl MessageEventGroup {
469	#[cfg(feature = "selector")]
470	pub fn selector(&'_ self) -> Selector<'_, Self> {
471		Selector { data: Some(self) }
472	}
473}
474
475#[cfg(feature = "selector")]
476impl<'a> Selector<'a, MessageEventGroup> {
477	pub fn filter(&mut self, f: impl FnOnce(&MessageEventGroup) -> bool) {
478		if let Some(data) = self.data
479			&& !f(data)
480		{
481			self.data = None
482		}
483	}
484
485	pub fn and_filter(mut self, f: impl FnOnce(&MessageEventGroup) -> bool) -> Self {
486		self.filter(f);
487		self
488	}
489
490	pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&MessageEventGroup) -> bool) {
491		if let Some(data) = self.data
492			&& !f(data).await
493		{
494			self.data = None
495		}
496	}
497
498	pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(&MessageEventGroup) -> bool) -> Self {
499		self.filter_async(f).await;
500		self
501	}
502
503	pub fn filter_sub_type(&mut self, f: impl FnOnce(GroupMessageSubType) -> bool) {
504		if let Some(data) = self.data
505			&& !f(data.sub_type)
506		{
507			self.data = None
508		}
509	}
510
511	pub fn and_filter_sub_type(mut self, f: impl FnOnce(GroupMessageSubType) -> bool) -> Self {
512		self.filter_sub_type(f);
513		self
514	}
515
516	pub async fn filter_sub_type_async(&mut self, f: impl AsyncFnOnce(GroupMessageSubType) -> bool) {
517		if let Some(data) = self.data
518			&& !f(data.sub_type).await
519		{
520			self.data = None
521		}
522	}
523
524	pub async fn and_filter_sub_type_async(
525		mut self,
526		f: impl AsyncFnOnce(GroupMessageSubType) -> bool,
527	) -> Self {
528		self.filter_sub_type_async(f).await;
529		self
530	}
531
532	pub fn filter_message_id(&mut self, f: impl FnOnce(i32) -> bool) {
533		if let Some(data) = self.data
534			&& !f(data.message_id)
535		{
536			self.data = None
537		}
538	}
539
540	pub fn and_filter_message_id(mut self, f: impl FnOnce(i32) -> bool) -> Self {
541		self.filter_message_id(f);
542		self
543	}
544
545	pub async fn filter_message_id_async(&mut self, f: impl AsyncFnOnce(i32) -> bool) {
546		if let Some(data) = self.data
547			&& !f(data.message_id).await
548		{
549			self.data = None
550		}
551	}
552
553	pub async fn and_filter_message_id_async(mut self, f: impl AsyncFnOnce(i32) -> bool) -> Self {
554		self.filter_message_id_async(f).await;
555		self
556	}
557
558	pub fn filter_group_id(&mut self, f: impl FnOnce(i64) -> bool) {
559		if let Some(data) = self.data
560			&& !f(data.group_id)
561		{
562			self.data = None
563		}
564	}
565
566	pub fn and_filter_group_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
567		self.filter_group_id(f);
568		self
569	}
570
571	pub async fn filter_group_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
572		if let Some(data) = self.data
573			&& !f(data.group_id).await
574		{
575			self.data = None
576		}
577	}
578
579	pub async fn and_filter_group_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
580		self.filter_group_id_async(f).await;
581		self
582	}
583
584	pub fn filter_user_id(&mut self, f: impl FnOnce(i64) -> bool) {
585		if let Some(data) = self.data
586			&& !f(data.user_id)
587		{
588			self.data = None
589		}
590	}
591
592	pub fn and_filter_user_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
593		self.filter_user_id(f);
594		self
595	}
596
597	pub async fn filter_user_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
598		if let Some(data) = self.data
599			&& !f(data.user_id).await
600		{
601			self.data = None
602		}
603	}
604
605	pub async fn and_filter_user_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
606		self.filter_user_id_async(f).await;
607		self
608	}
609
610	pub fn filter_anonymous(&mut self, f: impl FnOnce(&Option<GroupMessageAnonymous>) -> bool) {
611		if let Some(data) = self.data
612			&& !f(&data.anonymous)
613		{
614			self.data = None
615		}
616	}
617
618	pub fn and_filter_anonymous(
619		mut self,
620		f: impl FnOnce(&Option<GroupMessageAnonymous>) -> bool,
621	) -> Self {
622		self.filter_anonymous(f);
623		self
624	}
625
626	pub async fn filter_anonymous_async(
627		&mut self,
628		f: impl AsyncFnOnce(&Option<GroupMessageAnonymous>) -> bool,
629	) {
630		if let Some(data) = self.data
631			&& !f(&data.anonymous).await
632		{
633			self.data = None
634		}
635	}
636
637	pub async fn and_filter_anonymous_async(
638		mut self,
639		f: impl AsyncFnOnce(&Option<GroupMessageAnonymous>) -> bool,
640	) -> Self {
641		self.filter_anonymous_async(f).await;
642		self
643	}
644
645	pub fn filter_message(&mut self, f: impl FnOnce(&Vec<ReceiveSegment>) -> bool) {
646		if let Some(data) = self.data
647			&& !f(&data.message)
648		{
649			self.data = None
650		}
651	}
652
653	pub fn and_filter_message(mut self, f: impl FnOnce(&Vec<ReceiveSegment>) -> bool) -> Self {
654		self.filter_message(f);
655		self
656	}
657
658	pub async fn filter_message_async(&mut self, f: impl AsyncFnOnce(&Vec<ReceiveSegment>) -> bool) {
659		if let Some(data) = self.data
660			&& !f(&data.message).await
661		{
662			self.data = None
663		}
664	}
665
666	pub async fn and_filter_message_async(
667		mut self,
668		f: impl AsyncFnOnce(&Vec<ReceiveSegment>) -> bool,
669	) -> Self {
670		self.filter_message_async(f).await;
671		self
672	}
673
674	pub fn filter_raw_message(&mut self, f: impl FnOnce(&str) -> bool) {
675		if let Some(data) = self.data
676			&& !f(&data.raw_message)
677		{
678			self.data = None
679		}
680	}
681
682	pub fn and_filter_raw_message(mut self, f: impl FnOnce(&str) -> bool) -> Self {
683		self.filter_raw_message(f);
684		self
685	}
686
687	pub async fn filter_raw_message_async(&mut self, f: impl AsyncFnOnce(&str) -> bool) {
688		if let Some(data) = self.data
689			&& !f(&data.raw_message).await
690		{
691			self.data = None
692		}
693	}
694
695	pub async fn and_filter_raw_message_async(mut self, f: impl AsyncFnOnce(&str) -> bool) -> Self {
696		self.filter_raw_message_async(f).await;
697		self
698	}
699
700	pub fn filter_font(&mut self, f: impl FnOnce(i32) -> bool) {
701		if let Some(data) = self.data
702			&& !f(data.font)
703		{
704			self.data = None
705		}
706	}
707
708	pub fn and_filter_font(mut self, f: impl FnOnce(i32) -> bool) -> Self {
709		self.filter_font(f);
710		self
711	}
712
713	pub async fn filter_font_async(&mut self, f: impl AsyncFnOnce(i32) -> bool) {
714		if let Some(data) = self.data
715			&& !f(data.font).await
716		{
717			self.data = None
718		}
719	}
720
721	pub async fn and_filter_font_async(mut self, f: impl AsyncFnOnce(i32) -> bool) -> Self {
722		self.filter_font_async(f).await;
723		self
724	}
725
726	pub fn filter_sender(&mut self, f: impl FnOnce(&GroupMessageSender) -> bool) {
727		if let Some(data) = self.data
728			&& !f(&data.sender)
729		{
730			self.data = None
731		}
732	}
733
734	pub fn and_filter_sender(mut self, f: impl FnOnce(&GroupMessageSender) -> bool) -> Self {
735		self.filter_sender(f);
736		self
737	}
738
739	pub async fn filter_sender_async(&mut self, f: impl AsyncFnOnce(&GroupMessageSender) -> bool) {
740		if let Some(data) = self.data
741			&& !f(&data.sender).await
742		{
743			self.data = None
744		}
745	}
746
747	pub async fn and_filter_sender_async(
748		mut self,
749		f: impl AsyncFnOnce(&GroupMessageSender) -> bool,
750	) -> Self {
751		self.filter_sender_async(f).await;
752		self
753	}
754
755	pub fn normal(&mut self) {
756		if let Some(data) = self.data
757			&& !data.sub_type.is_normal()
758		{
759			self.data = None
760		}
761	}
762
763	pub fn and_normal(mut self) -> Self {
764		self.normal();
765		self
766	}
767
768	pub fn not_normal(&mut self) {
769		if let Some(data) = self.data
770			&& data.sub_type.is_normal()
771		{
772			self.data = None
773		}
774	}
775
776	pub fn and_not_normal(mut self) -> Self {
777		self.not_normal();
778		self
779	}
780
781	pub fn anonymous(&mut self) {
782		if let Some(data) = self.data
783			&& !data.sub_type.is_anonymous()
784		{
785			self.data = None
786		}
787	}
788
789	pub fn and_anonymous(mut self) -> Self {
790		self.anonymous();
791		self
792	}
793
794	pub fn not_anonymous(&mut self) {
795		if let Some(data) = self.data
796			&& data.sub_type.is_anonymous()
797		{
798			self.data = None
799		}
800	}
801
802	pub fn and_not_anonymous(mut self) -> Self {
803		self.not_anonymous();
804		self
805	}
806
807	pub fn notice(&mut self) {
808		if let Some(data) = self.data
809			&& !data.sub_type.is_notice()
810		{
811			self.data = None
812		}
813	}
814
815	pub fn and_notice(mut self) -> Self {
816		self.notice();
817		self
818	}
819
820	pub fn not_notice(&mut self) {
821		if let Some(data) = self.data
822			&& data.sub_type.is_notice()
823		{
824			self.data = None
825		}
826	}
827
828	pub fn and_not_notice(mut self) -> Self {
829		self.not_notice();
830		self
831	}
832}
833
834#[cfg(feature = "quick_operation")]
835#[async_trait]
836impl<T: APISender + Send + Sync> QuickSendMsg<T> for MessageEventGroup {
837	async fn send_msg(
838		&self,
839		api: &T,
840		msg: Vec<SendSegment>,
841		auto_escape: Option<bool>,
842	) -> APIResult<i32> {
843		api.send_group_msg(self.group_id, msg, auto_escape).await
844	}
845}
846
847#[derive(Deserialize, Debug, Clone, Display, EnumIs)]
848#[serde(tag = "message_type")]
849pub enum MessageEvent {
850	#[serde(rename = "private")]
851	Private(MessageEventPrivate),
852
853	#[serde(rename = "group")]
854	Group(MessageEventGroup),
855}
856
857#[cfg(feature = "quick_operation")]
858#[async_trait]
859impl<T: APISender + Send + Sync> QuickSendMsg<T> for MessageEvent {
860	async fn send_msg(
861		&self,
862		api: &T,
863		msg: Vec<SendSegment>,
864		auto_escape: Option<bool>,
865	) -> APIResult<i32> {
866		match self {
867			Self::Group(data) => data.send_msg(api, msg, auto_escape),
868			Self::Private(data) => data.send_msg(api, msg, auto_escape),
869		}
870		.await
871	}
872}
873
874impl MessageEvent {
875	#[cfg(feature = "selector")]
876	pub fn selector(&'_ self) -> Selector<'_, Self> {
877		Selector { data: Some(self) }
878	}
879
880	pub fn match_private(&self) -> Option<&MessageEventPrivate> {
881		if let Self::Private(data) = self {
882			Some(data)
883		} else {
884			None
885		}
886	}
887
888	pub fn on_private<T>(&self, handler: impl FnOnce(&MessageEventPrivate) -> T) -> Option<T> {
889		if let Self::Private(data) = self {
890			Some(handler(data))
891		} else {
892			None
893		}
894	}
895
896	pub async fn on_private_async<T>(
897		&self,
898		handler: impl AsyncFnOnce(&MessageEventPrivate) -> T,
899	) -> Option<T> {
900		if let Self::Private(data) = self {
901			Some(handler(data).await)
902		} else {
903			None
904		}
905	}
906
907	pub fn match_group(&self) -> Option<&MessageEventGroup> {
908		if let Self::Group(data) = self {
909			Some(data)
910		} else {
911			None
912		}
913	}
914
915	pub fn on_group<T>(&self, handler: impl FnOnce(&MessageEventGroup) -> T) -> Option<T> {
916		if let Self::Group(data) = self {
917			Some(handler(data))
918		} else {
919			None
920		}
921	}
922
923	pub async fn on_group_async<T>(
924		&self,
925		handler: impl AsyncFnOnce(&MessageEventGroup) -> T,
926	) -> Option<T> {
927		if let Self::Group(data) = self {
928			Some(handler(data).await)
929		} else {
930			None
931		}
932	}
933}
934
935#[cfg(feature = "selector")]
936impl<'a> Selector<'a, MessageEvent> {
937	pub fn private(&self) -> Selector<'a, MessageEventPrivate> {
938		Selector {
939			data: self.data.and_then(|d| d.match_private()),
940		}
941	}
942
943	pub fn group(&self) -> Selector<'a, MessageEventGroup> {
944		Selector {
945			data: self.data.and_then(|d| d.match_group()),
946		}
947	}
948}