serenity_utils 0.7.0

A library to provide additional utilies for Discord bots created with serenity.
Documentation
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Provides alternatives to serenity's embed builders.
//!
//! Unlike serenity's builders, the builders here use separate fields for all
//! values instead of a [`HashMap`]. This provides an easy way to access the
//! builder's fields.
//!
//! Due to the user-friendliness of these builders, they are slightly less
//! efficient than serenity's builders. You should only use these when you need
//! access to the embed's values which are set somewhere else.
//!
//! All builders provide trait implementations to convert them into serenity's
//! builders.
//!
//! ## Example
//!
//! ```
//! # use serenity_utils::builder::embed::EmbedBuilder;
//! #
//! let mut embed = EmbedBuilder::new();
//!
//! // Fields can set using setters.
//! embed.set_description("description").set_title("title");
//!
//! // Or by directly mutating the struct.
//! embed.description = Some("description".to_string());
//! embed.title = Some("title".to_string());
//! ```
//!
//! Other builders can be used in a similar fashion.
//!
//! [`HashMap`]: std::collections::HashMap

use serenity::builder::{CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter};
use serenity::model::channel::EmbedField;
use serenity::model::Timestamp;
use serenity::utils::Colour;

/// A struct to build the author portion of an embed.
///
/// It is meant to serve as an alternative to serenity's [`CreateEmbedAuthor`].
/// Unlike serenity's builder, this builder uses separate fields for all values
/// instead of a [`HashMap`]. This provides an easy way to access the builder's
/// fields.
///
/// All fields have setter methods like serenity's builder to allow you to pass
/// in a wide range of parameters/arguments.
///
/// The `name` field cannot be empty. All other fields are optional.
///
/// ## Example
///
/// ```
/// # use serenity_utils::builder::embed::EmbedAuthorBuilder;
/// #
/// let mut author = EmbedAuthorBuilder::new("name");
///
/// // Fields can set using the setter.
/// author.set_icon_url("icon_url");
///
/// // Or by directly mutating the struct.
/// author.icon_url = Some("icon_url".to_string());
/// ```
///
/// [`HashMap`]: std::collections::HashMap
#[derive(Clone, Debug)]
pub struct EmbedAuthorBuilder {
    /// The icon URL of the author. This only supports HTTP(S).
    pub icon_url: Option<String>,
    /// The name of the author.
    pub name: String,
    /// The URL of the author.
    pub url: Option<String>,
}

impl EmbedAuthorBuilder {
    /// Creates a new [`EmbedAuthorBuilder`] object.
    ///
    /// `name` must be specified when creating as it cannot be empty. Other
    /// fields are optional and be specified by directly mutating the struct
    /// or using one of the setters.
    pub fn new<S: ToString>(name: S) -> Self {
        Self {
            icon_url: None,
            name: name.to_string(),
            url: None,
        }
    }

    /// Sets the author's icon URL. This only supports HTTP(S).
    pub fn set_icon_url<S: ToString>(&mut self, icon_url: S) -> &mut Self {
        self.icon_url = Some(icon_url.to_string());

        self
    }

    /// Sets the author's name.
    pub fn set_name<S: ToString>(&mut self, name: S) -> &mut Self {
        self.name = name.to_string();

        self
    }

    /// Sets the author's URL.
    pub fn set_url<S: ToString>(&mut self, url: S) -> &mut Self {
        self.url = Some(url.to_string());

        self
    }

    /// Converts [`EmbedAuthorBuilder`] into serenity's [`CreateEmbedAuthor`].
    pub fn to_create_embed_author(&self) -> CreateEmbedAuthor {
        self.into()
    }
}

impl From<EmbedAuthorBuilder> for CreateEmbedAuthor {
    fn from(author_builder: EmbedAuthorBuilder) -> Self {
        let mut author = CreateEmbedAuthor::default();

        author.name(author_builder.name);

        if let Some(value) = author_builder.icon_url {
            author.icon_url(value);
        }

        if let Some(value) = author_builder.url {
            author.url(value);
        }

        author
    }
}

impl From<&EmbedAuthorBuilder> for CreateEmbedAuthor {
    fn from(author_builder: &EmbedAuthorBuilder) -> Self {
        let mut author = CreateEmbedAuthor::default();

        author.name(&author_builder.name);

        if let Some(value) = &author_builder.icon_url {
            author.icon_url(value);
        }

        if let Some(value) = &author_builder.url {
            author.url(value);
        }

        author
    }
}

/// A struct to build the footer portion of an embed.
///
/// It is meant to serve as an alternative to serenity's [`CreateEmbedFooter`].
/// Unlike serenity's builder, this builder uses separate fields for all values
/// instead of a [`HashMap`]. This provides an easy way to access the builder's
/// fields.
///
/// All fields have setter methods like serenity's builder to allow you to pass
/// in a wide range of parameters/arguments.
///
/// The `text` field cannot be empty. All other fields are optional.
///
/// ## Example
///
/// ```
/// # use serenity_utils::builder::embed::EmbedFooterBuilder;
/// #
/// let mut footer = EmbedFooterBuilder::new("text");
///
/// // Fields can set using the setter.
/// footer.set_icon_url("icon_url");
///
/// // Or by directly mutating the struct.
/// footer.icon_url = Some("icon_url".to_string());
/// ```
///
/// [`HashMap`]: std::collections::HashMap
#[derive(Clone, Debug)]
pub struct EmbedFooterBuilder {
    /// The icon url of the footer. This only supports HTTP(S).
    pub icon_url: Option<String>,
    /// The text of the footer.
    pub text: String,
}

impl EmbedFooterBuilder {
    /// Creates a new [`EmbedFooterBuilder`] object.
    ///
    /// `text` must be specified when creating as it cannot be empty. Other
    /// fields are optional and be specified by directly mutating the struct
    /// or using one of the setters.
    pub fn new<S: ToString>(text: S) -> Self {
        Self {
            icon_url: None,
            text: text.to_string(),
        }
    }

    /// Sets the footer's icon url. This only supports HTTP(S).
    pub fn set_icon_url<S: ToString>(&mut self, icon_url: S) -> &mut Self {
        self.icon_url = Some(icon_url.to_string());

        self
    }

    // Sets the footer's text.
    pub fn set_text<S: ToString>(&mut self, text: S) -> &mut Self {
        self.text = text.to_string();

        self
    }

    /// Converts [`EmbedFooterBuilder`] into serenity's [`CreateEmbedFooter`].
    pub fn to_create_embed_footer(&self) -> CreateEmbedFooter {
        self.into()
    }
}

impl From<EmbedFooterBuilder> for CreateEmbedFooter {
    fn from(footer_builder: EmbedFooterBuilder) -> Self {
        let mut footer = CreateEmbedFooter::default();

        footer.text(footer_builder.text);

        if let Some(value) = footer_builder.icon_url {
            footer.icon_url(value);
        }

        footer
    }
}

impl From<&EmbedFooterBuilder> for CreateEmbedFooter {
    fn from(footer_builder: &EmbedFooterBuilder) -> Self {
        let mut footer = CreateEmbedFooter::default();

        footer.text(&footer_builder.text);

        if let Some(value) = &footer_builder.icon_url {
            footer.icon_url(value);
        }

        footer
    }
}

/// A struct to build an embed field.
///
/// All fields have setter methods like serenity's builders to allow you to pass
/// in a wide range of parameters/arguments.
///
/// None of the fields are optional.
///
/// ## Example
///
/// ```
/// # use serenity_utils::builder::embed::EmbedFieldBuilder;
/// #
/// let mut field = EmbedFieldBuilder::new("name", "value", true);
///
/// # let inline = true;
/// // Fields can set using the setter.
/// field.set_inline(inline);
///
/// // Or by directly mutating the struct.
/// field.inline = inline;
/// ```
#[derive(Clone, Debug)]
pub struct EmbedFieldBuilder {
    /// Indicator of whether the field should display as inline.
    pub inline: bool,
    /// The name of the field.
    ///
    /// The maxiumum length of this field is 512 unicode codepoints.
    pub name: String,
    /// The value of the field.
    ///
    /// The maxiumum length of this field is 1024 unicode codepoints.
    pub value: String,
}

impl EmbedFieldBuilder {
    /// Creates a new [`EmbedFieldBuilder`] object.
    pub fn new<T: ToString, U: ToString>(name: T, value: U, inline: bool) -> Self {
        Self {
            name: name.to_string(),
            value: value.to_string(),
            inline,
        }
    }

    /// Sets the field's name.
    pub fn set_name<S: ToString>(&mut self, name: S) -> &mut Self {
        self.name = name.to_string();

        self
    }

    /// Sets the field's value.
    pub fn set_value<S: ToString>(&mut self, value: S) -> &mut Self {
        self.value = value.to_string();

        self
    }

    /// Sets the field's inline field.
    pub fn set_inline(&mut self, inline: bool) -> &mut Self {
        self.inline = inline;

        self
    }

    /// Adds the field to the given `CreateEmbed`.
    ///
    /// See [`EmbedBuilder::add_field`] method to add a field to [`EmbedBuilder`].
    ///
    /// [`EmbedBuilder::add_field`]: EmbedBuilder::add_field()
    pub fn insert_to(self, embed: &mut CreateEmbed) -> &mut CreateEmbed {
        embed.field(self.name, self.value, self.inline)
    }
}

impl From<EmbedFieldBuilder> for EmbedField {
    fn from(field: EmbedFieldBuilder) -> Self {
        EmbedField::new(field.name, field.value, field.inline)
    }
}

impl From<&EmbedFieldBuilder> for EmbedField {
    fn from(field: &EmbedFieldBuilder) -> Self {
        EmbedField::new(&field.name, &field.value, field.inline)
    }
}

/// A struct to build an embed.
///
/// It is meant to serve as an alternative to serenity's [`CreateEmbed`].
/// Unlike serenity's builder, this builder uses separate fields for all values
/// instead of a [`HashMap`]. This provides an easy way to access the builder's
/// fields.
///
/// All fields have setter methods like serenity's builder to allow you to pass
/// in a wide range of parameters/arguments.
///
/// ## Example
///
/// ```
/// # use serenity_utils::builder::embed::EmbedBuilder;
/// #
/// let mut embed = EmbedBuilder::new();
///
/// // Fields can set using setters.
/// embed.set_description("description").set_title("title");
///
/// // Or by directly mutating the struct.
/// embed.description = Some("description".to_string());
/// embed.title = Some("title".to_string());
/// ```
///
/// [`HashMap`]: std::collections::HashMap
#[derive(Clone, Debug, Default)]
pub struct EmbedBuilder {
    /// The author of the embed.
    pub author: Option<EmbedAuthorBuilder>,
    /// The colour of the embed.
    pub colour: Option<Colour>,
    /// The description of the embed.
    ///
    /// It can't be longer than 2048 characters.
    pub description: Option<String>,
    /// The fields of the embed.
    pub fields: Vec<EmbedFieldBuilder>,
    /// The footer of the embed.
    pub footer: Option<EmbedFooterBuilder>,
    /// The image of the embed.
    ///
    /// This only supports HTTP(S).
    pub image: Option<String>,
    /// The thumbnail of the embed.
    ///
    /// This only supports HTTP(S).
    pub thumbnail: Option<String>,
    /// The timestamp of the embed.
    pub timestamp: Option<Timestamp>,
    /// The title of the embed.
    pub title: Option<String>,
    /// The title url of the embed.
    pub url: Option<String>,
    /// The attachment of the embed.
    pub attachment: Option<String>,
}

impl EmbedBuilder {
    /// Creates an empty [`EmbedBuilder`] object.
    ///
    /// All fields are set to [`None`] or empty vectors. They can be changed by
    /// mutating the struct directly or by using the setter methods.
    pub fn new() -> Self {
        Self::default()
    }

    /// Same as calling [`set_image`] with "attachment://filename.(jpg, png)".
    ///
    /// Note however, you have to be sure you set an attachment (with serenity's
    /// `ChannelId::send_files`) with the provided filename or else this won't
    /// work.
    ///
    /// [`set_image`]: EmbedBuilder::set_image()
    pub fn set_attachment<S: ToString>(&mut self, filename: S) -> &mut Self {
        let mut filename = filename.to_string();
        filename.insert_str(0, "attachment://");

        self.attachment = Some(filename);

        self
    }

    /// Sets the embed's author.
    pub fn set_author(&mut self, author: EmbedAuthorBuilder) -> &mut Self {
        self.author = Some(author);

        self
    }

    /// Sets the embed's author using the specified closure.
    ///
    /// It allows you to set the author without unnecessarily importing
    /// [`EmbedAuthorBuilder`]. Note that it sets the `name` field of the
    /// author to "name", so you need to, at least, update that in the closure.
    ///
    /// ## Example
    ///
    /// ```
    /// # use serenity_utils::builder::embed::EmbedBuilder;
    /// #
    /// let mut embed = EmbedBuilder::new();
    /// embed.set_author_with(|a| {
    ///     a.set_name("name");
    ///     a.set_url("url");
    ///
    ///     a
    /// });
    /// ```
    pub fn set_author_with<F>(&mut self, f: F) -> &mut Self
    where
        F: FnOnce(&mut EmbedAuthorBuilder) -> &mut EmbedAuthorBuilder,
    {
        let mut author = EmbedAuthorBuilder::new("name");
        f(&mut author);

        self.set_author(author)
    }

    /// Sets the embed's colour.
    pub fn set_colour<C: Into<Colour>>(&mut self, colour: C) -> &mut Self {
        self.colour = Some(colour.into());

        self
    }

    /// Sets the embed's description.
    ///
    /// It can't be longer than 2048 characters.
    pub fn set_description<S: ToString>(&mut self, description: S) -> &mut Self {
        self.description = Some(description.to_string());

        self
    }

    /// Adds a field to the embed.
    ///
    /// The name of a field can contain 256 characters at most. The value can
    /// contain 1024 characters at most.
    pub fn add_field<T, U>(&mut self, field: (T, U, bool)) -> &mut Self
    where
        T: ToString,
        U: ToString,
    {
        self.fields.push(EmbedFieldBuilder::new(field.0, field.1, field.2));

        self
    }

    /// Adds multiple fields to the embed.
    ///
    /// The name of a field can contain 256 characters at most. The value can
    /// contain 1024 characters at most.
    pub fn add_fields<T, U, It>(&mut self, fields: It) -> &mut Self
    where
        It: IntoIterator<Item = (T, U, bool)>,
        T: ToString,
        U: ToString,
    {
        let mut fields =
            fields.into_iter().map(|(n, v, b)| EmbedFieldBuilder::new(n, v, b)).collect::<Vec<_>>();

        self.fields.append(&mut fields);

        self
    }

    /// Sets field at position `index`, if it is within bounds.
    pub fn set_field_at(&mut self, index: usize, field: EmbedFieldBuilder) -> &mut Self {
        if self.fields.len() - 1 > index {
            self.fields[index] = field;
        }

        self
    }

    /// Sets the embed's footer.
    pub fn set_footer(&mut self, footer: EmbedFooterBuilder) -> &mut Self {
        self.footer = Some(footer);

        self
    }

    /// Sets the embed's footer using the specified closure.
    ///
    /// It allows you to set the footer without unnecessarily importing
    /// [`EmbedFooterBuilder`]. Note that it sets the `text` field of the
    /// footer to "text", so you need to, at least, update that in the closure.
    ///
    /// ## Example
    ///
    /// ```
    /// # use serenity_utils::builder::embed::EmbedBuilder;
    /// #
    /// let mut embed = EmbedBuilder::new();
    /// embed.set_footer_with(|f| {
    ///     f.set_text("text");
    ///
    ///     f
    /// });
    /// ```
    pub fn set_footer_with<F>(&mut self, f: F) -> &mut Self
    where
        F: FnOnce(&mut EmbedFooterBuilder) -> &mut EmbedFooterBuilder,
    {
        let mut footer = EmbedFooterBuilder::new("text");
        f(&mut footer);

        self.set_footer(footer)
    }

    /// Sets the embed's image. This only supports HTTP(S).
    pub fn set_image<S: ToString>(&mut self, url: S) -> &mut Self {
        self.image = Some(url.to_string());

        self
    }

    /// Sets the embed's thumbnail. This only supports HTTP(S).
    pub fn set_thumbnail<S: ToString>(&mut self, url: S) -> &mut Self {
        self.thumbnail = Some(url.to_string());

        self
    }

    /// Sets the embed's timestamp.
    pub fn set_timestamp<T: Into<Timestamp>>(&mut self, timestamp: T) -> &mut Self {
        self.timestamp = Some(timestamp.into());

        self
    }

    /// Sets the embed's title.
    pub fn set_title<S: ToString>(&mut self, title: S) -> &mut Self {
        self.title = Some(title.to_string());

        self
    }

    /// Sets the embed's title url.
    pub fn set_url<S: ToString>(&mut self, url: S) -> &mut Self {
        self.url = Some(url.to_string());

        self
    }

    /// Converts [`EmbedBuilder`] into serenity's [`CreateEmbed`].
    pub fn to_create_embed(&self) -> CreateEmbed {
        self.into()
    }
}

impl From<EmbedBuilder> for CreateEmbed {
    fn from(embed_builder: EmbedBuilder) -> Self {
        let mut embed = CreateEmbed::default();

        if let Some(author) = embed_builder.author {
            embed.author(|f| {
                f.0 = author.to_create_embed_author().0;

                f
            });
        }

        if let Some(colour) = embed_builder.colour {
            embed.colour(colour);
        }

        if let Some(description) = embed_builder.description {
            embed.description(description);
        }

        for field in embed_builder.fields {
            embed.field(field.name, field.value, field.inline);
        }

        if let Some(footer) = embed_builder.footer {
            embed.footer(|f| {
                f.0 = footer.to_create_embed_footer().0;

                f
            });
        }

        if let Some(image) = embed_builder.image {
            embed.image(image);
        }

        if let Some(thumbnail) = embed_builder.thumbnail {
            embed.thumbnail(thumbnail);
        }

        if let Some(timestamp) = embed_builder.timestamp {
            embed.timestamp(timestamp);
        }

        if let Some(title) = embed_builder.title {
            embed.title(title);
        }

        if let Some(url) = embed_builder.url {
            embed.url(url);
        }

        if let Some(attachment) = embed_builder.attachment {
            embed.attachment(attachment);
        }

        embed
    }
}

impl From<&EmbedBuilder> for CreateEmbed {
    fn from(embed_builder: &EmbedBuilder) -> Self {
        let mut embed = CreateEmbed::default();

        if let Some(author) = &embed_builder.author {
            embed.author(|f| {
                f.0 = author.to_create_embed_author().0;

                f
            });
        }

        if let Some(colour) = embed_builder.colour {
            embed.colour(colour);
        }

        if let Some(description) = &embed_builder.description {
            embed.description(description);
        }

        for field in &embed_builder.fields {
            embed.field(&field.name, &field.value, field.inline);
        }

        if let Some(footer) = &embed_builder.footer {
            embed.footer(|f| {
                f.0 = footer.to_create_embed_footer().0;

                f
            });
        }

        if let Some(image) = &embed_builder.image {
            embed.image(image);
        }

        if let Some(thumbnail) = &embed_builder.thumbnail {
            embed.thumbnail(thumbnail);
        }

        if let Some(timestamp) = embed_builder.timestamp {
            embed.timestamp(timestamp);
        }

        if let Some(title) = &embed_builder.title {
            embed.title(title);
        }

        if let Some(url) = &embed_builder.url {
            embed.url(url);
        }

        if let Some(attachment) = &embed_builder.attachment {
            embed.attachment(attachment);
        }

        embed
    }
}