1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::collections::HashMap;
use crate::internal::prelude::*;
use crate::model::channel::ReactionType;
use crate::model::interactions::message_component::ButtonStyle;
use crate::utils;
#[derive(Clone, Debug, Default)]
pub struct CreateComponents(pub Vec<Value>);
impl CreateComponents {
pub fn create_action_row<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateActionRow) -> &mut CreateActionRow,
{
let mut data = CreateActionRow::default();
f(&mut data);
self.add_action_row(data);
self
}
pub fn add_action_row(&mut self, mut row: CreateActionRow) -> &mut Self {
self.0.push(row.build());
self
}
pub fn set_action_rows(&mut self, rows: Vec<CreateActionRow>) -> &mut Self {
let new_rows = rows.into_iter().map(|mut f| f.build()).collect::<Vec<Value>>();
for row in new_rows {
self.0.push(row);
}
self
}
}
#[derive(Clone, Debug, Default)]
pub struct CreateActionRow(pub HashMap<&'static str, Value>);
impl CreateActionRow {
pub fn create_button<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateButton) -> &mut CreateButton,
{
let mut data = CreateButton::default();
f(&mut data);
self.add_button(data);
self
}
pub fn add_button(&mut self, button: CreateButton) -> &mut Self {
let components = self.0.entry("components").or_insert_with(|| Value::Array(Vec::new()));
let components_array = components.as_array_mut().expect("Must be an array");
components_array.push(button.build());
self
}
pub fn create_select_menu<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateSelectMenu) -> &mut CreateSelectMenu,
{
let mut data = CreateSelectMenu::default();
f(&mut data);
self.add_select_menu(data);
self
}
pub fn add_select_menu(&mut self, menu: CreateSelectMenu) -> &mut Self {
let components = self.0.entry("components").or_insert_with(|| Value::Array(Vec::new()));
let components_array = components.as_array_mut().expect("Must be an array");
components_array.push(menu.build());
self
}
pub fn build(&mut self) -> Value {
self.0.insert("type", Value::Number(serde_json::Number::from(1_u8)));
utils::hashmap_to_json_map(self.0.clone()).into()
}
}
#[derive(Clone, Debug, Default)]
pub struct CreateButton(pub HashMap<&'static str, Value>);
impl CreateButton {
pub fn style(&mut self, kind: ButtonStyle) -> &mut Self {
self.0.insert("style", Value::Number(serde_json::Number::from(kind as u8)));
self
}
pub fn label<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("label", Value::String(label.to_string()));
self
}
pub fn custom_id<D: ToString>(&mut self, id: D) -> &mut Self {
self.0.insert("custom_id", Value::String(id.to_string()));
self
}
pub fn url<D: ToString>(&mut self, url: D) -> &mut Self {
self.0.insert("url", Value::String(url.to_string()));
self
}
pub fn emoji(&mut self, emoji: ReactionType) -> &mut Self {
let mut map = JsonMap::new();
match emoji {
ReactionType::Unicode(u) => {
map.insert("name".to_string(), Value::String(u));
},
ReactionType::Custom {
animated,
id,
name,
} => {
map.insert("animated".to_string(), Value::Bool(animated));
map.insert("id".to_string(), Value::String(id.to_string()));
if let Some(name) = name {
map.insert("name".to_string(), Value::String(name));
}
},
};
self.0.insert("emoji", Value::Object(map));
self
}
pub fn disabled(&mut self, disabled: bool) -> &mut Self {
self.0.insert("disabled", Value::Bool(disabled));
self
}
pub fn build(mut self) -> Value {
self.0.insert("type", Value::Number(serde_json::Number::from(2_u8)));
utils::hashmap_to_json_map(self.0.clone()).into()
}
}
#[derive(Clone, Debug, Default)]
pub struct CreateSelectMenu(pub HashMap<&'static str, Value>);
impl CreateSelectMenu {
pub fn placeholder<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("placeholder", Value::String(label.to_string()));
self
}
pub fn custom_id<D: ToString>(&mut self, id: D) -> &mut Self {
self.0.insert("custom_id", Value::String(id.to_string()));
self
}
pub fn min_values(&mut self, min: u64) -> &mut Self {
self.0.insert("min_values", Value::Number(Number::from(min)));
self
}
pub fn max_values(&mut self, max: u64) -> &mut Self {
self.0.insert("max_values", Value::Number(Number::from(max)));
self
}
pub fn disabled(&mut self, disabled: bool) -> &mut Self {
self.0.insert("disabled", Value::Bool(disabled));
self
}
pub fn options<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateSelectMenuOptions) -> &mut CreateSelectMenuOptions,
{
let mut data = CreateSelectMenuOptions::default();
f(&mut data);
self.0.insert("options", Value::Array(data.0));
self
}
pub fn build(mut self) -> Value {
self.0.insert("type", Value::Number(serde_json::Number::from(3_u8)));
utils::hashmap_to_json_map(self.0.clone()).into()
}
}
#[derive(Clone, Debug, Default)]
pub struct CreateSelectMenuOptions(pub Vec<Value>);
impl CreateSelectMenuOptions {
pub fn create_option<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateSelectMenuOption) -> &mut CreateSelectMenuOption,
{
let mut data = CreateSelectMenuOption::default();
f(&mut data);
self.add_option(data);
self
}
pub fn add_option(&mut self, option: CreateSelectMenuOption) -> &mut Self {
let data = utils::hashmap_to_json_map(option.0);
self.0.push(data.into());
self
}
pub fn set_options(&mut self, options: Vec<CreateSelectMenuOption>) -> &mut Self {
let new_options = options
.into_iter()
.map(|option| utils::hashmap_to_json_map(option.0).into())
.collect::<Vec<Value>>();
for option in new_options {
self.0.push(option);
}
self
}
}
#[derive(Clone, Debug, Default)]
pub struct CreateSelectMenuOption(pub HashMap<&'static str, Value>);
impl CreateSelectMenuOption {
pub fn label<D: ToString>(&mut self, label: D) -> &mut Self {
self.0.insert("label", Value::String(label.to_string()));
self
}
pub fn value<D: ToString>(&mut self, value: D) -> &mut Self {
self.0.insert("value", Value::String(value.to_string()));
self
}
pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
self.0.insert("description", Value::String(description.to_string()));
self
}
pub fn emoji(&mut self, emoji: ReactionType) -> &mut Self {
let mut map = JsonMap::new();
match emoji {
ReactionType::Unicode(u) => {
map.insert("name".to_string(), Value::String(u));
},
ReactionType::Custom {
animated,
id,
name,
} => {
map.insert("animated".to_string(), Value::Bool(animated));
map.insert("id".to_string(), Value::String(id.to_string()));
if let Some(name) = name {
map.insert("name".to_string(), Value::String(name));
}
},
};
self.0.insert("emoji", Value::Object(map));
self
}
pub fn default_selection(&mut self, disabled: bool) -> &mut Self {
self.0.insert("default", Value::Bool(disabled));
self
}
}