-
Notifications
You must be signed in to change notification settings - Fork 563
/
create_embed.rs
415 lines (342 loc) · 11.8 KB
/
create_embed.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
409
410
411
412
413
414
415
//! Developer note:
//!
//! This is a set of embed builders for rich embeds.
//!
//! These are used in the [`ChannelId::send_message`] and
//! [`ExecuteWebhook::embeds`] methods, both as part of builders.
//!
//! The only builder that should be exposed is [`CreateEmbed`]. The rest of
//! these have no real reason for being exposed, but are for completeness' sake.
//!
//! Documentation for embeds can be found [here].
//!
//! [`ChannelId::send_message`]: ../model/struct.ChannelId.html#method.send_message
//! [`CreateEmbed`]: struct.CreateEmbed.html
//! [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds
//! [here]: https://discordapp.com/developers/docs/resources/channel#embed-object
use serde_json::Value;
use std::default::Default;
use time::Tm;
use ::internal::prelude::*;
use ::model::Embed;
#[cfg(feature="utils")]
use ::utils::Colour;
/// A builder to create a fake [`Embed`] object, for use with the
/// [`ChannelId::send_message`] and [`ExecuteWebhook::embeds`] methods.
///
/// # Examples
///
/// Refer to the documentation for [`ChannelId::send_message`] for a very in-depth
/// example on how to use this.
///
/// [`ChannelId::send_message`]: ../model/struct.ChannelId.html#method.send_message
/// [`Embed`]: ../model/struct.Embed.html
/// [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds
#[derive(Clone, Debug)]
pub struct CreateEmbed(pub Map<String, Value>);
impl CreateEmbed {
/// Set the author of the embed.
///
/// Refer to the documentation for [`CreateEmbedAuthor`] for more
/// information.
///
/// [`CreateEmbedAuthor`]: struct.CreateEmbedAuthor.html
pub fn author<F>(mut self, f: F) -> Self
where F: FnOnce(CreateEmbedAuthor) -> CreateEmbedAuthor {
let author = f(CreateEmbedAuthor::default()).0;
self.0.insert("author".to_owned(), Value::Object(author));
CreateEmbed(self.0)
}
/// Set the colour of the left-hand side of the embed.
///
/// This is an alias of [`colour`].
///
/// [`colour`]: #method.colour
#[cfg(feature="utils")]
#[inline]
pub fn color<C: Into<Colour>>(self, colour: C) -> Self {
self.colour(colour.into())
}
/// Set the colour of the left-hand side of the embed.
#[cfg(feature="utils")]
pub fn colour<C: Into<Colour>>(mut self, colour: C) -> Self {
self.0.insert("color".to_owned(), Value::Number(Number::from(colour.into().0 as u64)));
CreateEmbed(self.0)
}
/// Set the colour of the left-hand side of the embed.
///
/// This is an alias of [`colour`].
///
/// [`colour`]: #method.colour
#[cfg(not(feature="utils"))]
#[inline]
pub fn color(self, colour: u32) -> Self {
self.colour(colour)
}
/// Set the colour of the left-hand side of the embed.
#[cfg(not(feature="utils"))]
pub fn colour(mut self, colour: u32) -> Self {
self.0.insert("color".to_owned(), Value::Number(Number::from(colour)));
CreateEmbed(self.0)
}
/// Set the description of the embed.
///
/// **Note**: This can't be longer than 2048 characters.
pub fn description(mut self, description: &str) -> Self {
self.0.insert("description".to_owned(), Value::String(description.to_owned()));
CreateEmbed(self.0)
}
/// Set a field. Note that this will not overwrite other fields, and will
/// add to them.
///
/// Refer to the documentation for [`CreateEmbedField`] for more
/// information.
///
/// **Note**: Maximum amount of characters you can put is 256 in a field
/// name and 1024 in a field value and a field is inline by default.
///
/// [`CreateEmbedField`]: struct.CreateEmbedField.html
pub fn field<F>(mut self, f: F) -> Self
where F: FnOnce(CreateEmbedField) -> CreateEmbedField {
let field = f(CreateEmbedField::default()).0;
{
let key = "fields".to_owned();
let entry = self.0.remove(&key).unwrap_or_else(|| Value::Array(vec![]));
let mut arr = match entry {
Value::Array(inner) => inner,
_ => {
// The type of `entry` should always be a `Value::Array`.
//
// Theoretically this never happens, but you never know.
//
// In the event that it does, just return the current value.
return CreateEmbed(self.0);
},
};
arr.push(Value::Object(field));
self.0.insert("fields".to_owned(), Value::Array(arr));
}
CreateEmbed(self.0)
}
/// Set the footer of the embed.
///
/// Refer to the documentation for [`CreateEmbedFooter`] for more
/// information.
///
/// [`CreateEmbedFooter`]: struct.CreateEmbedFooter.html
pub fn footer<F>(mut self, f: F) -> Self
where F: FnOnce(CreateEmbedFooter) -> CreateEmbedFooter {
let footer = f(CreateEmbedFooter::default()).0;
self.0.insert("footer".to_owned(), Value::Object(footer));
CreateEmbed(self.0)
}
/// Set the image associated with the embed. This only supports HTTP(S).
pub fn image(mut self, url: &str) -> Self {
let image = json!({
"url": url.to_owned()
});
self.0.insert("image".to_owned(), image);
CreateEmbed(self.0)
}
/// Set the thumbnail of the embed. This only supports HTTP(S).
pub fn thumbnail(mut self, url: &str) -> Self {
let thumbnail = json!({
"url": url.to_owned(),
});
self.0.insert("thumbnail".to_owned(), thumbnail);
CreateEmbed(self.0)
}
/// Set the timestamp.
///
/// **Note**: This timestamp must be in ISO-8601 format. It must also be
/// in UTC format.
///
/// # Examples
///
/// You may pass a direct string:
///
/// - `2017-01-03T23:00:00`
/// - `2004-06-08T16:04:23`
/// - `2004-06-08T16:04:23`
///
/// Or a `time::Tm`:
///
/// ```rust,ignore
/// extern crate time;
///
/// let now = time::now();
///
/// embed = embed.timestamp(now);
/// // ...
/// ```
pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self {
self.0.insert("timestamp".to_owned(), Value::String(timestamp.into().ts));
CreateEmbed(self.0)
}
/// Set the title of the embed.
pub fn title(mut self, title: &str) -> Self {
self.0.insert("title".to_owned(), Value::String(title.to_owned()));
CreateEmbed(self.0)
}
/// Set the URL to direct to when clicking on the title.
pub fn url(mut self, url: &str) -> Self {
self.0.insert("url".to_owned(), Value::String(url.to_owned()));
CreateEmbed(self.0)
}
}
impl Default for CreateEmbed {
/// Creates a builder with default values, setting the `type` to `rich`.
fn default() -> CreateEmbed {
let mut map = Map::new();
map.insert("type".to_owned(), Value::String("rich".to_owned()));
CreateEmbed(map)
}
}
impl From<Embed> for CreateEmbed {
/// Converts the fields of an embed into the values for a new embed builder.
///
/// Some values - such as Proxy URLs - are not preserved.
fn from(embed: Embed) -> CreateEmbed {
let mut b = CreateEmbed::default()
.colour(embed.colour);
if let Some(author) = embed.author {
b = b.author(move |mut a| {
a = a.name(&author.name);
if let Some(icon_url) = author.icon_url {
a = a.icon_url(&icon_url);
}
if let Some(url) = author.url {
a = a.url(&url);
}
a
});
}
if let Some(description) = embed.description {
b = b.description(&description);
}
for field in embed.fields {
b = b.field(move |f| f
.inline(field.inline)
.name(&field.name)
.value(&field.value));
}
if let Some(image) = embed.image {
b = b.image(&image.url);
}
if let Some(timestamp) = embed.timestamp {
b = b.timestamp(timestamp);
}
if let Some(thumbnail) = embed.thumbnail {
b = b.thumbnail(&thumbnail.url);
}
if let Some(url) = embed.url {
b = b.url(&url);
}
if let Some(title) = embed.title {
b = b.title(&title);
}
b
}
}
/// A builder to create a fake [`Embed`] object's author, for use with the
/// [`CreateEmbed::author`] method.
///
/// Requires that you specify a [`name`].
///
/// [`Embed`]: ../model/struct.Embed.html
/// [`CreateEmbed::author`]: struct.CreateEmbed.html#method.author
/// [`name`]: #method.name
#[derive(Clone, Debug, Default)]
pub struct CreateEmbedAuthor(pub Map<String, Value>);
impl CreateEmbedAuthor {
/// Set the URL of the author's icon.
pub fn icon_url(mut self, icon_url: &str) -> Self {
self.0.insert("icon_url".to_owned(), Value::String(icon_url.to_owned()));
self
}
/// Set the author's name.
pub fn name(mut self, name: &str) -> Self {
self.0.insert("name".to_owned(), Value::String(name.to_owned()));
self
}
/// Set the author's URL.
pub fn url(mut self, url: &str) -> Self {
self.0.insert("url".to_owned(), Value::String(url.to_owned()));
self
}
}
/// A builder to create a fake [`Embed`] object's field, for use with the
/// [`CreateEmbed::field`] method.
///
/// This does not require any field be set. `inline` is set to `true` by
/// default.
///
/// [`Embed`]: ../model/struct.Embed.html
/// [`CreateEmbed::field`]: struct.CreateEmbed.html#method.field
#[derive(Clone, Debug)]
pub struct CreateEmbedField(pub Map<String, Value>);
impl CreateEmbedField {
/// Set whether the field is inlined. Set to true by default.
pub fn inline(mut self, inline: bool) -> Self {
self.0.insert("inline".to_owned(), Value::Bool(inline));
self
}
/// Set the field's name. It can't be longer than 256 characters.
pub fn name(mut self, name: &str) -> Self {
self.0.insert("name".to_owned(), Value::String(name.to_owned()));
self
}
/// Set the field's value. It can't be longer than 1024 characters.
pub fn value(mut self, value: &str) -> Self {
self.0.insert("value".to_owned(), Value::String(value.to_owned()));
self
}
}
impl Default for CreateEmbedField {
/// Creates a builder with default values, setting the value of `inline` to
/// `true`.
fn default() -> CreateEmbedField {
let mut map = Map::new();
map.insert("inline".to_owned(), Value::Bool(true));
CreateEmbedField(map)
}
}
/// A builder to create a fake [`Embed`] object's footer, for use with the
/// [`CreateEmbed::footer`] method.
///
/// This does not require any field be set.
///
/// [`Embed`]: ../model/struct.Embed.html
/// [`CreateEmbed::footer`]: struct.CreateEmbed.html#method.footer
#[derive(Clone, Debug, Default)]
pub struct CreateEmbedFooter(pub Map<String, Value>);
impl CreateEmbedFooter {
/// Set the icon URL's value. This only supports HTTP(S).
pub fn icon_url(mut self, icon_url: &str) -> Self {
self.0.insert("icon_url".to_owned(), Value::String(icon_url.to_owned()));
self
}
/// Set the footer's text.
pub fn text(mut self, text: &str) -> Self {
self.0.insert("text".to_owned(), Value::String(text.to_owned()));
self
}
}
#[derive(Clone, Debug)]
pub struct Timestamp {
pub ts: String,
}
impl From<String> for Timestamp {
fn from(ts: String) -> Self {
Timestamp {
ts: ts,
}
}
}
impl From<Tm> for Timestamp {
fn from(tm: Tm) -> Self {
Timestamp {
ts: tm.to_utc().rfc3339().to_string(),
}
}
}