logo

Constatic

Discord Bot BaseResponders

Modals

How to reply discord modals

Creating a Responder for Modals

Below you can see a code snippet responding to a command interaction by showing a modal:

command.ts
import { createModalFields } from "@magicyan/discord";
// ...
interaction.showModal({
    customId: "form/modal",
    title: "Form",
    components: createModalFields({
        name:{
            label: "What's your name?",
            style: TextInputStyle.Short
        },
        age:{
            label: "What's your age?",
            style: TextInputStyle.Short
        },
    })
});
// ...

Reply to modal interaction

To reply to a modal with Responder, add ResponderType.Modal to the types:

responder.ts
createResponder({
    customId: "form/modal",

    types: [ResponderType.Modal], cache: "cached",
    async run(interaction) {
        const { fields, member } = interaction;
        const name = fields.getTextInputValue("name");
        const age = fields.getTextInputValue("age");
 
        await registerMember(member, { name, age }); // Example function
 
        interaction.reply({ flags: ["Ephemeral"], content: `Registered as ${name}` });
    },
});

On this page