Skip to main content

zaun_bot/
embed.rs

1//! [`EmbedBuilder`].
2
3use crate::types::{Embed, EmbedAuthor, EmbedField, EmbedFooter, Id};
4
5/// Builds an [`Embed`] (§8.5). Pictures are attachment ids of the same message.
6#[derive(Debug, Clone)]
7pub struct EmbedBuilder(Embed);
8
9impl Default for EmbedBuilder {
10    fn default() -> Self {
11        Self(Embed {
12            title: None,
13            description: None,
14            url: None,
15            color: None,
16            timestamp: None,
17            author: None,
18            footer: None,
19            image: None,
20            thumbnail: None,
21            fields: Vec::new(),
22        })
23    }
24}
25
26impl EmbedBuilder {
27    /// An empty embed; give it at least a title, description, field, author or footer.
28    #[must_use]
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    /// Title (≤ 256).
34    #[must_use]
35    pub fn title(mut self, t: impl Into<String>) -> Self {
36        self.0.title = Some(t.into());
37        self
38    }
39
40    /// Description (≤ 4 096).
41    #[must_use]
42    pub fn description(mut self, d: impl Into<String>) -> Self {
43        self.0.description = Some(d.into());
44        self
45    }
46
47    /// The title's `https://` link.
48    #[must_use]
49    pub fn url(mut self, url: impl Into<String>) -> Self {
50        self.0.url = Some(url.into());
51        self
52    }
53
54    /// Side colour, `0xRRGGBB`.
55    #[must_use]
56    pub fn color(mut self, rgb: u32) -> Self {
57        self.0.color = Some(rgb);
58        self
59    }
60
61    /// Timestamp, ms since the epoch.
62    #[must_use]
63    pub fn timestamp(mut self, ms: u64) -> Self {
64        self.0.timestamp = Some(ms);
65        self
66    }
67
68    /// Author line.
69    #[must_use]
70    pub fn author(mut self, name: impl Into<String>) -> Self {
71        self.0.author = Some(EmbedAuthor {
72            name: name.into(),
73            url: None,
74            icon: None,
75        });
76        self
77    }
78
79    /// Footer text.
80    #[must_use]
81    pub fn footer(mut self, text: impl Into<String>) -> Self {
82        self.0.footer = Some(EmbedFooter {
83            text: text.into(),
84            icon: None,
85        });
86        self
87    }
88
89    /// Large picture: an image attachment of the same message.
90    #[must_use]
91    pub fn image(mut self, attachment: impl Into<Id>) -> Self {
92        self.0.image = Some(attachment.into());
93        self
94    }
95
96    /// Small picture: an image attachment of the same message.
97    #[must_use]
98    pub fn thumbnail(mut self, attachment: impl Into<Id>) -> Self {
99        self.0.thumbnail = Some(attachment.into());
100        self
101    }
102
103    /// A field (≤ 25).
104    #[must_use]
105    pub fn field(
106        mut self,
107        name: impl Into<String>,
108        value: impl Into<String>,
109        inline: bool,
110    ) -> Self {
111        let field = EmbedField {
112            name: name.into(),
113            value: value.into(),
114            inline,
115        };
116        self.0.fields.push(field);
117        self
118    }
119
120    /// The finished embed.
121    #[must_use]
122    pub fn build(self) -> Embed {
123        self.0
124    }
125}
126
127impl From<EmbedBuilder> for Embed {
128    fn from(b: EmbedBuilder) -> Self {
129        b.build()
130    }
131}