djs_model/message/embed.rs
1//! Embeds: rich content blocks attached to messages.
2
3use compact_str::CompactString;
4use serde::{Deserialize, Serialize};
5
6use super::{EmbedFlags, EmbedMediaFlags};
7use crate::{component::Component, macros::str_enum, util::Timestamp};
8
9/// An [embed](https://docs.discord.com/developers/resources/message#embed-object): a rich
10/// content block (link previews, images, videos, or a bot's custom "rich" embed).
11///
12/// Every part is optional. Sub-objects are boxed so an embed without them stays small (D-054).
13/// Discord's [limits](https://docs.discord.com/developers/resources/message#embed-object-embed-limits)
14/// apply when sending: title 256 characters, description 4096, up to 25 fields (name 256,
15/// value 1024), footer text 2048, author name 256, and 6000 characters across all embeds of a
16/// message. Building and validating embeds is the job of `djs-builders`.
17///
18/// discord.js counterpart: `Embed` (received) / `EmbedBuilder` (sent).
19#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
20#[non_exhaustive]
21pub struct Embed {
22 /// The title.
23 #[serde(default, skip_serializing_if = "Option::is_none")]
24 pub title: Option<CompactString>,
25 /// The kind of embed (always [`EmbedType::Rich`] for embeds sent by bots and webhooks).
26 #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
27 pub kind: Option<EmbedType>,
28 /// The description.
29 #[serde(default, skip_serializing_if = "Option::is_none")]
30 pub description: Option<CompactString>,
31 /// The URL the title links to.
32 #[serde(default, skip_serializing_if = "Option::is_none")]
33 pub url: Option<CompactString>,
34 /// The timestamp shown in the footer.
35 #[serde(default, skip_serializing_if = "Option::is_none")]
36 pub timestamp: Option<Timestamp>,
37 /// The color of the left border, as an RGB integer.
38 #[serde(default, skip_serializing_if = "Option::is_none")]
39 pub color: Option<u32>,
40 /// The footer.
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub footer: Option<Box<EmbedFooter>>,
43 /// The large image.
44 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub image: Option<Box<EmbedImage>>,
46 /// The small image in the top-right corner.
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub thumbnail: Option<Box<EmbedImage>>,
49 /// The video (only set by Discord, never sent by bots).
50 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub video: Option<Box<EmbedVideo>>,
52 /// The provider (only set by Discord, e.g. "YouTube").
53 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub provider: Option<Box<EmbedProvider>>,
55 /// The author shown above the title.
56 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub author: Option<Box<EmbedAuthor>>,
58 /// The fields (max 25).
59 #[serde(default, skip_serializing_if = "Vec::is_empty")]
60 pub fields: Vec<EmbedField>,
61 /// The components of a [component embed](https://docs.discord.com/developers/link-previews/component-embeds)
62 /// ([`EmbedType::Components`]): link buttons, sections and containers.
63 #[serde(default, skip_serializing_if = "Vec::is_empty")]
64 pub components: Vec<Component>,
65 /// The embed's flags.
66 #[serde(default, skip_serializing_if = "Option::is_none")]
67 pub flags: Option<EmbedFlags>,
68}
69
70str_enum! {
71 /// The [type of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-types).
72 ///
73 /// discord.js counterpart: `EmbedType`.
74 pub enum EmbedType {
75 /// A generic embed rendered from its fields (the only type bots can send).
76 Rich = "rich",
77 /// An image.
78 Image = "image",
79 /// A video.
80 Video = "video",
81 /// An animated GIF rendered as a video.
82 Gifv = "gifv",
83 /// An article.
84 Article = "article",
85 /// A link.
86 Link = "link",
87 /// An auto moderation alert in the alert channel.
88 ///
89 /// **Undocumented:** known from discord-api-types, not in the official docs; may change
90 /// without notice.
91 AutoModerationMessage = "auto_moderation_message",
92 /// The result of a [poll](crate::poll), in the message announcing it; the data is in
93 /// the [fields](Embed::fields) (`poll_question_text`, `victor_answer_votes`,
94 /// `total_votes`, `victor_answer_id`, `victor_answer_text`, `victor_answer_emoji_id`,
95 /// `victor_answer_emoji_name`, `victor_answer_emoji_animated`).
96 PollResult = "poll_result",
97 /// A [link preview built from components](https://docs.discord.com/developers/link-previews/component-embeds).
98 Components = "components",
99 }
100}
101
102/// The [footer of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-footer-structure).
103///
104/// discord.js counterpart: `APIEmbedFooter` (discord-api-types).
105#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
106#[non_exhaustive]
107pub struct EmbedFooter {
108 /// The footer text (max 2048 characters).
109 pub text: CompactString,
110 /// The URL of the footer icon (`http(s)` or `attachment://`).
111 #[serde(default, skip_serializing_if = "Option::is_none")]
112 pub icon_url: Option<CompactString>,
113 /// The URL of the icon through Discord's media proxy.
114 #[serde(default, skip_serializing_if = "Option::is_none")]
115 pub proxy_icon_url: Option<CompactString>,
116}
117
118impl EmbedFooter {
119 /// Creates a footer with `text` and no icon.
120 ///
121 /// Nothing is validated; `djs-builders` checks Discord's limits.
122 ///
123 /// discord.js counterpart: `EmbedFooterOptions` (`@discordjs/builders`).
124 ///
125 /// # Examples
126 ///
127 /// ```
128 /// use djs_model::message::EmbedFooter;
129 ///
130 /// let footer = EmbedFooter::new("Page 1 of 3");
131 /// assert_eq!(footer.text, "Page 1 of 3");
132 /// assert_eq!(footer.icon_url, None);
133 /// ```
134 #[must_use]
135 pub fn new(text: impl Into<CompactString>) -> Self {
136 Self {
137 text: text.into(),
138 icon_url: None,
139 proxy_icon_url: None,
140 }
141 }
142}
143
144/// The [image or thumbnail of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-image-structure).
145///
146/// discord.js counterpart: `EmbedAssetData`.
147#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
148#[non_exhaustive]
149#[doc(alias = "EmbedThumbnail")]
150pub struct EmbedImage {
151 /// The source URL (`http(s)` or `attachment://`).
152 pub url: CompactString,
153 /// The URL through Discord's media proxy.
154 #[serde(default, skip_serializing_if = "Option::is_none")]
155 pub proxy_url: Option<CompactString>,
156 /// The height in pixels.
157 #[serde(default, skip_serializing_if = "Option::is_none")]
158 pub height: Option<u32>,
159 /// The width in pixels.
160 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub width: Option<u32>,
162 /// The [media type](https://en.wikipedia.org/wiki/Media_type).
163 #[serde(default, skip_serializing_if = "Option::is_none")]
164 pub content_type: Option<CompactString>,
165 /// A [thumbhash](https://evanw.github.io/thumbhash/) placeholder.
166 #[serde(default, skip_serializing_if = "Option::is_none")]
167 pub placeholder: Option<CompactString>,
168 /// The version of the placeholder.
169 #[serde(default, skip_serializing_if = "Option::is_none")]
170 pub placeholder_version: Option<u32>,
171 /// The description (alt text).
172 #[serde(default, skip_serializing_if = "Option::is_none")]
173 pub description: Option<CompactString>,
174 /// The media flags.
175 #[serde(default, skip_serializing_if = "Option::is_none")]
176 pub flags: Option<EmbedMediaFlags>,
177}
178
179impl EmbedImage {
180 /// Creates an image or thumbnail showing `url` (`http(s)` or `attachment://`), with no
181 /// metadata — the metadata is filled in by Discord.
182 ///
183 /// Nothing is validated; `djs-builders` checks the URL scheme.
184 ///
185 /// discord.js counterpart: `EmbedImageData`.
186 ///
187 /// # Examples
188 ///
189 /// ```
190 /// use djs_model::message::EmbedImage;
191 ///
192 /// let image = EmbedImage::new("attachment://chart.png");
193 /// assert_eq!(image.url, "attachment://chart.png");
194 /// assert_eq!(image.width, None);
195 /// ```
196 #[must_use]
197 pub fn new(url: impl Into<CompactString>) -> Self {
198 Self {
199 url: url.into(),
200 proxy_url: None,
201 height: None,
202 width: None,
203 content_type: None,
204 placeholder: None,
205 placeholder_version: None,
206 description: None,
207 flags: None,
208 }
209 }
210}
211
212/// The [video of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-video-structure).
213///
214/// discord.js counterpart: `APIEmbedVideo` (discord-api-types).
215#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
216#[non_exhaustive]
217pub struct EmbedVideo {
218 /// The source URL.
219 #[serde(default, skip_serializing_if = "Option::is_none")]
220 pub url: Option<CompactString>,
221 /// The URL through Discord's media proxy.
222 #[serde(default, skip_serializing_if = "Option::is_none")]
223 pub proxy_url: Option<CompactString>,
224 /// The height in pixels.
225 #[serde(default, skip_serializing_if = "Option::is_none")]
226 pub height: Option<u32>,
227 /// The width in pixels.
228 #[serde(default, skip_serializing_if = "Option::is_none")]
229 pub width: Option<u32>,
230 /// The [media type](https://en.wikipedia.org/wiki/Media_type).
231 #[serde(default, skip_serializing_if = "Option::is_none")]
232 pub content_type: Option<CompactString>,
233 /// A [thumbhash](https://evanw.github.io/thumbhash/) placeholder.
234 #[serde(default, skip_serializing_if = "Option::is_none")]
235 pub placeholder: Option<CompactString>,
236 /// The version of the placeholder.
237 #[serde(default, skip_serializing_if = "Option::is_none")]
238 pub placeholder_version: Option<u32>,
239 /// The description (alt text).
240 #[serde(default, skip_serializing_if = "Option::is_none")]
241 pub description: Option<CompactString>,
242 /// The media flags.
243 #[serde(default, skip_serializing_if = "Option::is_none")]
244 pub flags: Option<EmbedMediaFlags>,
245}
246
247/// The [provider of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-provider-structure),
248/// e.g. the website of a link preview.
249///
250/// discord.js counterpart: `APIEmbedProvider` (discord-api-types).
251#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
252#[non_exhaustive]
253pub struct EmbedProvider {
254 /// The provider's name.
255 #[serde(default, skip_serializing_if = "Option::is_none")]
256 pub name: Option<CompactString>,
257 /// The provider's URL.
258 #[serde(default, skip_serializing_if = "Option::is_none")]
259 pub url: Option<CompactString>,
260}
261
262/// The [author of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-author-structure).
263///
264/// discord.js counterpart: `APIEmbedAuthor` (discord-api-types).
265#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
266#[non_exhaustive]
267pub struct EmbedAuthor {
268 /// The author's name (max 256 characters).
269 pub name: CompactString,
270 /// The URL the name links to (`http(s)` only).
271 #[serde(default, skip_serializing_if = "Option::is_none")]
272 pub url: Option<CompactString>,
273 /// The URL of the author icon (`http(s)` or `attachment://`).
274 #[serde(default, skip_serializing_if = "Option::is_none")]
275 pub icon_url: Option<CompactString>,
276 /// The URL of the icon through Discord's media proxy.
277 #[serde(default, skip_serializing_if = "Option::is_none")]
278 pub proxy_icon_url: Option<CompactString>,
279}
280
281impl EmbedAuthor {
282 /// Creates an author named `name`, with no URL and no icon.
283 ///
284 /// Nothing is validated; `djs-builders` checks Discord's limits.
285 ///
286 /// discord.js counterpart: `EmbedAuthorOptions` (`@discordjs/builders`).
287 ///
288 /// # Examples
289 ///
290 /// ```
291 /// use djs_model::message::EmbedAuthor;
292 ///
293 /// let author = EmbedAuthor::new("djs-rs");
294 /// assert_eq!(author.name, "djs-rs");
295 /// assert_eq!(author.url, None);
296 /// ```
297 #[must_use]
298 pub fn new(name: impl Into<CompactString>) -> Self {
299 Self {
300 name: name.into(),
301 url: None,
302 icon_url: None,
303 proxy_icon_url: None,
304 }
305 }
306}
307
308/// A [field of an embed](https://docs.discord.com/developers/resources/message#embed-object-embed-field-structure).
309///
310/// discord.js counterpart: `APIEmbedField`.
311#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
312#[non_exhaustive]
313pub struct EmbedField {
314 /// The field's name (max 256 characters).
315 pub name: CompactString,
316 /// The field's value (max 1024 characters).
317 pub value: CompactString,
318 /// Whether the field is shown next to other inline fields.
319 #[serde(default, skip_serializing_if = "core::ops::Not::not")]
320 pub inline: bool,
321}
322
323impl EmbedField {
324 /// Creates a field named `name` showing `value`, next to other inline fields if `inline`.
325 ///
326 /// Nothing is validated; `djs-builders` checks Discord's limits.
327 ///
328 /// discord.js counterpart: `EmbedField`.
329 ///
330 /// # Examples
331 ///
332 /// ```
333 /// use djs_model::message::EmbedField;
334 ///
335 /// let field = EmbedField::new("Ping", "42 ms", true);
336 /// assert_eq!((field.name.as_str(), field.value.as_str(), field.inline), ("Ping", "42 ms", true));
337 /// ```
338 #[must_use]
339 pub fn new(
340 name: impl Into<CompactString>,
341 value: impl Into<CompactString>,
342 inline: bool,
343 ) -> Self {
344 Self {
345 name: name.into(),
346 value: value.into(),
347 inline,
348 }
349 }
350}