Skip to main content

@ModalComponent

add modal interaction handler for your bot using @ModalComponent decorator

Here are some example screenshots:

Signature

@ModalComponent(options: ComponentOptions)

Example

@Discord()
class Example {
@Slash({ description: "modal" })
modal(interaction: CommandInteraction): void {
// Create the modal
const modal = new ModalBuilder()
.setTitle("My Awesome Form")
.setCustomId("AwesomeForm");

// Create text input fields
const tvShowInputComponent = new TextInputBuilder()
.setCustomId("tvField")
.setLabel("Favorite TV show")
.setStyle(TextInputStyle.Short);

const haikuInputComponent = new TextInputBuilder()
.setCustomId("haikuField")
.setLabel("Write down your favorite haiku")
.setStyle(TextInputStyle.Paragraph);

const row1 = new ActionRowBuilder<TextInputBuilder>().addComponents(
tvShowInputComponent,
);

const row2 = new ActionRowBuilder<TextInputBuilder>().addComponents(
haikuInputComponent,
);

// Add action rows to form
modal.addComponents(row1, row2);

// --- snip ---

// Present the modal to the user
interaction.showModal(modal);
}

@ModalComponent()
async AwesomeForm(interaction: ModalSubmitInteraction): Promise<void> {
const [favTVShow, favHaiku] = ["tvField", "haikuField"].map((id) =>
interaction.fields.getTextInputValue(id),
);

await interaction.reply(
`Favorite TV Show: ${favTVShow}, Favorite haiku: ${favHaiku}`,
);

return;
}
}