Skip to main content

djs_model/component/
content.rs

1//! Content components (Components V2): text, thumbnails, media galleries and files.
2
3use compact_str::CompactString;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    id::AttachmentId,
8    macros::{int_enum, int_flags},
9};
10
11/// A [text display](https://docs.discord.com/developers/components/reference#text-display):
12/// markdown text, like a message's content (messages and modals).
13///
14/// discord.js counterpart: `TextDisplayComponent` / `TextDisplayBuilder`.
15#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
16#[non_exhaustive]
17pub struct TextDisplay {
18    /// The component's numeric identifier (generated by Discord when not set).
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub id: Option<i32>,
21    /// The markdown text.
22    pub content: CompactString,
23}
24
25impl TextDisplay {
26    /// Creates a text display.
27    ///
28    /// discord.js counterpart: `TextDisplayBuilder` (`@discordjs/builders`).
29    #[must_use]
30    pub fn new(content: impl Into<CompactString>) -> Self {
31        Self {
32            id: None,
33            content: content.into(),
34        }
35    }
36}
37
38/// A [thumbnail](https://docs.discord.com/developers/components/reference#thumbnail): a small
39/// image, used as a [section](super::Section) accessory.
40///
41/// discord.js counterpart: `ThumbnailComponent` / `ThumbnailBuilder`.
42#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
43#[non_exhaustive]
44pub struct Thumbnail {
45    /// The component's numeric identifier (generated by Discord when not set).
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub id: Option<i32>,
48    /// The image (a URL or an `attachment://` reference).
49    pub media: Box<UnfurledMediaItem>,
50    /// The alt text (max 1024 characters).
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub description: Option<CompactString>,
53    /// Whether the image is blurred until clicked.
54    #[serde(skip_serializing_if = "core::ops::Not::not")]
55    pub spoiler: bool,
56}
57
58impl Thumbnail {
59    /// Creates a thumbnail.
60    ///
61    /// discord.js counterpart: `ThumbnailBuilder` (`@discordjs/builders`).
62    #[must_use]
63    pub fn new(media: UnfurledMediaItem) -> Self {
64        Self {
65            id: None,
66            media: Box::new(media),
67            description: None,
68            spoiler: false,
69        }
70    }
71}
72
73/// A [media gallery](https://docs.discord.com/developers/components/reference#media-gallery):
74/// 1-10 images or videos in a grid.
75///
76/// discord.js counterpart: `MediaGalleryComponent` / `MediaGalleryBuilder`.
77#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
78#[non_exhaustive]
79pub struct MediaGallery {
80    /// The component's numeric identifier (generated by Discord when not set).
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub id: Option<i32>,
83    /// The items (1-10).
84    pub items: Vec<MediaGalleryItem>,
85}
86
87impl MediaGallery {
88    /// Creates a media gallery.
89    ///
90    /// discord.js counterpart: `MediaGalleryBuilder` (`@discordjs/builders`).
91    #[must_use]
92    pub const fn new(items: Vec<MediaGalleryItem>) -> Self {
93        Self { id: None, items }
94    }
95}
96
97/// An [item of a media gallery](https://docs.discord.com/developers/components/reference#media-gallery-media-gallery-item-structure).
98///
99/// discord.js counterpart: `MediaGalleryItem` / `MediaGalleryItemBuilder`.
100#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
101#[non_exhaustive]
102pub struct MediaGalleryItem {
103    /// The image or video (a URL or an `attachment://` reference).
104    pub media: UnfurledMediaItem,
105    /// The alt text (max 1024 characters).
106    #[serde(default, skip_serializing_if = "Option::is_none")]
107    pub description: Option<CompactString>,
108    /// Whether the item is blurred until clicked.
109    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
110    pub spoiler: bool,
111}
112
113impl MediaGalleryItem {
114    /// Creates an item.
115    ///
116    /// discord.js counterpart: `MediaGalleryItemBuilder` (`@discordjs/builders`).
117    #[must_use]
118    pub const fn new(media: UnfurledMediaItem) -> Self {
119        Self {
120            media,
121            description: None,
122            spoiler: false,
123        }
124    }
125}
126
127/// A [file](https://docs.discord.com/developers/components/reference#file): an attached file
128/// shown in the message.
129///
130/// discord.js counterpart: `FileComponent` / `FileBuilder`.
131#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
132#[non_exhaustive]
133pub struct File {
134    /// The component's numeric identifier (generated by Discord when not set).
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub id: Option<i32>,
137    /// The file: only `attachment://<filename>` references are supported.
138    pub file: Box<UnfurledMediaItem>,
139    /// Whether the file is blurred until clicked.
140    #[serde(skip_serializing_if = "core::ops::Not::not")]
141    pub spoiler: bool,
142    /// The file's name (set by Discord in responses; ignored when sending).
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub name: Option<CompactString>,
145    /// The file's size in bytes (set by Discord in responses; ignored when sending).
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub size: Option<u64>,
148}
149
150impl File {
151    /// Creates a file component.
152    ///
153    /// discord.js counterpart: `FileBuilder` (`@discordjs/builders`).
154    #[must_use]
155    pub fn new(file: UnfurledMediaItem) -> Self {
156        Self {
157            id: None,
158            file: Box::new(file),
159            spoiler: false,
160            name: None,
161            size: None,
162        }
163    }
164}
165
166/// An [unfurled media item](https://docs.discord.com/developers/components/reference#unfurled-media-item):
167/// a URL or an uploaded attachment that Discord resolves into media.
168///
169/// When sending, only [`url`](Self::url) is used; Discord fills in the other fields in
170/// responses.
171///
172/// discord.js counterpart: `UnfurledMediaItem`.
173#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
174#[non_exhaustive]
175pub struct UnfurledMediaItem {
176    /// Any URL, or `attachment://<filename>` for an uploaded file.
177    pub url: CompactString,
178    /// The URL through Discord's media proxy.
179    #[serde(default, skip_serializing_if = "Option::is_none")]
180    pub proxy_url: Option<CompactString>,
181    /// The height of an image or video, in pixels.
182    #[serde(default, skip_serializing_if = "Option::is_none")]
183    pub height: Option<u32>,
184    /// The width of an image or video, in pixels.
185    #[serde(default, skip_serializing_if = "Option::is_none")]
186    pub width: Option<u32>,
187    /// A [thumbhash](https://evanw.github.io/thumbhash/) placeholder of an image or video.
188    #[serde(default, skip_serializing_if = "Option::is_none")]
189    pub placeholder: Option<CompactString>,
190    /// The version of the placeholder.
191    #[serde(default, skip_serializing_if = "Option::is_none")]
192    pub placeholder_version: Option<u32>,
193    /// The [media type](https://en.wikipedia.org/wiki/Media_type).
194    #[serde(default, skip_serializing_if = "Option::is_none")]
195    pub content_type: Option<CompactString>,
196    /// Whether Discord could load the media.
197    ///
198    /// **Undocumented:** known from discord-api-types, not in the official docs; may change
199    /// without notice.
200    #[serde(default, skip_serializing_if = "Option::is_none")]
201    pub loading_state: Option<LoadingState>,
202    /// The media flags.
203    #[serde(default, skip_serializing_if = "Option::is_none")]
204    pub flags: Option<UnfurledMediaItemFlags>,
205    /// The results of Discord's content scanning, once it ran. Scanning can finish after the
206    /// message was sent; then the results arrive in a later `MESSAGE_UPDATE`.
207    #[serde(default, skip_serializing_if = "Option::is_none")]
208    pub content_scan_metadata: Option<ContentScanMetadata>,
209    /// The uploaded attachment, for `attachment://` references.
210    #[serde(default, skip_serializing_if = "Option::is_none")]
211    pub attachment_id: Option<AttachmentId>,
212}
213
214impl UnfurledMediaItem {
215    /// Creates a media item from a URL or `attachment://<filename>` reference.
216    ///
217    /// discord.js counterpart: `UnfurledMediaItemData`.
218    #[must_use]
219    pub fn new(url: impl Into<CompactString>) -> Self {
220        Self {
221            url: url.into(),
222            proxy_url: None,
223            height: None,
224            width: None,
225            placeholder: None,
226            placeholder_version: None,
227            content_type: None,
228            loading_state: None,
229            flags: None,
230            content_scan_metadata: None,
231            attachment_id: None,
232        }
233    }
234}
235
236int_enum! {
237    /// Whether Discord could load an [`UnfurledMediaItem`].
238    ///
239    /// **Undocumented:** known from discord-api-types (`UnfurledMediaItemLoadingState`), not in
240    /// the official docs; may change without notice.
241    ///
242    /// discord.js counterpart: `UnfurledMediaItemLoadingState` (discord-api-types).
243    #[doc(alias = "UnfurledMediaItemLoadingState")]
244    pub enum LoadingState: u8 {
245        /// The state is not known.
246        Unspecified = 0,
247        /// The media is being loaded.
248        Loading = 1,
249        /// The media was loaded.
250        LoadedSuccess = 2,
251        /// The media was not found.
252        LoadedNotFound = 3,
253    }
254}
255
256int_flags! {
257    /// [Flags of an unfurled media item](https://docs.discord.com/developers/components/reference#unfurled-media-item-unfurled-media-item-flags).
258    ///
259    /// discord.js counterpart: `UnfurledMediaItemFlags` (discord-api-types).
260    pub struct UnfurledMediaItemFlags: u64 {
261        /// The image is animated.
262        const IS_ANIMATED = 1 << 0;
263    }
264}
265
266/// The [results of Discord's content scanning](https://docs.discord.com/developers/components/reference#unfurled-media-item-content-scan-metadata-structure)
267/// of an [`UnfurledMediaItem`].
268///
269/// discord.js counterpart: `UnfurledMediaItemContentScanMetadata` (discord-api-types).
270#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
271#[non_exhaustive]
272pub struct ContentScanMetadata {
273    /// What the media was flagged as.
274    pub flags: ContentScanMetadataFlags,
275}
276
277int_flags! {
278    /// [Flags of content scan metadata](https://docs.discord.com/developers/components/reference#unfurled-media-item-content-scan-metadata-flags).
279    ///
280    /// discord.js counterpart: `UnfurledMediaItemContentScanMetadataFlags` (discord-api-types).
281    pub struct ContentScanMetadataFlags: u64 {
282        /// The media was flagged as explicit.
283        const IS_EXPLICIT_CONTENT = 1 << 0;
284        /// The media was flagged as gore.
285        const IS_GORE_CONTENT = 1 << 1;
286        /// The media was flagged as self-harm.
287        const IS_SELF_HARM_CONTENT = 1 << 2;
288    }
289}