Skip to main content

onebot_api/message/
segment_builder.rs

1use super::send_segment::*;
2use super::utils::*;
3
4#[derive(Default, Debug, Clone)]
5pub struct SegmentBuilder {
6	segments: Vec<SendSegment>,
7}
8
9impl From<Vec<SendSegment>> for SegmentBuilder {
10	fn from(value: Vec<SendSegment>) -> Self {
11		Self { segments: value }
12	}
13}
14
15impl SegmentBuilder {
16	pub fn new() -> Self {
17		Self::default()
18	}
19
20	pub fn build(self) -> Vec<SendSegment> {
21		self.segments
22	}
23
24	pub fn text(mut self, text: impl ToString) -> Self {
25		self.segments.push(SendSegment::Text {
26			data: TextData {
27				text: text.to_string(),
28			},
29		});
30		self
31	}
32
33	pub fn face(mut self, id: impl ToString) -> Self {
34		self.segments.push(SendSegment::Face {
35			data: FaceData { id: id.to_string() },
36		});
37		self
38	}
39
40	pub fn image(
41		mut self,
42		file: impl ToString,
43		image_type: Option<ImageType>,
44		cache: Option<bool>,
45		proxy: Option<bool>,
46		timeout: Option<i32>,
47	) -> Self {
48		self.segments.push(SendSegment::Image {
49			data: ImageData {
50				file: file.to_string(),
51				image_type,
52				cache,
53				proxy,
54				timeout,
55			},
56		});
57		self
58	}
59
60	pub fn record(
61		mut self,
62		file: impl ToString,
63		magic: impl ToString,
64		cache: Option<bool>,
65		proxy: Option<bool>,
66		timeout: Option<i32>,
67	) -> Self {
68		self.segments.push(SendSegment::Record {
69			data: RecordData {
70				file: file.to_string(),
71				magic: magic.to_string(),
72				cache,
73				proxy,
74				timeout,
75			},
76		});
77		self
78	}
79
80	pub fn video(
81		mut self,
82		file: impl ToString,
83		cache: Option<bool>,
84		proxy: Option<bool>,
85		timeout: Option<i32>,
86	) -> Self {
87		self.segments.push(SendSegment::Video {
88			data: VideoData {
89				file: file.to_string(),
90				cache,
91				proxy,
92				timeout,
93			},
94		});
95		self
96	}
97
98	pub fn at(mut self, qq: AtType) -> Self {
99		self.segments.push(SendSegment::At {
100			data: AtData { qq },
101		});
102		self
103	}
104
105	pub fn rps(mut self) -> Self {
106		self.segments.push(SendSegment::Rps { data: RpsData {} });
107		self
108	}
109
110	pub fn dice(mut self) -> Self {
111		self.segments.push(SendSegment::Dice { data: DiceData {} });
112		self
113	}
114
115	pub fn shake(mut self) -> Self {
116		self
117			.segments
118			.push(SendSegment::Shake { data: ShakeData {} });
119		self
120	}
121
122	pub fn poke(mut self, poke_type: impl ToString, id: impl ToString) -> Self {
123		self.segments.push(SendSegment::Poke {
124			data: PokeData {
125				poke_type: poke_type.to_string(),
126				id: id.to_string(),
127			},
128		});
129		self
130	}
131
132	pub fn anonymous(mut self, ignore: Option<bool>) -> Self {
133		self.segments.push(SendSegment::Anonymous {
134			data: AnonymousData { ignore },
135		});
136		self
137	}
138
139	pub fn share(
140		mut self,
141		url: impl ToString,
142		title: impl ToString,
143		content: impl ToString,
144		image: impl ToString,
145	) -> Self {
146		self.segments.push(SendSegment::Share {
147			data: ShareData {
148				url: url.to_string(),
149				title: title.to_string(),
150				content: content.to_string(),
151				image: image.to_string(),
152			},
153		});
154		self
155	}
156
157	pub fn contact(mut self, contact_type: ContactType, id: impl ToString) -> Self {
158		self.segments.push(SendSegment::Contact {
159			data: ContactData {
160				contact_type,
161				id: id.to_string(),
162			},
163		});
164		self
165	}
166
167	pub fn contact_qq(self, id: impl ToString) -> Self {
168		self.contact(ContactType::QQ, id)
169	}
170
171	pub fn contact_group(self, id: impl ToString) -> Self {
172		self.contact(ContactType::Group, id)
173	}
174
175	pub fn location(
176		mut self,
177		lat: impl ToString,
178		lon: impl ToString,
179		title: Option<String>,
180		content: Option<String>,
181	) -> Self {
182		self.segments.push(SendSegment::Location {
183			data: LocationData {
184				lat: lat.to_string(),
185				lon: lon.to_string(),
186				title,
187				content,
188			},
189		});
190		self
191	}
192
193	#[allow(clippy::too_many_arguments)]
194	pub fn music(
195		mut self,
196		music_type: MusicType,
197		id: Option<String>,
198		url: Option<String>,
199		audio: Option<String>,
200		title: Option<String>,
201		content: Option<String>,
202		image: Option<String>,
203	) -> Self {
204		self.segments.push(SendSegment::Music {
205			data: MusicData {
206				music_type,
207				id,
208				url,
209				audio,
210				title,
211				content,
212				image,
213			},
214		});
215		self
216	}
217
218	pub fn music_typed(self, music_type: MusicType, id: impl ToString) -> Self {
219		self.music(
220			music_type,
221			Some(id.to_string()),
222			None,
223			None,
224			None,
225			None,
226			None,
227		)
228	}
229
230	pub fn music_qq(self, id: impl ToString) -> Self {
231		self.music_typed(MusicType::QQ, id)
232	}
233
234	pub fn music_163(self, id: impl ToString) -> Self {
235		self.music_typed(MusicType::NetEaseCloudMusic, id)
236	}
237
238	pub fn music_xm(self, id: impl ToString) -> Self {
239		self.music_typed(MusicType::Xm, id)
240	}
241
242	pub fn music_custom(
243		self,
244		url: impl ToString,
245		audio: impl ToString,
246		title: impl ToString,
247		content: impl ToString,
248		image: impl ToString,
249	) -> Self {
250		self.music(
251			MusicType::Custom,
252			None,
253			Some(url.to_string()),
254			Some(audio.to_string()),
255			Some(title.to_string()),
256			Some(content.to_string()),
257			Some(image.to_string()),
258		)
259	}
260
261	pub fn reply(mut self, id: impl ToString) -> Self {
262		self.segments.push(SendSegment::Reply {
263			data: ReplyData { id: id.to_string() },
264		});
265		self
266	}
267
268	pub fn forward(mut self) -> Self {
269		self.segments.push(SendSegment::Forward {
270			data: ForwardData {},
271		});
272		self
273	}
274
275	pub fn node(
276		mut self,
277		id: Option<String>,
278		user_id: Option<String>,
279		nickname: Option<String>,
280		content: Option<Vec<SendSegment>>,
281	) -> Self {
282		self.segments.push(SendSegment::Node {
283			data: NodeData {
284				id,
285				user_id,
286				nickname,
287				content,
288			},
289		});
290		self
291	}
292
293	pub fn node_forward(self, id: impl ToString) -> Self {
294		self.node(Some(id.to_string()), None, None, None)
295	}
296
297	pub fn node_custom(
298		self,
299		user_id: impl ToString,
300		nickname: impl ToString,
301		content: Vec<SendSegment>,
302	) -> Self {
303		self.node(
304			None,
305			Some(user_id.to_string()),
306			Some(nickname.to_string()),
307			Some(content),
308		)
309	}
310
311	pub fn xml(mut self, data: impl ToString) -> Self {
312		self.segments.push(SendSegment::Xml {
313			data: XmlData {
314				data: data.to_string(),
315			},
316		});
317		self
318	}
319
320	pub fn json(mut self, data: impl ToString) -> Self {
321		self.segments.push(SendSegment::Json {
322			data: JsonData {
323				data: data.to_string(),
324			},
325		});
326		self
327	}
328}
329
330#[macro_export]
331macro_rules! text {
332	( $( $arg: tt )* ) => {
333		{
334			$crate::message::segment_builder::SegmentBuilder::new().text( format!( $( $arg )* ) ).build()
335		}
336	};
337}