← Back to lessons

Lesson 8 — Modals

Goal: Open a modal (form) and read what the user typed.

Video

Step-by-step

Step 1 — Create a command to open a modal

Create commands/feedback.js

const { SlashCommandBuilder } = require("@discordjs/builders");
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require("discord.js");

module.exports = {
  dev: true,
  data: new SlashCommandBuilder().setName("feedback").setDescription("Open a feedback form"),
  async execute(interaction) {
    const modal = new ModalBuilder().setCustomId("feedback_modal").setTitle("Feedback");

    const input = new TextInputBuilder()
      .setCustomId("feedback_text")
      .setLabel("What should we improve?")
      .setStyle(TextInputStyle.Paragraph)
      .setRequired(true);

    modal.addComponents(new ActionRowBuilder().addComponents(input));
    await interaction.showModal(modal);
  },
};
Step 2 — Create the modal handler

Create modals/feedback_modal.js

module.exports = {
  customID: "feedback_modal",
  async execute(interaction) {
    const text = interaction.fields.getTextInputValue("feedback_text");
    await interaction.reply({ content: `Thanks! You said: ${text}`, ephemeral: true });
  }
};
Step 3 — Restart & test
node index.js

Run /feedback, fill the form, submit.

← Lesson 7 Next → Lesson 9