Skip to main content

ModalBuilder

Struct ModalBuilder 

Source
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

Source

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(…).

Source

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.

Source

pub fn text(self, content: impl Into<CompactString>) -> Self

Adds a text display showing content (markdown, 1-4000 characters).

discord.js counterpart: addTextDisplayComponents.

Source

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.

Source

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.

Source

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).

Trait Implementations§

Source§

impl Clone for ModalBuilder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModalBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ModalBuilder

Source§

impl PartialEq for ModalBuilder

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ModalBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.