Skip to main content

djs_builders/component/
content.rs

1//! Components V2 content builders: [`TextDisplayBuilder`], [`ThumbnailBuilder`],
2//! [`MediaGalleryBuilder`] with [`MediaGalleryItemBuilder`], and [`FileBuilder`].
3
4use compact_str::CompactString;
5use djs_model::component::{
6    File, MediaGallery, MediaGalleryItem, TextDisplay, Thumbnail, UnfurledMediaItem,
7};
8
9/// Builds a [text display](https://docs.discord.com/developers/components/reference#text-display):
10/// markdown text in a Components V2 message, a container or a section (1-4000 characters,
11/// not blank). All text displays of a message share 4000 characters (D-200).
12///
13/// Sections and containers also take text directly with `.text("…")` (D-204).
14///
15/// discord.js counterpart: `TextDisplayBuilder`.
16///
17/// # Examples
18///
19/// ```
20/// use djs_builders::TextDisplayBuilder;
21///
22/// let text = TextDisplayBuilder::new("## Release notes\n- faster cache").build().unwrap();
23/// assert_eq!(text.kind(), djs_model::component::ComponentType::TextDisplay);
24/// ```
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[must_use = "a builder does nothing until it is added to a message or built"]
27pub struct TextDisplayBuilder {
28    component: TextDisplay,
29}
30
31impl TextDisplayBuilder {
32    /// Creates a text display showing `content` (markdown, 1-4000 characters).
33    ///
34    /// discord.js counterpart: `new TextDisplayBuilder().setContent(…)`.
35    pub fn new(content: impl Into<CompactString>) -> Self {
36        Self {
37            component: TextDisplay::new(content),
38        }
39    }
40}
41
42component_builder!(TextDisplayBuilder => TextDisplay);
43
44/// Builds a [thumbnail](https://docs.discord.com/developers/components/reference#thumbnail):
45/// a small image, the accessory of a [section](crate::SectionBuilder).
46///
47/// The URL is a link (`http://`, `https://`) or a file uploaded with the message
48/// (`attachment://name.png`); other schemes are rejected although Discord accepts them, since
49/// it cannot show them (D-190, D-205). The description (alt text) has 1-1024 characters.
50///
51/// discord.js counterpart: `ThumbnailBuilder`.
52#[derive(Clone, Debug, PartialEq, Eq)]
53#[must_use = "a builder does nothing until it is added to a section or built"]
54pub struct ThumbnailBuilder {
55    component: Thumbnail,
56}
57
58impl ThumbnailBuilder {
59    /// Creates a thumbnail showing `url`.
60    ///
61    /// discord.js counterpart: `new ThumbnailBuilder().setURL(…)`.
62    pub fn new(url: impl Into<CompactString>) -> Self {
63        Self {
64            component: Thumbnail::new(UnfurledMediaItem::new(url)),
65        }
66    }
67
68    /// Sets the alt text (1-1024 characters).
69    ///
70    /// discord.js counterpart: `setDescription`.
71    pub fn description(mut self, description: impl Into<CompactString>) -> Self {
72        self.component.description = Some(description.into());
73        self
74    }
75
76    /// Sets whether the image is blurred until clicked.
77    ///
78    /// discord.js counterpart: `setSpoiler`.
79    pub fn spoiler(mut self, spoiler: bool) -> Self {
80        self.component.spoiler = spoiler;
81        self
82    }
83}
84
85component_builder!(ThumbnailBuilder => Thumbnail);
86
87/// Builds a [media gallery](https://docs.discord.com/developers/components/reference#media-gallery):
88/// a grid of 1-10 images and videos.
89///
90/// Add plain items with [`item`](Self::item) and items with alt text or a spoiler with
91/// [`item_with`](Self::item_with) (D-204). Each URL is a link or an uploaded file.
92///
93/// discord.js counterpart: `MediaGalleryBuilder`.
94///
95/// # Examples
96///
97/// ```
98/// use djs_builders::{MediaGalleryBuilder, MediaGalleryItemBuilder};
99///
100/// let gallery = MediaGalleryBuilder::new()
101///     .item("https://example.com/a.png")
102///     .item_with(MediaGalleryItemBuilder::new("attachment://b.png").description("A chart").spoiler(true))
103///     .build()
104///     .unwrap();
105/// assert_eq!(gallery.kind(), djs_model::component::ComponentType::MediaGallery);
106/// ```
107#[derive(Clone, Debug, PartialEq, Eq)]
108#[must_use = "a builder does nothing until it is added to a message or built"]
109pub struct MediaGalleryBuilder {
110    component: MediaGallery,
111}
112
113impl Default for MediaGalleryBuilder {
114    fn default() -> Self {
115        Self::new()
116    }
117}
118
119impl MediaGalleryBuilder {
120    /// Creates an empty gallery; it needs 1-10 items.
121    ///
122    /// discord.js counterpart: `new MediaGalleryBuilder()`.
123    pub fn new() -> Self {
124        Self {
125            component: MediaGallery::new(Vec::new()),
126        }
127    }
128
129    /// Adds an item showing `url`.
130    ///
131    /// discord.js counterpart: `addItems`.
132    pub fn item(mut self, url: impl Into<CompactString>) -> Self {
133        self.component
134            .items
135            .push(MediaGalleryItem::new(UnfurledMediaItem::new(url)));
136        self
137    }
138
139    /// Adds an item with alt text or a spoiler.
140    ///
141    /// discord.js counterpart: `addItems(new MediaGalleryItemBuilder()…)`.
142    pub fn item_with(mut self, item: MediaGalleryItemBuilder) -> Self {
143        self.component.items.push(item.item);
144        self
145    }
146
147    /// Adds several items: [`MediaGalleryItemBuilder`]s or `djs-model`
148    /// [`MediaGalleryItem`]s.
149    ///
150    /// discord.js counterpart: `addItems`.
151    pub fn items<I: Into<MediaGalleryItem>>(mut self, items: impl IntoIterator<Item = I>) -> Self {
152        self.component
153            .items
154            .extend(items.into_iter().map(Into::into));
155        self
156    }
157}
158
159component_builder!(MediaGalleryBuilder => MediaGallery);
160
161/// Builds an [item of a media gallery](https://docs.discord.com/developers/components/reference#media-gallery-media-gallery-item-structure)
162/// with alt text or a spoiler, for [`MediaGalleryBuilder::item_with`].
163///
164/// discord.js counterpart: `MediaGalleryItemBuilder`.
165#[derive(Clone, Debug, PartialEq, Eq)]
166#[must_use = "an item does nothing until it is added to a gallery"]
167pub struct MediaGalleryItemBuilder {
168    item: MediaGalleryItem,
169}
170
171impl MediaGalleryItemBuilder {
172    /// Creates an item showing `url` (a link or `attachment://name`).
173    ///
174    /// discord.js counterpart: `new MediaGalleryItemBuilder().setURL(…)`.
175    pub fn new(url: impl Into<CompactString>) -> Self {
176        Self {
177            item: MediaGalleryItem::new(UnfurledMediaItem::new(url)),
178        }
179    }
180
181    /// Sets the alt text (1-1024 characters).
182    ///
183    /// discord.js counterpart: `setDescription`.
184    pub fn description(mut self, description: impl Into<CompactString>) -> Self {
185        self.item.description = Some(description.into());
186        self
187    }
188
189    /// Sets whether the item is blurred until clicked.
190    ///
191    /// discord.js counterpart: `setSpoiler`.
192    pub fn spoiler(mut self, spoiler: bool) -> Self {
193        self.item.spoiler = spoiler;
194        self
195    }
196}
197
198impl From<MediaGalleryItemBuilder> for MediaGalleryItem {
199    fn from(item: MediaGalleryItemBuilder) -> Self {
200        item.item
201    }
202}
203
204/// Builds a [file component](https://docs.discord.com/developers/components/reference#file):
205/// a file uploaded with the message, shown as a downloadable box.
206///
207/// The URL must be `attachment://name` naming one of the message's files — Discord refuses
208/// links here (D-205).
209///
210/// discord.js counterpart: `FileBuilder`.
211///
212/// # Examples
213///
214/// ```
215/// use djs_builders::FileBuilder;
216///
217/// assert!(FileBuilder::new("attachment://report.pdf").build().is_ok());
218/// assert!(FileBuilder::new("https://example.com/report.pdf").build().is_err());
219/// ```
220#[derive(Clone, Debug, PartialEq, Eq)]
221#[must_use = "a builder does nothing until it is added to a message or built"]
222pub struct FileBuilder {
223    component: File,
224}
225
226impl FileBuilder {
227    /// Creates a file component showing the uploaded file `url` (`attachment://name`).
228    ///
229    /// discord.js counterpart: `new FileBuilder().setURL(…)`.
230    pub fn new(url: impl Into<CompactString>) -> Self {
231        Self {
232            component: File::new(UnfurledMediaItem::new(url)),
233        }
234    }
235
236    /// Sets whether the file is blurred until clicked.
237    ///
238    /// discord.js counterpart: `setSpoiler`.
239    pub fn spoiler(mut self, spoiler: bool) -> Self {
240        self.component.spoiler = spoiler;
241        self
242    }
243}
244
245component_builder!(FileBuilder => File);