Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

discord-modals

Mateo-tem3.2kMITdeprecated1.3.9TypeScript support: included

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

Discord-Modals is a package that allows your discord.js v13 and v14 bot to create, and interact with Modals, a new Discord feature.

discord.js, modals, interaction modals, modal interactions, forms, modal form, discord-forms, discord-modals, discord, discord.js-modals, discord.js-forms

readme

A package that allows your discord.js v13 and v14 bot to create, and interact with Modals, a new Discord feature.

🔎 Installation

npm install discord-modals
yarn add discord-modals

🔮 What is this package for?

Recently, Discord API officialy announced Modal Interactions.

What is that? Modal is a popup of Text Input Components [Example]. It's so cool and useful for many commands that needs arguments. Discord-Modals can be a solution if you want to test or use Modals right now. Supports discord.js v13 and v14. Try it!

✨ Setup

The most recommended is to put this on your main file.

const { Client } = require('discord.js'); // Get the Client class
const client = new Client(); // Create a Discord Client, with intents and what you need
const discordModals = require('discord-modals'); // Define the discord-modals package!
discordModals(client); // discord-modals needs your client in order to interact with modals

client.login('token'); // Login with your bot token

Important: Don't forget to put discordModals(client), will be essential to receive the Modal Submit Interaction.

❓ How can i use it?

First of all, we need to understand that Modals and Text Input Components are completely different. Modals is a popup that shows the text input components and text input are the components of modals. To understand better, you can explore the Discord API Documentation here.

Modals have:

  • A Title
  • A Custom Id
  • Components (Action Rows with Text Inputs)

Text Inputs have:

  • A Custom Id
  • A Style (Short or Paragraph)
  • A Label
  • A minimum length
  • A maximum length
  • A value (A prefilled value if there is not text)
  • And...a placeholder

If you have understood this, you can continue on "Examples" section.

Important: Modals also support select menus. So, you need to know the select menu structure.

📜 Examples

If you are ready, take this examples.

  • First, we are going to create a Modal.
const { Modal } = require('discord-modals'); // Modal class

const modal = new Modal() // We create a Modal
    .setCustomId('modal-customid')
    .setTitle('Modal')
    .addComponents();

This is a basic structure of a Modal, but something is missing. Yeah! Components.

  • We are going to create and add a Text Input Component and a Select Menu to the Modal.
const { Modal, TextInputComponent, SelectMenuComponent } = require('discord-modals'); // Import all

const modal = new Modal() // We create a Modal
    .setCustomId('modal-customid')
    .setTitle('Modal')
    .addComponents(
        new TextInputComponent() // We create a Text Input Component
            .setCustomId('name')
            .setLabel('Name')
            .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG'
            .setPlaceholder('Write your name here')
            .setRequired(true), // If it's required or not

        new SelectMenuComponent() // We create a Select Menu Component
            .setCustomId('theme')
            .setPlaceholder('What theme of Discord do you like?')
            .addOptions(
                {
                    label: 'Dark',
                    description: 'The default theme of Discord.',
                    value: 'dark',
                    emoji: '⚫',
                },
                {
                    label: 'Light',
                    description: 'Some people hate it, some people like it.',
                    value: 'light',
                    emoji: '⚪',
                },
            ),
    );

Yay! We have the full Modal, but... How can i send/show a Modal?

  • We are going to use the showModal() method to send the modal in an interaction.
const { Modal, TextInputComponent, SelectMenuComponent, showModal } = require('discord-modals'); // Import all

const modal = new Modal() // We create a Modal
    .setCustomId('modal-customid')
    .setTitle('Modal')
    .addComponents(
        new TextInputComponent() // We create a Text Input Component
            .setCustomId('name')
            .setLabel('Name')
            .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG'
            .setPlaceholder('Write your name here')
            .setRequired(true), // If it's required or not

        new SelectMenuComponent() // We create a Select Menu Component
            .setCustomId('theme')
            .setPlaceholder('What theme of Discord do you like?')
            .addOptions(
                {
                    label: 'Dark',
                    description: 'The default theme of Discord.',
                    value: 'dark',
                    emoji: '⚫',
                },
                {
                    label: 'Light',
                    description: 'Some people hate it, some people like it.',
                    value: 'light',
                    emoji: '⚪',
                },
            ),
    );

client.on('interactionCreate', (interaction) => {
    // Let's say the interaction will be a Slash Command called 'ping'.
    if (interaction.commandName === 'ping') {
        showModal(modal, {
            client: client, // Client to show the Modal through the Discord API.
            interaction: interaction, // Show the modal with interaction data.
        });
    }
});

Congrats! You show the Modal to the Interaction User. Now, how can i receive the Modal Interaction?

📢 Events: Receiving Modal Submit Interaction and extracting data

  • discord-modals integrates to your Client a new event called modalSubmit. We are going to use it.
  • To have access to the responses, just use the .getTextInputValue() method with the Custom Id of the Text Input Component, or if you use Select Menus, the same way, use the .getSelectMenuValues() method with the Custom Id of the Select Menu Component.

Recommendation: Put your modalSubmit event on your main file or in an Event Handler.

client.on('modalSubmit', async (modal) => {
    if (modal.customId === 'modal-customid') {
        const nameResponse = modal.getTextInputValue('name');
        const themeResponse = modal.getSelectMenuValues('theme');
        modal.reply(
            `Thank you for answering the form! Powered by discord-modals.\nSo, you are **${nameResponse}** and you like the **${themeResponse}** theme. Awesome!`,
        );
    }
});

And you made it! I hope this examples help you :)

Modal Test

📚 Documentation

  • Check our documentation here.

❓ FAQ

Can I show a modal, replying to a modal?

  • No, Discord API don't support that, but there are plans to add that.

Can I add buttons/select menus to modals?

  • Yes, but only select menus are supported at this moment. Discord API have plans to add other type of components. Be patient :)

Can I show a modal in a message?

  • No, modals are only for interactions (Select Menus, Slash Commands, Buttons and Context Menu Commands). They are a response for interactions.

Can I add more than 5 Components in a modal?

  • No, a modal supports 5 Action Rows (containing 1 Text Input Component or Select Menu per row). There is no plans in Discord API to increase the amount of rows per modal.

DiscordAPIError: Interaction has already been acknowledged.

  • The showModal() method is a response of an interaction. also reply() or deferReply() are responses of an interaction. If you give a response, you can't give a response again. So you need to remove reply() or editReply() on your code.

  • If that doesn't work, probably you have the modalSubmit event on the same file of your command, please just add it to a event handler or just separate it.

The Select Menu in Modal is not being displayed in mobile/phone.

  • Discord is still developing it, so the select menu is not being displayed in this type of clients. For now, only works in desktop/web client. Use it at your own risk.

🔨 Developers

  • 『𝑴𝒂𝒕𝒆𝒐ᵗᵉᵐ』#9999

⛔ Issues/Bugs?

Please report it on our GitHub Repository here to fix it inmmediately or join to the support server.

Credits: This package is based on discord.js, code base was extracted for this.

changelog

🎈 Discord-Modals Change log

Hello! This is the Change log section of Discord-Modals. Here you can see what things have been changed in the different versions, to be informed. This will be updated every time there are new versions. I hope you enjoy this package :)

v1.3.9 (Stable)

  • Fix: Table issues in docs.
  • Fix: mistake exporting Interaction in v14.

v1.3.8

Sometimes we have this proyect discontinued, many people ask if this package can be marked as deprecated, because of discord.js v13 and discord.js v14, which already have this modal feature. What do you think? 🤔

Use the discussion in our GitHub repository: Discussion #52.

  • Fix: discord.js v14 util error at 'verifyString'.
  • Fix: "Cannot read properties of undefined (reading '0')" error inside modal structure.
  • Style: Improve style in files and docs.

v1.3.7

Surprise! We are back.

Featured: Discord Developers announced Select Menus in modals, they are about to finish to develop it, but you can use it now (at your own risk). So, now Discord-Modals supports Select Menus in modals! 🎉

  • Chore: Support Select Menus in Modals.
  • Chore: Some improvements to FAQ.
  • Chore: [Select Menus] Update readme to new examples.
  • Chore: [Select Menus] Update docs.

v1.3.6

Probably this will be the final version of Discord-Modals. Thank you so much for all the support!

Why? discord.js just released modals on v13.7 and in v14 (dev - unstable). In a very short time, this package will no longer make much sense when it already exists (on all the actual versions).

  • Fix: Ephemeral option when replying to the Modal Submit Interaction.
  • Add FAQ to readme.

v1.3.5

  • Style and Code Cleanup: All files.
  • Chore: Add eslint.

v1.3.4

  • Fix: JSON Modals on .showModal() didn't work.

v1.3.3

  • TypeScript Compatibility: Add .init() method.
  • Fix: Readme example type error .addComponents() on Modal class.

v1.3.2

  • Compatibility: change discord-api-types enums from v10 to v9.
  • Modal and ModalSubmitInteraction class: Now the .components property returns ModalActionRows.
  • Code cleanup.

v1.3.1

  • Fix: change discord-api-types enums from v9 to v10.

v1.3.0

  • Fix bug: 'INVALID_CLIENT' error when the Client is valid on .showModal() method.
  • Now the TextInputComponent returns an Action Row Component. Add ModalActionRow class and types.
  • Featured: Now the .showModal() method supports JSON Modals.
  • Fix error typings ModalSubmitField.
  • Featured: Implement .update() method to ModalSubmitInteraction class.
  • Fix types: .showModal() method, .addComponents() and .setComponents() of Modal class.
  • Typings: discord-api-types updated to v10.

v1.2.9 (Stable)

  • Fix: Add types for reply options on ModalSubmitInteraction class.

v1.2.8

  • Fix: Add setPlaceholder type definition missing of TextInputComponent class.

v1.2.7

  • Fix types in the declaration file.
  • Align documentation with typings.
  • Fix bug (Compatibility with discord.js v14)
  • Fix examples issues (Readme)

v1.2.6

  • Add types of discord-api-types/v9 in the declaration file.
  • Add .isRepliable() and .isFromMessage() methods to ModalSubmitInteraction class.
  • Add discord-modals to yarn.
  • Update Readme and docs (yarn).

v1.2.5

  • Fix and add missing types in declaration file.
  • Update Readme (Aspect: Example Code Block Formatter).
  • package.json fixes.

v1.2.4

🎉 Important Update:

  • Add examples to constructors of the Classes.
  • Fix error of ModalSubmitField class, if the field has not a valid value, before returns a empty string, now returns null.
  • Featured: Add discord.js v14 (Dev) Support.
  • Featured: Add TypeScript Support.
  • Update documentation (djs v14).
  • Update Readme (djs v14).

v1.2.3

  • Change TextInputComponent class method .setValue() to .setDefaultValue().
  • Documentation: Add limits and specifications to classes and update TextInputComponent class.

v1.2.2

  • Add recomendations on readme.
  • Add .message property to ModalSubmitInteraction class. Some code cleanup there.
  • Add types to Classes properties.
  • Documentation: Add ModalSubmitInteraction class properties and add Errors codes & messages.
  • Fix the 'INTERACTION_REQUIRED' error message.

v1.2.0 & v1.2.1

  • Fix issue of errors.

v1.1.9

  • Add examples to classes. More comprehension.
  • Improvements to documentation and fix errors.
  • More organization in errors.
  • Update readme.

v1.1.8

  • Fix compatibility issues. Now the package throws an error if you have a different version than discord.js v13 to avoid confusions.
  • Update readme.

v1.1.7

  • Fix .getTextInputValue() & .getField() method of ModalSubmitInteraction class. Now if the customId is incorrect throws null.

v1.1.6

  • Add Errors directory and files.
  • Fix ModalSubmitInteraction#type bug. Before returns 5, now returns 'MODAL_SUBMIT' type.
  • Fix .toJSON() method of TextInputComponent class. Previously did not return the value.
  • Fix .getTextInputValue() method of ModalSubmitInteraction class. Now if the customId is incorrect throws null.
  • A little bit of code cleanup & maintenance.

v1.1.5

  • Add Changelog file.
  • Finish docs file.
  • Add ModalSubmitField class, add ModalSubmitInteraction#fields property and add .getField() and .getTextInputValue() methods to ModalSubmitInteraction class.
  • Update Readme with new Examples and fix typos.

v1.1.4

  • Fix ephemeral reply bug
  • Add reply examples to readme.

v1.1.3

  • Update readme (Only compatible on discord.js v13)

v1.1.2

  • Fix ModalSubmitInteraction#guild bug (undefined).

v1.1.1

  • Fix ModalSubmitInteraction#components bug.
  • Update readme with new examples.
  • Add Docs file.

Powered by Discord-Modals