-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathbuilders.rs
408 lines (332 loc) · 10.8 KB
/
builders.rs
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Builder types used for patches and other complex data structures.
//!
//! These types do not usually need to be imported, but the methods available
//! on them are very relevant to where they are used.
use serde_json::Value;
use chrono::offset::FixedOffset;
use chrono::DateTime;
use model::*;
use Object;
macro_rules! builder {
($(#[$attr:meta] $name:ident($inner:ty);)*) => {
$(
#[$attr]
#[derive(Serialize, Deserialize)]
pub struct $name($inner);
impl $name {
#[doc(hidden)]
#[inline(always)]
pub fn __build<F: FnOnce($name) -> $name>(f: F) -> $inner where $inner: Default {
Self::__apply(f, Default::default())
}
#[doc(hidden)]
pub fn __apply<F: FnOnce($name) -> $name>(f: F, inp: $inner) -> $inner {
f($name(inp)).0
}
/// Merge this builder's contents with another of the same type.
/// Keys in `other` will override those in `self`.
///
/// This method is intended to be used with deserialized
/// instances. Note that deserialization *does not* check that
/// the keys are valid for the relevant API call.
///
/// ```ignore
/// discord.edit_server(|b| b
/// .merge(serde_json::from_str(r#"{"name":"My Server"}"#)?))
/// ```
pub fn merge(mut self, other: $name) -> $name {
self.0.extend(other.0); self
}
}
)*
}
}
builder! {
/// Patch content for the `edit_server` call.
EditServer(Object);
/// Patch content for the `edit_channel` call.
EditChannel(Object);
/// Patch content for the `edit_member` call.
EditMember(Object);
/// Patch content for the `edit_profile` call.
EditProfile(Object);
/// Patch content for the `edit_user_profile` call.
EditUserProfile(Object);
/// Patch content for the `edit_role` call.
EditRole(Object);
/// Content for the `send_message` call.
SendMessage(Object);
/// `allowed_mentions` object for use within `send_message`.
AllowedMentions(Object);
/// Patch content for the `send_embed` call.
EmbedBuilder(Object);
/// Inner patch content for the `send_embed` call.
EmbedFooterBuilder(Object);
/// Inner patch content for the `send_embed` call.
EmbedAuthorBuilder(Object);
/// Inner patch content for the `send_embed` call.
EmbedFieldsBuilder(Vec<Value>);
}
macro_rules! set {
($self:ident, $key:expr, $($rest:tt)*) => {{
{let mut s = $self; s.0.insert($key.into(), json!($($rest)*)); s}
}}
}
impl EditServer {
/// Edit the server's name.
pub fn name(self, name: &str) -> Self {
set!(self, "name", name)
}
/// Edit the server's voice region.
pub fn region(self, region: &str) -> Self {
set!(self, "region", region)
}
/// Edit the server's icon. Use `None` to remove the icon.
pub fn icon(self, icon: Option<&str>) -> Self {
set!(self, "icon", icon)
}
/// Edit the server's AFK channel. Use `None` to select no AFK channel.
pub fn afk_channel(self, channel: Option<ChannelId>) -> Self {
set!(self, "afk_channel_id", channel)
}
/// Edit the server's AFK timeout.
pub fn afk_timeout(self, timeout: u64) -> Self {
set!(self, "afk_timeout", timeout)
}
/// Transfer ownership of the server to a new owner.
pub fn owner(self, owner: UserId) -> Self {
set!(self, "owner_id", owner.0)
}
/// Edit the verification level of the server.
pub fn verification_level(self, verification_level: VerificationLevel) -> Self {
set!(self, "verification_level", verification_level)
}
/// Edit the server's splash. Use `None` to remove the splash.
pub fn splash(self, splash: Option<&str>) -> Self {
set!(self, "splash", splash)
}
}
impl EditChannel {
/// Edit the channel's name.
pub fn name(self, name: &str) -> Self {
set!(self, "name", name)
}
/// Edit the text channel's topic.
pub fn topic(self, topic: &str) -> Self {
set!(self, "topic", topic)
}
/// Edit the channel's position in the list.
pub fn position(self, position: u64) -> Self {
set!(self, "position", position)
}
/// Edit the voice channel's bitrate.
pub fn bitrate(self, bitrate: u64) -> Self {
set!(self, "bitrate", bitrate)
}
/// Edit the voice channel's user limit. Zero (`0`) means unlimited.
pub fn user_limit(self, user_limit: u64) -> Self {
set!(self, "user_limit", user_limit)
}
}
impl EditMember {
/// Edit the member's nickname. Supply the empty string to remove a nickname.
pub fn nickname(self, nick: &str) -> Self {
set!(self, "nick", nick)
}
/// Edit whether the member is server-muted.
pub fn mute(self, mute: bool) -> Self {
set!(self, "mute", mute)
}
/// Edit whether the member is server-deafened.
pub fn deaf(self, deafen: bool) -> Self {
set!(self, "deaf", deafen)
}
/// Edit the member's assigned roles.
pub fn roles(self, roles: &[RoleId]) -> Self {
set!(self, "roles", roles)
}
/// Move the member to another voice channel.
pub fn channel(self, channel: ChannelId) -> Self {
set!(self, "channel_id", channel.0)
}
}
impl EditProfile {
/// Edit the user's username. Must be between 2 and 32 characters long.
pub fn username(self, username: &str) -> Self {
set!(self, "username", username)
}
/// Edit the user's avatar. Use `None` to remove the avatar.
pub fn avatar(self, icon: Option<&str>) -> Self {
set!(self, "avatar", icon)
}
}
impl EditUserProfile {
/// Provide the user's current password for authentication. Required if
/// the email or password is being changed.
pub fn password(self, password: &str) -> Self {
set!(self, "password", password)
}
/// Edit the user's email address.
pub fn email(self, email: &str) -> Self {
set!(self, "email", email)
}
/// Edit the user's password.
pub fn new_password(self, password: &str) -> Self {
set!(self, "new_password", password)
}
/// Edit the user's username. Must be between 2 and 32 characters long.
pub fn username(self, username: &str) -> Self {
set!(self, "username", username)
}
/// Edit the user's avatar. Use `None` to remove the avatar.
pub fn avatar(self, icon: Option<&str>) -> Self {
set!(self, "avatar", icon)
}
}
impl EditRole {
/// Edit the role's name. Supply the empty string to remove a name.
pub fn name(self, name: &str) -> Self {
set!(self, "name", name)
}
/// Edit the role's permissions.
pub fn permissions(self, permissions: Permissions) -> Self {
set!(self, "permissions", permissions)
}
/// Edit the role's color. Set to zero for default.
pub fn color(self, color: u64) -> Self {
set!(self, "color", color)
}
/// Edit the role's hoist status (whether the role should be displayed separately in the sidebar).
pub fn hoist(self, hoist: bool) -> Self {
set!(self, "hoist", hoist)
}
/// Edit the role's mentionability, if the role can be mentioned.
pub fn mentionable(self, mentionable: bool) -> Self {
set!(self, "mentionable", mentionable)
}
}
impl SendMessage {
/// Set the text content of the message.
pub fn content(self, content: &str) -> Self {
set!(self, "content", content)
}
/// Set a nonce that can be used for optimistic message sending.
pub fn nonce(self, nonce: &str) -> Self {
set!(self, "nonce", nonce)
}
/// Set to true to use text-to-speech.
pub fn tts(self, tts: bool) -> Self {
set!(self, "tts", tts)
}
/// Embed rich content.
pub fn embed<F: FnOnce(EmbedBuilder) -> EmbedBuilder>(self, f: F) -> Self {
set!(self, "embed", EmbedBuilder::__build(f))
}
/// Restrict allowed mentions for this message.
pub fn allowed_mentions<F: FnOnce(AllowedMentions) -> AllowedMentions>(self, f: F) -> Self {
set!(self, "allowed_mentions", AllowedMentions::__build(f))
}
/// Reply to the given message, optionally mentioning the sender.
///
/// The given `message_id` must be in the same channel that this message is
/// being sent to.
pub fn reply(self, message_id: MessageId, mention: bool) -> Self {
set!(self, "message_reference", json! {{
"message_id": message_id,
}}).allowed_mentions(|b| b.replied_user(mention))
}
/// Change the message's flags.
///
/// Can only be set while editing. Only `SUPPRESS_EMBEDS` can be edited on
/// request.
pub fn flags(self, flags: MessageFlags) -> Self {
set!(self, "flags", flags)
}
// TODO: file, payload_json, message_reference
}
impl AllowedMentions {
// TODO: parse, roles, users
/// Set to `false` to disable mentioning a replied-to user.
pub fn replied_user(self, replied_user: bool) -> Self {
set!(self, "replied_user", replied_user)
}
}
impl EmbedBuilder {
/// Add the "title of embed".
pub fn title(self, title: &str) -> Self {
set!(self, "title", title)
}
/// Add the "description of embed".
pub fn description(self, description: &str) -> Self {
set!(self, "description", description)
}
/// Add the "url of embed".
pub fn url(self, url: &str) -> Self {
set!(self, "url", url)
}
/// Add the "timestamp of embed content".
pub fn timestamp(self, timestamp: DateTime<FixedOffset>) -> Self {
set!(self, "timestamp", timestamp.to_rfc3339())
}
/// Add the "color code of the embed".
pub fn color(self, color: u64) -> Self {
set!(self, "color", color)
}
/// Add "footer information". See the `EmbedFooterBuilder` struct for the editable fields.
pub fn footer<F: FnOnce(EmbedFooterBuilder) -> EmbedFooterBuilder>(self, f: F) -> Self {
set!(self, "footer", EmbedFooterBuilder::__build(f))
}
/// Add "source url of image". Only supports http(s).
pub fn image(self, url: &str) -> Self {
set!(self, "image", { "url": url })
}
/// Add "source url of thumbnail". Only supports http(s).
pub fn thumbnail(self, url: &str) -> Self {
set!(self, "thumbnail", { "url": url })
}
/// Add "author information". See the `EmbedAuthorBuilder` struct for the editable fields.
pub fn author<F: FnOnce(EmbedAuthorBuilder) -> EmbedAuthorBuilder>(self, f: F) -> Self {
set!(self, "author", EmbedAuthorBuilder::__build(f))
}
/// Add "fields information". See the `EmbedFieldsBuilder` struct for the editable fields.
pub fn fields<F: FnOnce(EmbedFieldsBuilder) -> EmbedFieldsBuilder>(self, f: F) -> Self {
set!(self, "fields", EmbedFieldsBuilder::__build(f))
}
}
impl EmbedFooterBuilder {
/// Add the "footer text".
pub fn text(self, text: &str) -> Self {
set!(self, "text", text)
}
/// Add the "url of footer icon". Only the http(s) protocols are supported.
pub fn icon_url(self, icon_url: &str) -> Self {
set!(self, "icon_url", icon_url)
}
}
impl EmbedAuthorBuilder {
/// Add the "name of author".
pub fn name(self, name: &str) -> Self {
set!(self, "name", name)
}
/// Add the "url of author".
pub fn url(self, url: &str) -> Self {
set!(self, "url", url)
}
/// Add the "url of author icon". Only the http(s) protocols are supported.
pub fn icon_url(self, icon_url: &str) -> Self {
set!(self, "icon_url", icon_url)
}
}
impl EmbedFieldsBuilder {
/// Add an entire field structure, representing a mapping from `name` to `value`.
///
/// `inline` determines "whether or not this field should display inline".
pub fn field(mut self, name: &str, value: &str, inline: bool) -> Self {
self.0.push(json! {{
"name": name,
"value": value,
"inline": inline,
}});
self
}
}