Skip to main content

onebot_api/event/
request.rs

1#[cfg(feature = "selector")]
2use crate::selector::Selector;
3use serde::Deserialize;
4use strum::{Display, EnumIs};
5
6#[derive(Deserialize, Debug, Copy, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
7pub enum GroupType {
8	#[serde(rename = "add")]
9	Add,
10	#[serde(rename = "invite")]
11	Invite,
12}
13
14#[derive(Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
15pub struct RequestEventFriend {
16	user_id: i64,
17	comment: String,
18	flag: String,
19}
20
21impl RequestEventFriend {
22	#[cfg(feature = "selector")]
23	pub fn selector(&'_ self) -> Selector<'_, Self> {
24		Selector { data: Some(self) }
25	}
26}
27
28#[cfg(feature = "selector")]
29impl<'a> Selector<'a, RequestEventFriend> {
30	pub fn filter(&mut self, f: impl FnOnce(&RequestEventFriend) -> bool) {
31		if let Some(data) = self.data
32			&& !f(data)
33		{
34			self.data = None
35		}
36	}
37
38	pub fn and_filter(mut self, f: impl FnOnce(&RequestEventFriend) -> bool) -> Self {
39		self.filter(f);
40		self
41	}
42
43	pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&RequestEventFriend) -> bool) {
44		if let Some(data) = self.data
45			&& !f(data).await
46		{
47			self.data = None
48		}
49	}
50
51	pub async fn and_filter_async(
52		mut self,
53		f: impl AsyncFnOnce(&RequestEventFriend) -> bool,
54	) -> Self {
55		self.filter_async(f).await;
56		self
57	}
58
59	pub fn filter_user_id(&mut self, f: impl FnOnce(i64) -> bool) {
60		if let Some(data) = self.data
61			&& !f(data.user_id)
62		{
63			self.data = None
64		}
65	}
66
67	pub fn and_filter_user_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
68		self.filter_user_id(f);
69		self
70	}
71
72	pub async fn filter_user_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
73		if let Some(data) = self.data
74			&& !f(data.user_id).await
75		{
76			self.data = None
77		}
78	}
79
80	pub async fn and_filter_user_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
81		self.filter_user_id_async(f).await;
82		self
83	}
84
85	pub fn filter_comment(&mut self, f: impl FnOnce(&str) -> bool) {
86		if let Some(data) = self.data
87			&& !f(&data.comment)
88		{
89			self.data = None
90		}
91	}
92
93	pub fn and_filter_comment(mut self, f: impl FnOnce(&str) -> bool) -> Self {
94		self.filter_comment(f);
95		self
96	}
97
98	pub async fn filter_comment_async(&mut self, f: impl AsyncFnOnce(&str) -> bool) {
99		if let Some(data) = self.data
100			&& !f(&data.comment).await
101		{
102			self.data = None
103		}
104	}
105
106	pub async fn and_filter_comment_async(mut self, f: impl AsyncFnOnce(&str) -> bool) -> Self {
107		self.filter_comment_async(f).await;
108		self
109	}
110
111	pub fn filter_flag(&mut self, f: impl FnOnce(&str) -> bool) {
112		if let Some(data) = self.data
113			&& !f(&data.flag)
114		{
115			self.data = None
116		}
117	}
118
119	pub fn and_filter_flag(mut self, f: impl FnOnce(&str) -> bool) -> Self {
120		self.filter_flag(f);
121		self
122	}
123
124	pub async fn filter_flag_async(&mut self, f: impl AsyncFnOnce(&str) -> bool) {
125		if let Some(data) = self.data
126			&& !f(&data.flag).await
127		{
128			self.data = None
129		}
130	}
131
132	pub async fn and_filter_flag_async(mut self, f: impl AsyncFnOnce(&str) -> bool) -> Self {
133		self.filter_flag_async(f).await;
134		self
135	}
136}
137
138#[derive(Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
139pub struct RequestEventGroup {
140	sub_type: GroupType,
141	group_id: i64,
142	user_id: i64,
143	comment: String,
144	flag: String,
145}
146
147impl RequestEventGroup {
148	#[cfg(feature = "selector")]
149	pub fn selector(&'_ self) -> Selector<'_, Self> {
150		Selector { data: Some(self) }
151	}
152}
153
154#[cfg(feature = "selector")]
155impl<'a> Selector<'a, RequestEventGroup> {
156	pub fn filter(&mut self, f: impl FnOnce(&RequestEventGroup) -> bool) {
157		if let Some(data) = self.data
158			&& !f(data)
159		{
160			self.data = None
161		}
162	}
163
164	pub fn and_filter(mut self, f: impl FnOnce(&RequestEventGroup) -> bool) -> Self {
165		self.filter(f);
166		self
167	}
168
169	pub async fn filter_async(&mut self, f: impl AsyncFnOnce(&RequestEventGroup) -> bool) {
170		if let Some(data) = self.data
171			&& !f(data).await
172		{
173			self.data = None
174		}
175	}
176
177	pub async fn and_filter_async(mut self, f: impl AsyncFnOnce(&RequestEventGroup) -> bool) -> Self {
178		self.filter_async(f).await;
179		self
180	}
181
182	pub fn filter_sub_type(&mut self, f: impl FnOnce(GroupType) -> bool) {
183		if let Some(data) = self.data
184			&& !f(data.sub_type)
185		{
186			self.data = None
187		}
188	}
189
190	pub fn and_filter_sub_type(mut self, f: impl FnOnce(GroupType) -> bool) -> Self {
191		self.filter_sub_type(f);
192		self
193	}
194
195	pub async fn filter_sub_type_async(&mut self, f: impl AsyncFnOnce(GroupType) -> bool) {
196		if let Some(data) = self.data
197			&& !f(data.sub_type).await
198		{
199			self.data = None
200		}
201	}
202
203	pub async fn and_filter_sub_type_async(mut self, f: impl AsyncFnOnce(GroupType) -> bool) -> Self {
204		self.filter_sub_type_async(f).await;
205		self
206	}
207
208	pub fn filter_group_id(&mut self, f: impl FnOnce(i64) -> bool) {
209		if let Some(data) = self.data
210			&& !f(data.group_id)
211		{
212			self.data = None
213		}
214	}
215
216	pub fn and_filter_group_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
217		self.filter_group_id(f);
218		self
219	}
220
221	pub async fn filter_group_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
222		if let Some(data) = self.data
223			&& !f(data.group_id).await
224		{
225			self.data = None
226		}
227	}
228
229	pub async fn and_filter_group_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
230		self.filter_group_id_async(f).await;
231		self
232	}
233
234	pub fn filter_user_id(&mut self, f: impl FnOnce(i64) -> bool) {
235		if let Some(data) = self.data
236			&& !f(data.user_id)
237		{
238			self.data = None
239		}
240	}
241
242	pub fn and_filter_user_id(mut self, f: impl FnOnce(i64) -> bool) -> Self {
243		self.filter_user_id(f);
244		self
245	}
246
247	pub async fn filter_user_id_async(&mut self, f: impl AsyncFnOnce(i64) -> bool) {
248		if let Some(data) = self.data
249			&& !f(data.user_id).await
250		{
251			self.data = None
252		}
253	}
254
255	pub async fn and_filter_user_id_async(mut self, f: impl AsyncFnOnce(i64) -> bool) -> Self {
256		self.filter_user_id_async(f).await;
257		self
258	}
259
260	pub fn filter_comment(&mut self, f: impl FnOnce(&str) -> bool) {
261		if let Some(data) = self.data
262			&& !f(&data.comment)
263		{
264			self.data = None
265		}
266	}
267
268	pub fn and_filter_comment(mut self, f: impl FnOnce(&str) -> bool) -> Self {
269		self.filter_comment(f);
270		self
271	}
272
273	pub async fn filter_comment_async(&mut self, f: impl AsyncFnOnce(&str) -> bool) {
274		if let Some(data) = self.data
275			&& !f(&data.comment).await
276		{
277			self.data = None
278		}
279	}
280
281	pub async fn and_filter_comment_async(mut self, f: impl AsyncFnOnce(&str) -> bool) -> Self {
282		self.filter_comment_async(f).await;
283		self
284	}
285
286	pub fn filter_flag(&mut self, f: impl FnOnce(&str) -> bool) {
287		if let Some(data) = self.data
288			&& !f(&data.flag)
289		{
290			self.data = None
291		}
292	}
293
294	pub fn and_filter_flag(mut self, f: impl FnOnce(&str) -> bool) -> Self {
295		self.filter_flag(f);
296		self
297	}
298
299	pub async fn filter_flag_async(&mut self, f: impl AsyncFnOnce(&str) -> bool) {
300		if let Some(data) = self.data
301			&& !f(&data.flag).await
302		{
303			self.data = None
304		}
305	}
306
307	pub async fn and_filter_flag_async(mut self, f: impl AsyncFnOnce(&str) -> bool) -> Self {
308		self.filter_flag_async(f).await;
309		self
310	}
311
312	pub fn add(&mut self) {
313		if let Some(data) = self.data
314			&& !data.sub_type.is_add()
315		{
316			self.data = None
317		}
318	}
319
320	pub fn and_add(mut self) -> Self {
321		self.add();
322		self
323	}
324
325	pub fn not_add(&mut self) {
326		if let Some(data) = self.data
327			&& data.sub_type.is_add()
328		{
329			self.data = None
330		}
331	}
332
333	pub fn and_not_add(mut self) -> Self {
334		self.not_add();
335		self
336	}
337
338	pub fn invite(&mut self) {
339		if let Some(data) = self.data
340			&& !data.sub_type.is_invite()
341		{
342			self.data = None
343		}
344	}
345
346	pub fn and_invite(mut self) -> Self {
347		self.invite();
348		self
349	}
350
351	pub fn not_invite(&mut self) {
352		if let Some(data) = self.data
353			&& data.sub_type.is_invite()
354		{
355			self.data = None
356		}
357	}
358
359	pub fn and_not_invite(mut self) -> Self {
360		self.not_invite();
361		self
362	}
363}
364
365#[derive(Deserialize, Debug, Clone, Display, EnumIs, Ord, PartialOrd, Eq, PartialEq)]
366#[serde(tag = "request_type")]
367pub enum RequestEvent {
368	#[serde(rename = "friend")]
369	Friend(RequestEventFriend),
370
371	#[serde(rename = "group")]
372	Group(RequestEventGroup),
373}
374
375impl RequestEvent {
376	#[cfg(feature = "selector")]
377	pub fn selector(&'_ self) -> Selector<'_, Self> {
378		Selector { data: Some(self) }
379	}
380
381	pub fn match_friend(&self) -> Option<&RequestEventFriend> {
382		if let Self::Friend(data) = self {
383			Some(data)
384		} else {
385			None
386		}
387	}
388
389	pub fn on_friend<T>(&self, handler: impl FnOnce(&RequestEventFriend) -> T) -> Option<T> {
390		if let Self::Friend(data) = self {
391			Some(handler(data))
392		} else {
393			None
394		}
395	}
396
397	pub async fn on_friend_async<T>(
398		&self,
399		handler: impl AsyncFnOnce(&RequestEventFriend) -> T,
400	) -> Option<T> {
401		if let Self::Friend(data) = self {
402			Some(handler(data).await)
403		} else {
404			None
405		}
406	}
407
408	pub fn match_group(&self) -> Option<&RequestEventGroup> {
409		if let Self::Group(data) = self {
410			Some(data)
411		} else {
412			None
413		}
414	}
415
416	pub fn on_group<T>(&self, handler: impl FnOnce(&RequestEventGroup) -> T) -> Option<T> {
417		if let Self::Group(data) = self {
418			Some(handler(data))
419		} else {
420			None
421		}
422	}
423
424	pub async fn on_group_async<T>(
425		&self,
426		handler: impl AsyncFnOnce(&RequestEventGroup) -> T,
427	) -> Option<T> {
428		if let Self::Group(data) = self {
429			Some(handler(data).await)
430		} else {
431			None
432		}
433	}
434}
435
436#[cfg(feature = "selector")]
437impl<'a> Selector<'a, RequestEvent> {
438	pub fn friend(&self) -> Selector<'a, RequestEventFriend> {
439		Selector {
440			data: self.data.and_then(|d| d.match_friend()),
441		}
442	}
443
444	pub fn group(&self) -> Selector<'a, RequestEventGroup> {
445		Selector {
446			data: self.data.and_then(|d| d.match_group()),
447		}
448	}
449}