pub struct ModalBuilder { /* private fields */ }Expand description
Builds a modal: a pop-up form your app shows in answer to an interaction.
A modal has a custom_id, a title (1-45 characters) and 1-5 labels and text displays
(D-206, D-207). Each label holds one field: a
text input, a select menu, a file upload, a
radio group, a checkbox group or a
checkbox. label adds a label without a description;
component takes a LabelBuilder with one.
The rules come from Discord’s documentation only: a modal cannot be sent without a user’s interaction, so they were not tried on the live API (D-200).
discord.js counterpart: ModalBuilder. Unlike discord.js v14, action rows with text inputs
(the deprecated format) are rejected (D-207).
§Examples
use djs_builders::{
FileUploadBuilder, LabelBuilder, ModalBuilder, RadioGroupBuilder, TextInputBuilder,
};
let modal = ModalBuilder::new("feedback", "Your feedback")
.text("Thanks for helping!")
.label("Topic", TextInputBuilder::short("topic").max_length(80))
.component(
LabelBuilder::new("Details", TextInputBuilder::paragraph("body").required(false))
.description("Optional"),
)
.label(
"Rating",
RadioGroupBuilder::new("rating").option("Good", "good").option("Bad", "bad"),
)
.label("Screenshots", FileUploadBuilder::new("shots").max_values(3))
.build()?;
assert_eq!(modal.components.len(), 5);Implementations§
Source§impl ModalBuilder
impl ModalBuilder
Sourcepub fn new(
custom_id: impl Into<CompactString>,
title: impl Into<CompactString>,
) -> Self
pub fn new( custom_id: impl Into<CompactString>, title: impl Into<CompactString>, ) -> Self
Creates a modal sending custom_id (1-100 characters) back when submitted, titled
title (1-45 characters). It needs 1-5 labels and texts.
discord.js counterpart: new ModalBuilder().setCustomId(…).setTitle(…).
Sourcepub fn label(
self,
label: impl Into<CompactString>,
field: impl Into<Component>,
) -> Self
pub fn label( self, label: impl Into<CompactString>, field: impl Into<Component>, ) -> Self
Adds a label label (1-45 characters) around field, without a description.
discord.js counterpart: addLabelComponents.
Sourcepub fn text(self, content: impl Into<CompactString>) -> Self
pub fn text(self, content: impl Into<CompactString>) -> Self
Adds a text display showing content (markdown, 1-4000 characters).
discord.js counterpart: addTextDisplayComponents.
Sourcepub fn component(self, component: impl Into<Component>) -> Self
pub fn component(self, component: impl Into<Component>) -> Self
Adds a component after those already added: a LabelBuilder, a text display, or a
djs-model [Component] of those types.
discord.js counterpart: addLabelComponents / addTextDisplayComponents.
Sourcepub fn build(self) -> Result<ModalCallbackData, ValidationError>
pub fn build(self) -> Result<ModalCallbackData, ValidationError>
Checks the modal and everything in it, and returns it ready for
InteractionResponse::Modal.
discord.js counterpart: ModalBuilder#toJSON.
§Errors
The first ValidationError found: the custom_id, the title, the number and types
of components, each label and field (paths such as components[1].component.max_length),
then repeated custom_ids.
Sourcepub fn build_unchecked(self) -> ModalCallbackData
pub fn build_unchecked(self) -> ModalCallbackData
Returns the modal without any check (D-180) — also the way to send the deprecated action-row format (D-207).
discord.js counterpart: ModalBuilder#toJSON with validation off (disableValidators).