spectacles_model/message/
embed.rs

1use chrono::{DateTime, Utc};
2
3/// Represents a Message Embed being sent.
4#[derive(Serialize, Deserialize, Clone, Debug, Default)]
5pub struct Embed {
6    /// The title of the embed.
7    #[serde(default, skip_serializing_if = "Option::is_none")]
8    pub title: Option<String>,
9    /// The type of embed.
10    #[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
11    pub kind: Option<String>,
12    /// The description of the embed.
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub description: Option<String>,
15    /// The URL of the embed.
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub url: Option<String>,
18    /// The timestamp of the embed.
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub timestamp: Option<DateTime<Utc>>,
21    /// The color of the embed.
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub color: Option<i32>,
24    /// Information about the embed's footer.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub footer: Option<EmbedFooter>,
27    /// Information about the embed's image.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub image: Option<EmbedImage>,
30    /// Information about the embed's thumbnail.
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    pub thumbnail: Option<EmbedThumbnail>,
33    /// Information about an embed's video, if applicable.
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub video: Option<EmbedVideo>,
36    /// Information about an embed's provider if applicable.
37    #[serde(default)]
38    pub provider: Option<EmbedProvider>,
39    /// Information about the embed's author.
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub author: Option<EmbedAuthor>,
42    /// Information about the embed's fields.
43    #[serde(default)]
44    pub fields: Vec<EmbedField>
45}
46
47impl Embed {
48    pub fn new() -> Self {
49        Embed::default()
50    }
51
52    /// Sets the title for this embed.
53    pub fn set_title(mut self, text: impl Into<String>) -> Self {
54        self.title = Some(text.into());
55        self
56    }
57
58    /// Sets the description of this embed.
59    pub fn set_description(mut self, text: impl Into<String>) -> Self {
60        self.description = Some(text.into());
61        self
62    }
63
64    /// Sets the color of this embed.
65    pub fn set_color(mut self, code: i32) -> Self {
66        self.color = Some(code);
67        self
68    }
69
70    /// Adds a field to this embed.
71    pub fn add_field<F: FnOnce(EmbedField) -> EmbedField>(mut self, builder: F) -> Self {
72        let new_field = builder(EmbedField::default());
73        self.fields.push(new_field);
74        self
75    }
76
77    /// Sets the author of this embed.
78    pub fn set_author<F>(mut self, author: F) -> Self
79        where F: FnOnce(EmbedAuthor) -> EmbedAuthor
80    {
81        self.author = Some(author(EmbedAuthor::default()));
82        self
83    }
84
85    /// Sets the footer of this embed.
86    pub fn set_footer<F>(mut self, footer: F) -> Self
87        where F: FnOnce(EmbedFooter) -> EmbedFooter
88    {
89        self.footer = Some(footer(EmbedFooter::default()));
90        self
91    }
92
93    /// Adds a thumbnail to this embed.
94    pub fn set_thumbnail<T>(mut self, thumb: T) -> Self
95        where T: FnOnce(EmbedThumbnail) -> EmbedThumbnail
96    {
97        self.thumbnail = Some(thumb(EmbedThumbnail::default()));
98        self
99    }
100
101    /// Adds an image to this embed.
102    pub fn set_image<F>(mut self, img: F) -> Self
103        where F: FnOnce(EmbedImage) -> EmbedImage
104    {
105        self.image = Some(img(EmbedImage::default()));
106        self
107    }
108
109    pub fn set_current_timestamp(mut self) -> Self {
110        self.timestamp = Some(chrono::Utc::now());
111
112        self
113    }
114
115    pub fn set_timestamp(mut self, time: DateTime<Utc>) -> Self {
116        self.timestamp = Some(time);
117
118        self
119    }
120
121
122}
123/// An Embed Footer data object.
124#[derive(Serialize, Deserialize, Clone, Debug, Default)]
125pub struct EmbedFooter {
126    /// The text of this footer.
127    pub text: String,
128    /// The Icon URL of this footer.
129    #[serde(default)]
130    pub icon_url: String,
131    /// The proxied URL of the icon.
132    #[serde(default)]
133    pub proxy_icon_url: String
134
135}
136
137impl EmbedFooter {
138    /// Sets the text for this footer.
139    pub fn set_text(mut self, txt: impl Into<String>) -> Self {
140        self.text = txt.into();
141        self
142    }
143
144    /// Set the icon URL for this footer.
145    pub fn set_icon_url(mut self, url: impl Into<String>) -> Self {
146        self.icon_url = url.into();
147        self
148    }
149}
150/// An Embed Image data object.
151#[derive(Serialize, Deserialize, Clone, Debug, Default)]
152pub struct EmbedImage {
153    /// The source URL of the image.
154    #[serde(default)]
155    pub url: Option<String>,
156    /// A proxied URL of the image.
157    #[serde(default)]
158    pub proxy_url: Option<String>,
159    /// The height of the image.
160    #[serde(default)]
161    pub height: Option<i32>,
162    /// The width of the image.
163    #[serde(default)]
164    pub width: Option<i32>
165}
166
167impl EmbedImage {
168    /// Set the URL for this image.
169    pub fn set_url(mut self, url: impl Into<String>) -> Self {
170        self.url = Some(url.into());
171        self
172    }
173}
174
175/// An Embed Thumbnail data object.
176#[derive(Serialize, Deserialize, Clone, Debug, Default)]
177pub struct EmbedThumbnail {
178    /// The source URL of the thumbnail.
179    #[serde(default)]
180    pub url: Option<String>,
181    #[serde(default)]
182    /// A proxied URL of the thumbnail.
183    pub proxy_url: Option<String>,
184    /// The height of the thumbnail.
185    #[serde(default)]
186    pub height: Option<i32>,
187    /// The width of the thumbnail.
188    #[serde(default)]
189    pub width: Option<i32>
190}
191
192impl EmbedThumbnail {
193    /// Set the URL of this thumbnail.
194    pub fn set_url(mut self, url: impl Into<String>) -> Self {
195        self.url = Some(url.into());
196        self
197    }
198}
199/// An Embed Video data object.
200#[derive(Serialize, Deserialize, Clone, Debug, Default)]
201pub struct EmbedVideo {
202    /// The source URL of the video.
203    #[serde(default)]
204    pub url: Option<String>,
205    /// The height of the video.
206    #[serde(default)]
207    pub height: Option<i32>,
208    /// The width of the thumbnail.
209    #[serde(default)]
210    pub width: Option<i32>
211}
212
213/// Information about the embed's provider.
214#[derive(Serialize, Deserialize, Clone, Debug, Default)]
215pub struct EmbedProvider {
216    /// The name of the provider.
217    #[serde(default)]
218    pub name: Option<String>,
219    /// The url of the provider.
220    #[serde(default)]
221    pub url: Option<String>
222}
223
224/// Information about the embed's author.
225#[derive(Serialize, Deserialize, Clone, Debug, Default)]
226pub struct EmbedAuthor {
227    /// The name of the author.
228    #[serde(default)]
229    pub name: Option<String>,
230    /// The URL of the author.
231    #[serde(default)]
232    pub url: Option<String>,
233    /// The URL of the author's icon.
234    #[serde(default)]
235    pub icon_url: Option<String>,
236    /// A proxied version of the author's icon.
237    #[serde(default)]
238    pub proxy_icon_url: Option<String>
239}
240
241impl EmbedAuthor {
242    /// Set the name of the author.
243    pub fn set_name(mut self, name: impl Into<String>) -> Self {
244        self.name = Some(name.into());
245        self
246    }
247
248    /// Set the URL for this author.
249    pub fn set_url(mut self, url: impl Into<String>) -> Self {
250        self.url = Some(url.into());
251        self
252    }
253
254    /// Set the author's icon URL.
255    pub fn set_icon_url(mut self, url: impl Into<String>) -> Self {
256        self.icon_url = Some(url.into());
257        self
258    }
259}
260
261/// Represents an Embed Field object.
262#[derive(Serialize, Deserialize, Clone, Debug, Default)]
263pub struct EmbedField {
264    /// The name of the field.
265    pub name: String,
266    /// The value of the field.
267    pub value: String,
268    /// Whether or not this field should display as inline.
269    #[serde(default)]
270    pub inline: Option<bool>
271}
272
273impl EmbedField {
274    /// Sets the name of this field.
275    pub fn set_name(mut self, name: impl Into<String>) -> Self {
276        self.name = name.into();
277        self
278    }
279
280    pub fn set_value(mut self, val: impl Into<String>) -> Self {
281        self.value = val.into();
282        self
283    }
284
285    /// Sets the inline flag of this field.
286    pub fn set_inline(mut self, inline: bool) -> Self {
287        self.inline = Some(inline);
288
289        self
290    }
291}