djs_model/component/interactive.rs
1//! Interactive components: buttons, select menus and modal inputs.
2
3use compact_str::CompactString;
4use serde::{Serialize, Serializer};
5
6use crate::{
7 channel::ChannelType,
8 emoji::PartialEmoji,
9 id::{GenericId, SkuId},
10 macros::{int_enum, str_enum},
11};
12
13/// A [button](https://docs.discord.com/developers/components/reference#button), in an
14/// [action row](super::ActionRow) or as a [section](super::Section) accessory (messages only).
15///
16/// Its `style` decides which fields it has (D-066). Link and premium buttons do not send an
17/// interaction when clicked.
18///
19/// discord.js counterpart: `ButtonComponent` / `ButtonBuilder`.
20#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22#[doc(alias = "ButtonComponent")]
23pub enum Button {
24 /// A button with a `custom_id` that sends an interaction (styles 1-4).
25 Interactive(InteractiveButton),
26 /// A button that opens a URL (style 5).
27 Link(LinkButton),
28 /// A button that opens the purchase of an SKU (style 6).
29 Premium(PremiumButton),
30 /// A button style this version of the library does not know about (boxed: it keeps every
31 /// button field).
32 Unknown(Box<UnknownButton>),
33}
34
35impl Button {
36 /// Returns the component's numeric identifier, if set.
37 ///
38 /// discord.js counterpart: `ButtonComponent#id`.
39 #[must_use]
40 pub const fn id(&self) -> Option<i32> {
41 match self {
42 Self::Interactive(button) => button.id,
43 Self::Link(button) => button.id,
44 Self::Premium(button) => button.id,
45 Self::Unknown(button) => button.id,
46 }
47 }
48
49 /// Returns the button's style.
50 ///
51 /// discord.js counterpart: `ButtonComponent#style`.
52 #[must_use]
53 pub const fn style(&self) -> ButtonStyle {
54 match self {
55 Self::Interactive(button) => button.style,
56 Self::Link(_) => ButtonStyle::Link,
57 Self::Premium(_) => ButtonStyle::Premium,
58 Self::Unknown(button) => button.style,
59 }
60 }
61
62 /// Returns the developer-defined identifier of an interactive button.
63 ///
64 /// discord.js counterpart: `ButtonComponent#customId`.
65 #[must_use]
66 pub fn custom_id(&self) -> Option<&str> {
67 match self {
68 Self::Interactive(button) => Some(&button.custom_id),
69 Self::Unknown(button) => button.custom_id.as_deref(),
70 Self::Link(_) | Self::Premium(_) => None,
71 }
72 }
73
74 /// Whether the button is disabled.
75 ///
76 /// discord.js counterpart: `ButtonComponent#disabled`.
77 #[must_use]
78 pub const fn disabled(&self) -> bool {
79 match self {
80 Self::Interactive(button) => button.disabled,
81 Self::Link(button) => button.disabled,
82 Self::Premium(button) => button.disabled,
83 Self::Unknown(button) => button.disabled,
84 }
85 }
86}
87
88/// Serializes a button variant together with its `style`.
89#[derive(Serialize)]
90struct Styled<'a, T> {
91 style: ButtonStyle,
92 #[serde(flatten)]
93 button: &'a T,
94}
95
96impl Serialize for Button {
97 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
98 let style = self.style();
99 match self {
100 Self::Interactive(button) => Styled { style, button }.serialize(serializer),
101 Self::Link(button) => Styled { style, button }.serialize(serializer),
102 Self::Premium(button) => Styled { style, button }.serialize(serializer),
103 Self::Unknown(button) => Styled { style, button }.serialize(serializer),
104 }
105 }
106}
107
108/// A button that sends an interaction with its [`custom_id`](Self::custom_id) when clicked
109/// (styles [`Primary`](ButtonStyle::Primary) to [`Danger`](ButtonStyle::Danger)).
110///
111/// discord.js counterpart: `APIButtonComponentWithCustomId` (discord-api-types).
112#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
113#[non_exhaustive]
114pub struct InteractiveButton {
115 /// The component's numeric identifier (generated by Discord when not set).
116 #[serde(skip_serializing_if = "Option::is_none")]
117 pub id: Option<i32>,
118 /// The style: one of `Primary`, `Secondary`, `Success`, `Danger`.
119 #[serde(skip)]
120 pub style: ButtonStyle,
121 /// The developer-defined identifier (1-100 characters).
122 pub custom_id: CompactString,
123 /// The text on the button (max 80 characters).
124 #[serde(skip_serializing_if = "Option::is_none")]
125 pub label: Option<CompactString>,
126 /// The emoji on the button.
127 #[serde(skip_serializing_if = "Option::is_none")]
128 pub emoji: Option<PartialEmoji>,
129 /// Whether the button is disabled.
130 #[serde(skip_serializing_if = "core::ops::Not::not")]
131 pub disabled: bool,
132}
133
134impl InteractiveButton {
135 /// Creates an enabled button without label or emoji (set at least one before sending).
136 ///
137 /// discord.js counterpart: `ButtonBuilder` (`@discordjs/builders`).
138 #[must_use]
139 pub fn new(style: ButtonStyle, custom_id: impl Into<CompactString>) -> Self {
140 Self {
141 id: None,
142 style,
143 custom_id: custom_id.into(),
144 label: None,
145 emoji: None,
146 disabled: false,
147 }
148 }
149}
150
151/// A button that opens a [`url`](Self::url) (style [`Link`](ButtonStyle::Link)).
152///
153/// discord.js counterpart: `APIButtonComponentWithURL` (discord-api-types).
154#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
155#[non_exhaustive]
156pub struct LinkButton {
157 /// The component's numeric identifier (generated by Discord when not set).
158 #[serde(skip_serializing_if = "Option::is_none")]
159 pub id: Option<i32>,
160 /// The URL (max 512 characters).
161 pub url: CompactString,
162 /// The text on the button (max 80 characters).
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub label: Option<CompactString>,
165 /// The emoji on the button.
166 #[serde(skip_serializing_if = "Option::is_none")]
167 pub emoji: Option<PartialEmoji>,
168 /// Whether the button is disabled.
169 #[serde(skip_serializing_if = "core::ops::Not::not")]
170 pub disabled: bool,
171}
172
173impl LinkButton {
174 /// Creates an enabled link button without label or emoji (set at least one before sending).
175 ///
176 /// discord.js counterpart: `ButtonBuilder` with `setURL` (`@discordjs/builders`).
177 #[must_use]
178 pub fn new(url: impl Into<CompactString>) -> Self {
179 Self {
180 id: None,
181 url: url.into(),
182 label: None,
183 emoji: None,
184 disabled: false,
185 }
186 }
187}
188
189/// A button that starts the purchase of an [SKU](crate::sku) (style
190/// [`Premium`](ButtonStyle::Premium)). It shows the SKU's name and price; it cannot have a
191/// label or emoji.
192///
193/// discord.js counterpart: `APIButtonComponentWithSKUId` (discord-api-types).
194#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
195#[non_exhaustive]
196pub struct PremiumButton {
197 /// The component's numeric identifier (generated by Discord when not set).
198 #[serde(skip_serializing_if = "Option::is_none")]
199 pub id: Option<i32>,
200 /// The SKU to purchase.
201 pub sku_id: SkuId,
202 /// Whether the button is disabled.
203 #[serde(skip_serializing_if = "core::ops::Not::not")]
204 pub disabled: bool,
205}
206
207impl PremiumButton {
208 /// Creates an enabled premium button.
209 ///
210 /// discord.js counterpart: `ButtonBuilder` with `setSKUId` (`@discordjs/builders`).
211 #[must_use]
212 pub const fn new(sku_id: SkuId) -> Self {
213 Self {
214 id: None,
215 sku_id,
216 disabled: false,
217 }
218 }
219}
220
221/// A button whose style this version of the library does not know about; every button field is
222/// kept.
223///
224/// discord.js counterpart: none (a button style unknown to this version, kept as received).
225#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
226#[non_exhaustive]
227pub struct UnknownButton {
228 /// The component's numeric identifier.
229 #[serde(skip_serializing_if = "Option::is_none")]
230 pub id: Option<i32>,
231 /// The raw style.
232 #[serde(skip)]
233 pub style: ButtonStyle,
234 /// The developer-defined identifier.
235 #[serde(skip_serializing_if = "Option::is_none")]
236 pub custom_id: Option<CompactString>,
237 /// The text on the button.
238 #[serde(skip_serializing_if = "Option::is_none")]
239 pub label: Option<CompactString>,
240 /// The emoji on the button.
241 #[serde(skip_serializing_if = "Option::is_none")]
242 pub emoji: Option<PartialEmoji>,
243 /// The URL.
244 #[serde(skip_serializing_if = "Option::is_none")]
245 pub url: Option<CompactString>,
246 /// The SKU.
247 #[serde(skip_serializing_if = "Option::is_none")]
248 pub sku_id: Option<SkuId>,
249 /// Whether the button is disabled.
250 #[serde(skip_serializing_if = "core::ops::Not::not")]
251 pub disabled: bool,
252}
253
254int_enum! {
255 /// The [style of a button](https://docs.discord.com/developers/components/reference#button-button-styles).
256 ///
257 /// discord.js counterpart: `ButtonStyle`.
258 pub enum ButtonStyle: u8 {
259 /// The most important or recommended action (blurple).
260 Primary = 1,
261 /// An alternative or supporting action (grey).
262 Secondary = 2,
263 /// A positive confirmation or completion (green).
264 Success = 3,
265 /// An action with irreversible consequences (red).
266 Danger = 4,
267 /// Opens a URL.
268 Link = 5,
269 /// Starts a purchase.
270 Premium = 6,
271 }
272}
273
274/// A [string select](https://docs.discord.com/developers/components/reference#string-select):
275/// a menu of developer-defined options (in an action row in messages, in a label in modals).
276///
277/// discord.js counterpart: `StringSelectMenuComponent` / `StringSelectMenuBuilder`.
278#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
279#[non_exhaustive]
280#[doc(alias = "StringSelectMenu")]
281pub struct StringSelect {
282 /// The component's numeric identifier (generated by Discord when not set).
283 #[serde(skip_serializing_if = "Option::is_none")]
284 pub id: Option<i32>,
285 /// The developer-defined identifier (1-100 characters).
286 pub custom_id: CompactString,
287 /// The options (max 25).
288 pub options: Vec<SelectOption>,
289 /// The text shown when nothing is selected (max 150 characters).
290 #[serde(skip_serializing_if = "Option::is_none")]
291 pub placeholder: Option<CompactString>,
292 /// The minimum number of options to choose (Discord's default: 1; 0-25).
293 #[serde(skip_serializing_if = "Option::is_none")]
294 pub min_values: Option<u8>,
295 /// The maximum number of options to choose (Discord's default: 1; max 25).
296 #[serde(skip_serializing_if = "Option::is_none")]
297 pub max_values: Option<u8>,
298 /// Whether an answer is required in a modal (default `true`; ignored in messages).
299 pub required: bool,
300 /// Whether the menu is disabled (messages only; modals reject it).
301 #[serde(skip_serializing_if = "core::ops::Not::not")]
302 pub disabled: bool,
303}
304
305impl StringSelect {
306 /// Creates a select menu with the given options.
307 ///
308 /// discord.js counterpart: `StringSelectMenuBuilder` (`@discordjs/builders`).
309 #[must_use]
310 pub fn new(custom_id: impl Into<CompactString>, options: Vec<SelectOption>) -> Self {
311 Self {
312 id: None,
313 custom_id: custom_id.into(),
314 options,
315 placeholder: None,
316 min_values: None,
317 max_values: None,
318 required: true,
319 disabled: false,
320 }
321 }
322}
323
324/// An [option of a string select](https://docs.discord.com/developers/components/reference#string-select-select-option-structure).
325///
326/// discord.js counterpart: `APISelectMenuOption` / `StringSelectMenuOptionBuilder`.
327#[derive(Clone, Debug, PartialEq, Eq, Serialize, serde::Deserialize)]
328#[non_exhaustive]
329pub struct SelectOption {
330 /// The text shown to the user (max 100 characters).
331 pub label: CompactString,
332 /// The developer-defined value (max 100 characters).
333 pub value: CompactString,
334 /// An additional description (max 100 characters).
335 #[serde(default, skip_serializing_if = "Option::is_none")]
336 pub description: Option<CompactString>,
337 /// The emoji.
338 #[serde(default, skip_serializing_if = "Option::is_none")]
339 pub emoji: Option<PartialEmoji>,
340 /// Whether the option is selected by default.
341 #[serde(default, skip_serializing_if = "core::ops::Not::not")]
342 pub default: bool,
343}
344
345impl SelectOption {
346 /// Creates an option.
347 ///
348 /// discord.js counterpart: `StringSelectMenuOptionBuilder` (`@discordjs/builders`).
349 #[must_use]
350 pub fn new(label: impl Into<CompactString>, value: impl Into<CompactString>) -> Self {
351 Self {
352 label: label.into(),
353 value: value.into(),
354 description: None,
355 emoji: None,
356 default: false,
357 }
358 }
359}
360
361/// A [text input](https://docs.discord.com/developers/components/reference#text-input) in a
362/// modal (inside a [label](super::Label)).
363///
364/// discord.js counterpart: `TextInputComponent` / `TextInputBuilder`.
365#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
366#[non_exhaustive]
367pub struct TextInput {
368 /// The component's numeric identifier (generated by Discord when not set).
369 #[serde(skip_serializing_if = "Option::is_none")]
370 pub id: Option<i32>,
371 /// The developer-defined identifier (1-100 characters).
372 pub custom_id: CompactString,
373 /// Single-line or multi-line.
374 pub style: TextInputStyle,
375 /// The label above the input. **Deprecated** by Discord: put the input in a
376 /// [`Label`](super::Label) instead.
377 #[serde(skip_serializing_if = "Option::is_none")]
378 pub label: Option<CompactString>,
379 /// The minimum length (0-4000).
380 #[serde(skip_serializing_if = "Option::is_none")]
381 pub min_length: Option<u16>,
382 /// The maximum length (1-4000).
383 #[serde(skip_serializing_if = "Option::is_none")]
384 pub max_length: Option<u16>,
385 /// Whether the input must be filled (default `true`).
386 pub required: bool,
387 /// The pre-filled value (max 4000 characters).
388 #[serde(skip_serializing_if = "Option::is_none")]
389 pub value: Option<CompactString>,
390 /// The text shown when the input is empty (max 100 characters).
391 #[serde(skip_serializing_if = "Option::is_none")]
392 pub placeholder: Option<CompactString>,
393}
394
395impl TextInput {
396 /// Creates a required text input.
397 ///
398 /// discord.js counterpart: `TextInputBuilder` (`@discordjs/builders`).
399 #[must_use]
400 pub fn new(custom_id: impl Into<CompactString>, style: TextInputStyle) -> Self {
401 Self {
402 id: None,
403 custom_id: custom_id.into(),
404 style,
405 label: None,
406 min_length: None,
407 max_length: None,
408 required: true,
409 value: None,
410 placeholder: None,
411 }
412 }
413}
414
415int_enum! {
416 /// The [style of a text input](https://docs.discord.com/developers/components/reference#text-input-text-input-styles).
417 ///
418 /// discord.js counterpart: `TextInputStyle`.
419 pub enum TextInputStyle: u8 {
420 /// A single line.
421 Short = 1,
422 /// Several lines.
423 Paragraph = 2,
424 }
425}
426
427/// A select menu whose options Discord fills in: the [user](https://docs.discord.com/developers/components/reference#user-select),
428/// [role](https://docs.discord.com/developers/components/reference#role-select) and
429/// [mentionable](https://docs.discord.com/developers/components/reference#mentionable-select)
430/// selects share this structure (see [`ChannelSelect`] for channels).
431///
432/// discord.js counterpart: `UserSelectMenuComponent`, `RoleSelectMenuComponent`,
433/// `MentionableSelectMenuComponent`.
434#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
435#[non_exhaustive]
436#[doc(
437 alias = "UserSelect",
438 alias = "RoleSelect",
439 alias = "MentionableSelect"
440)]
441pub struct AutoPopulatedSelect {
442 /// The component's numeric identifier (generated by Discord when not set).
443 #[serde(skip_serializing_if = "Option::is_none")]
444 pub id: Option<i32>,
445 /// The developer-defined identifier (1-100 characters).
446 pub custom_id: CompactString,
447 /// The text shown when nothing is selected (max 150 characters).
448 #[serde(skip_serializing_if = "Option::is_none")]
449 pub placeholder: Option<CompactString>,
450 /// The entities selected by default; their number must be within `min_values` and
451 /// `max_values`.
452 #[serde(skip_serializing_if = "Vec::is_empty")]
453 pub default_values: Vec<SelectDefaultValue>,
454 /// The minimum number of entities to choose (Discord's default: 1; 0-25).
455 #[serde(skip_serializing_if = "Option::is_none")]
456 pub min_values: Option<u8>,
457 /// The maximum number of entities to choose (Discord's default: 1; max 25).
458 #[serde(skip_serializing_if = "Option::is_none")]
459 pub max_values: Option<u8>,
460 /// Whether an answer is required in a modal (default `true`; ignored in messages).
461 pub required: bool,
462 /// Whether the menu is disabled (messages only; modals reject it).
463 #[serde(skip_serializing_if = "core::ops::Not::not")]
464 pub disabled: bool,
465}
466
467impl AutoPopulatedSelect {
468 /// Creates a select menu.
469 ///
470 /// discord.js counterpart: `UserSelectMenuBuilder` and its siblings (`@discordjs/builders`).
471 #[must_use]
472 pub fn new(custom_id: impl Into<CompactString>) -> Self {
473 Self {
474 id: None,
475 custom_id: custom_id.into(),
476 placeholder: None,
477 default_values: Vec::new(),
478 min_values: None,
479 max_values: None,
480 required: true,
481 disabled: false,
482 }
483 }
484}
485
486/// A [channel select](https://docs.discord.com/developers/components/reference#channel-select):
487/// a menu of channels that Discord fills in.
488///
489/// discord.js counterpart: `ChannelSelectMenuComponent` / `ChannelSelectMenuBuilder`.
490#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
491#[non_exhaustive]
492#[doc(alias = "ChannelSelectMenu")]
493pub struct ChannelSelect {
494 /// The component's numeric identifier (generated by Discord when not set).
495 #[serde(skip_serializing_if = "Option::is_none")]
496 pub id: Option<i32>,
497 /// The developer-defined identifier (1-100 characters).
498 pub custom_id: CompactString,
499 /// The channel types to offer (all when empty).
500 #[serde(skip_serializing_if = "Vec::is_empty")]
501 pub channel_types: Vec<ChannelType>,
502 /// The text shown when nothing is selected (max 150 characters).
503 #[serde(skip_serializing_if = "Option::is_none")]
504 pub placeholder: Option<CompactString>,
505 /// The channels selected by default.
506 #[serde(skip_serializing_if = "Vec::is_empty")]
507 pub default_values: Vec<SelectDefaultValue>,
508 /// The minimum number of channels to choose (Discord's default: 1; 0-25).
509 #[serde(skip_serializing_if = "Option::is_none")]
510 pub min_values: Option<u8>,
511 /// The maximum number of channels to choose (Discord's default: 1; max 25).
512 #[serde(skip_serializing_if = "Option::is_none")]
513 pub max_values: Option<u8>,
514 /// Whether an answer is required in a modal (default `true`; ignored in messages).
515 pub required: bool,
516 /// Whether the menu is disabled (messages only; modals reject it).
517 #[serde(skip_serializing_if = "core::ops::Not::not")]
518 pub disabled: bool,
519}
520
521impl ChannelSelect {
522 /// Creates a channel select menu.
523 ///
524 /// discord.js counterpart: `ChannelSelectMenuBuilder` (`@discordjs/builders`).
525 #[must_use]
526 pub fn new(custom_id: impl Into<CompactString>) -> Self {
527 Self {
528 id: None,
529 custom_id: custom_id.into(),
530 channel_types: Vec::new(),
531 placeholder: None,
532 default_values: Vec::new(),
533 min_values: None,
534 max_values: None,
535 required: true,
536 disabled: false,
537 }
538 }
539}
540
541/// An [entity selected by default](https://docs.discord.com/developers/components/reference#user-select-select-default-value-structure)
542/// in an auto-populated select menu.
543///
544/// discord.js counterpart: `APISelectMenuDefaultValue`.
545#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, serde::Deserialize)]
546#[non_exhaustive]
547pub struct SelectDefaultValue {
548 /// The ID of the user, role or channel.
549 pub id: GenericId,
550 /// What [`id`](Self::id) refers to.
551 #[serde(rename = "type")]
552 pub kind: SelectDefaultValueType,
553}
554
555impl SelectDefaultValue {
556 /// Creates a default value.
557 ///
558 /// discord.js counterpart: `APISelectMenuDefaultValue` (discord-api-types).
559 #[must_use]
560 pub fn new(id: GenericId, kind: SelectDefaultValueType) -> Self {
561 Self { id, kind }
562 }
563}
564
565str_enum! {
566 /// What a [`SelectDefaultValue`] refers to.
567 ///
568 /// discord.js counterpart: `SelectMenuDefaultValueType`.
569 #[doc(alias = "SelectMenuDefaultValueType")]
570 pub enum SelectDefaultValueType {
571 /// A user.
572 User = "user",
573 /// A role.
574 Role = "role",
575 /// A channel.
576 Channel = "channel",
577 }
578}
579
580/// A [file upload](https://docs.discord.com/developers/components/reference#file-upload) in a
581/// modal (inside a [label](super::Label)).
582///
583/// discord.js counterpart: `FileUploadComponent`.
584#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
585#[non_exhaustive]
586pub struct FileUpload {
587 /// The component's numeric identifier (generated by Discord when not set).
588 #[serde(skip_serializing_if = "Option::is_none")]
589 pub id: Option<i32>,
590 /// The developer-defined identifier (1-100 characters).
591 pub custom_id: CompactString,
592 /// The minimum number of files (Discord's default: 1; 0-10).
593 #[serde(skip_serializing_if = "Option::is_none")]
594 pub min_values: Option<u8>,
595 /// The maximum number of files (Discord's default: 1; max 10).
596 #[serde(skip_serializing_if = "Option::is_none")]
597 pub max_values: Option<u8>,
598 /// Whether files must be uploaded before submitting (default `true`).
599 pub required: bool,
600 /// The [file types to accept](https://docs.discord.com/developers/reference#file-type-filtering):
601 /// `image`, `video`, `audio` or extensions such as `.pdf` (max 10; all when empty).
602 #[serde(skip_serializing_if = "Vec::is_empty")]
603 pub file_types: Vec<CompactString>,
604}
605
606impl FileUpload {
607 /// Creates a required file upload for one file of any type.
608 ///
609 /// discord.js counterpart: `AttachmentBuilder`.
610 #[must_use]
611 pub fn new(custom_id: impl Into<CompactString>) -> Self {
612 Self {
613 id: None,
614 custom_id: custom_id.into(),
615 min_values: None,
616 max_values: None,
617 required: true,
618 file_types: Vec::new(),
619 }
620 }
621}
622
623/// A [radio group](https://docs.discord.com/developers/components/reference#radio-group) in a
624/// modal: exactly one of 2-10 options.
625///
626/// discord.js counterpart: `RadioGroupComponent`.
627#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
628#[non_exhaustive]
629pub struct RadioGroup {
630 /// The component's numeric identifier (generated by Discord when not set).
631 #[serde(skip_serializing_if = "Option::is_none")]
632 pub id: Option<i32>,
633 /// The developer-defined identifier (1-100 characters).
634 pub custom_id: CompactString,
635 /// The options (2-10).
636 pub options: Vec<GroupOption>,
637 /// Whether a choice is required to submit the modal (default `true`).
638 pub required: bool,
639}
640
641impl RadioGroup {
642 /// Creates a required radio group.
643 ///
644 /// discord.js counterpart: `APIRadioGroupComponent` (discord-api-types).
645 #[must_use]
646 pub fn new(custom_id: impl Into<CompactString>, options: Vec<GroupOption>) -> Self {
647 Self {
648 id: None,
649 custom_id: custom_id.into(),
650 options,
651 required: true,
652 }
653 }
654}
655
656/// A [checkbox group](https://docs.discord.com/developers/components/reference#checkbox-group)
657/// in a modal: any number of 1-10 options.
658///
659/// discord.js counterpart: `CheckboxGroupComponent`.
660#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
661#[non_exhaustive]
662pub struct CheckboxGroup {
663 /// The component's numeric identifier (generated by Discord when not set).
664 #[serde(skip_serializing_if = "Option::is_none")]
665 pub id: Option<i32>,
666 /// The developer-defined identifier (1-100 characters).
667 pub custom_id: CompactString,
668 /// The options (1-10).
669 pub options: Vec<GroupOption>,
670 /// The minimum number of options to check (Discord's default: 1; 0-10).
671 #[serde(skip_serializing_if = "Option::is_none")]
672 pub min_values: Option<u8>,
673 /// The maximum number of options to check (Discord's default: the number of options).
674 #[serde(skip_serializing_if = "Option::is_none")]
675 pub max_values: Option<u8>,
676 /// Whether checking is required to submit the modal (default `true`).
677 pub required: bool,
678}
679
680impl CheckboxGroup {
681 /// Creates a required checkbox group.
682 ///
683 /// discord.js counterpart: `APICheckboxGroupComponent` (discord-api-types).
684 #[must_use]
685 pub fn new(custom_id: impl Into<CompactString>, options: Vec<GroupOption>) -> Self {
686 Self {
687 id: None,
688 custom_id: custom_id.into(),
689 options,
690 min_values: None,
691 max_values: None,
692 required: true,
693 }
694 }
695}
696
697/// An option of a [`RadioGroup`] or [`CheckboxGroup`] (the docs define two identical
698/// structures).
699///
700/// discord.js counterpart: `APIRadioGroupOption` / `APICheckboxGroupOption` (discord-api-types).
701#[derive(Clone, Debug, PartialEq, Eq, Serialize, serde::Deserialize)]
702#[non_exhaustive]
703#[doc(alias = "RadioGroupOption", alias = "CheckboxGroupOption")]
704pub struct GroupOption {
705 /// The developer-defined value (max 100 characters).
706 pub value: CompactString,
707 /// The text shown to the user (max 100 characters).
708 pub label: CompactString,
709 /// An additional description (max 100 characters).
710 #[serde(default, skip_serializing_if = "Option::is_none")]
711 pub description: Option<CompactString>,
712 /// Whether the option is selected by default.
713 #[serde(default, skip_serializing_if = "core::ops::Not::not")]
714 pub default: bool,
715}
716
717impl GroupOption {
718 /// Creates an option.
719 ///
720 /// discord.js counterpart: `APIRadioGroupOption` (discord-api-types).
721 #[must_use]
722 pub fn new(label: impl Into<CompactString>, value: impl Into<CompactString>) -> Self {
723 Self {
724 value: value.into(),
725 label: label.into(),
726 description: None,
727 default: false,
728 }
729 }
730}
731
732/// A single [checkbox](https://docs.discord.com/developers/components/reference#checkbox) in a
733/// modal: a yes/no choice.
734///
735/// discord.js counterpart: `CheckboxComponent`.
736#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
737#[non_exhaustive]
738pub struct Checkbox {
739 /// The component's numeric identifier (generated by Discord when not set).
740 #[serde(skip_serializing_if = "Option::is_none")]
741 pub id: Option<i32>,
742 /// The developer-defined identifier (1-100 characters).
743 pub custom_id: CompactString,
744 /// Whether the checkbox is checked by default.
745 #[serde(skip_serializing_if = "core::ops::Not::not")]
746 pub default: bool,
747}
748
749impl Checkbox {
750 /// Creates an unchecked checkbox.
751 ///
752 /// discord.js counterpart: `APICheckboxComponent` (discord-api-types).
753 #[must_use]
754 pub fn new(custom_id: impl Into<CompactString>) -> Self {
755 Self {
756 id: None,
757 custom_id: custom_id.into(),
758 default: false,
759 }
760 }
761}