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
//! 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`]: crate::model::id::ChannelId::send_message
//! [`ExecuteWebhook::embeds`]: crate::builder::ExecuteWebhook::embeds
//! [here]: https://discord.com/developers/docs/resources/channel#embed-object
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
/// A builder to create an embed in a message
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object)
#[derive(Clone, Debug, Serialize, PartialEq)]
#[must_use]
pub struct CreateEmbed(Embed);
impl CreateEmbed {
/// Equivalent to [`Self::default`].
pub fn new() -> Self {
Self::default()
}
/// Set the author of the embed.
///
/// Refer to the documentation for [`CreateEmbedAuthor`] for more information.
pub fn author(mut self, author: CreateEmbedAuthor) -> Self {
self.0.author = Some(author.0);
self
}
/// Set the colour of the left-hand side of the embed.
///
/// This is an alias of [`Self::colour`].
#[inline]
pub fn color<C: Into<Colour>>(self, colour: C) -> Self {
self.colour(colour)
}
/// Set the colour of the left-hand side of the embed.
#[inline]
pub fn colour<C: Into<Colour>>(mut self, colour: C) -> Self {
self.0.colour = Some(colour.into());
self
}
/// Set the description of the embed.
///
/// **Note**: This can't be longer than 4096 characters.
#[inline]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.0.description = Some(description.into());
self
}
/// Set a field. Note that this will not overwrite other fields, and will add to them.
///
/// **Note**: Maximum amount of characters you can put is 256 in a field name and 1024 in a
/// field value.
#[inline]
pub fn field(
mut self,
name: impl Into<String>,
value: impl Into<String>,
inline: bool,
) -> Self {
self.0.fields.push(EmbedField::new(name, value, inline));
self
}
/// Adds multiple fields at once.
///
/// This is sugar to reduce the need of calling [`Self::field`] manually multiple times.
pub fn fields<N, V>(mut self, fields: impl IntoIterator<Item = (N, V, bool)>) -> Self
where
N: Into<String>,
V: Into<String>,
{
let fields =
fields.into_iter().map(|(name, value, inline)| EmbedField::new(name, value, inline));
self.0.fields.extend(fields);
self
}
/// Set the footer of the embed.
///
/// Refer to the documentation for [`CreateEmbedFooter`] for more information.
pub fn footer(mut self, footer: CreateEmbedFooter) -> Self {
self.0.footer = Some(footer.0);
self
}
/// Set the image associated with the embed.
///
/// Refer [Discord Documentation](https://discord.com/developers/docs/reference#uploading-files)
/// for rules on naming local attachments.
#[inline]
pub fn image(mut self, url: impl Into<String>) -> Self {
self.0.image = Some(EmbedImage {
url: url.into(),
proxy_url: None,
height: None,
width: None,
});
self
}
/// Set the thumbnail of the embed.
#[inline]
pub fn thumbnail(mut self, url: impl Into<String>) -> Self {
self.0.thumbnail = Some(EmbedThumbnail {
url: url.into(),
proxy_url: None,
height: None,
width: None,
});
self
}
/// Set the timestamp.
///
/// See the documentation of [`Timestamp`] for more information.
///
/// # Examples
///
/// Passing a string timestamp:
///
/// ```rust
/// # use serenity::builder::CreateEmbed;
/// # use serenity::model::Timestamp;
/// let timestamp: Timestamp = "2004-06-08T16:04:23Z".parse().expect("Invalid timestamp!");
/// let embed = CreateEmbed::new().title("hello").timestamp(timestamp);
/// ```
#[inline]
pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self {
self.0.timestamp = Some(timestamp.into());
self
}
/// Set the title of the embed.
#[inline]
pub fn title(mut self, title: impl Into<String>) -> Self {
self.0.title = Some(title.into());
self
}
/// Set the URL to direct to when clicking on the title.
#[inline]
pub fn url(mut self, url: impl Into<String>) -> Self {
self.0.url = Some(url.into());
self
}
/// Same as calling [`Self::image`] with "attachment://filename.(jpg, png)".
///
/// Note however, you have to be sure you set an attachment (with [`ChannelId::send_files`])
/// with the provided filename. Or else this won't work.
///
/// Refer [`Self::image`] for rules on naming local attachments.
///
/// [`ChannelId::send_files`]: crate::model::id::ChannelId::send_files
#[inline]
pub fn attachment(self, filename: impl Into<String>) -> Self {
let mut filename = filename.into();
filename.insert_str(0, "attachment://");
self.image(filename)
}
#[cfg(feature = "http")]
pub(super) fn check_length(&self) -> Result<()> {
let mut length = 0;
if let Some(ref author) = self.0.author {
length += author.name.chars().count();
}
if let Some(ref description) = self.0.description {
length += description.chars().count();
}
for field in &self.0.fields {
length += field.name.chars().count();
length += field.value.chars().count();
}
if let Some(ref footer) = self.0.footer {
length += footer.text.chars().count();
}
if let Some(ref title) = self.0.title {
length += title.chars().count();
}
super::check_overflow(length, crate::constants::EMBED_MAX_LENGTH)
.map_err(|overflow| Error::Model(ModelError::EmbedTooLarge(overflow)))
}
}
impl Default for CreateEmbed {
/// Creates a builder with default values, setting the `type` to `rich`.
fn default() -> Self {
Self(Embed {
fields: Vec::new(),
description: None,
thumbnail: None,
timestamp: None,
kind: Some("rich".into()),
author: None,
colour: None,
footer: None,
image: None,
title: None,
url: None,
video: None,
provider: None,
})
}
}
impl From<Embed> for CreateEmbed {
fn from(embed: Embed) -> Self {
Self(embed)
}
}
/// A builder to create the author data of an embed. See [`CreateEmbed::author`]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateEmbedAuthor(EmbedAuthor);
impl CreateEmbedAuthor {
/// Creates an author object with the given name, leaving all other fields empty.
pub fn new(name: impl Into<String>) -> Self {
Self(EmbedAuthor {
name: name.into(),
icon_url: None,
url: None,
// Has no builder method because I think this field is only relevant when receiving (?)
proxy_icon_url: None,
})
}
/// Set the author's name, replacing the current value as set in [`Self::new`].
pub fn name(mut self, name: impl Into<String>) -> Self {
self.0.name = name.into();
self
}
/// Set the URL of the author's icon.
pub fn icon_url(mut self, icon_url: impl Into<String>) -> Self {
self.0.icon_url = Some(icon_url.into());
self
}
/// Set the author's URL.
pub fn url(mut self, url: impl Into<String>) -> Self {
self.0.url = Some(url.into());
self
}
}
impl From<EmbedAuthor> for CreateEmbedAuthor {
fn from(author: EmbedAuthor) -> Self {
Self(author)
}
}
#[cfg(feature = "model")]
impl From<User> for CreateEmbedAuthor {
fn from(user: User) -> Self {
let avatar_icon = user.face();
Self::new(user.name).icon_url(avatar_icon)
}
}
#[cfg(feature = "model")]
impl From<&User> for CreateEmbedAuthor {
fn from(user: &User) -> Self {
let avatar_icon = user.face();
Self::new(user.name.clone()).icon_url(avatar_icon)
}
}
/// A builder to create the footer data for an embed. See [`CreateEmbed::footer`]
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateEmbedFooter(EmbedFooter);
impl CreateEmbedFooter {
/// Creates a new footer object with the given text, leaving all other fields empty.
pub fn new(text: impl Into<String>) -> Self {
Self(EmbedFooter {
text: text.into(),
icon_url: None,
// Has no builder method because I think this field is only relevant when receiving (?)
proxy_icon_url: None,
})
}
/// Set the footer's text, replacing the current value as set in [`Self::new`].
pub fn text(mut self, text: impl Into<String>) -> Self {
self.0.text = text.into();
self
}
/// Set the icon URL's value.
///
/// Refer [`CreateEmbed::image`] for rules on naming local attachments.
pub fn icon_url(mut self, icon_url: impl Into<String>) -> Self {
self.0.icon_url = Some(icon_url.into());
self
}
}
impl From<EmbedFooter> for CreateEmbedFooter {
fn from(footer: EmbedFooter) -> Self {
Self(footer)
}
}