npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zerotwobot/discord-modals

v2.0.1

Published

@zerotwobot/discord-modals is a library for creating and displaying modals in Discord.

Downloads

65

Readme

this is a modified fork of discord-modals with a few bug fixes, and to make it work better with ZeroTwo's framework.

installation

npm install @zerotwobot/discord-modals

🔮 What is this package for?

Recently, Discord 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. However, discord.js hasn't added it yet. @zerotwobot/discord-modals can be a solution if you want to test or use modals right now in v13. Try it!

✨ Setup

const { Client } = require('discord.js'); // Extract the Client class
const client = new Client({ intents: 32767 }); // Create a Client
const discordModals = require('@zerotwobot/discord-modals'); // Define the @zerotwobot/discord-modals package!
discordModals(client); // @zerotwobot/discord-modals needs your client in order to interact with modals

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

❓ 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 (Text Input)

Text Input 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 place holder

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

📜 Examples

If you are ready, take this examples.

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

const modal = new Modal() // We create a Modal
  .setCustomId('customid')
  .setTitle('Test of @zerotwobot/discord-modals!')
  .addComponents();

This is a basic structure of a Modal, but something is missing. Yeah! Text Input components.

  • We are going to create and add a Text Input Component to the Modal.
const { Modal, TextInputComponent } = require('@zerotwobot/discord-modals'); // Modal and TextInputComponent class

const modal = new Modal() // We create a Modal
  .setCustomId('modal-customid')
  .setTitle('Test of @zerotwobot/discord-modals!')
  .addComponents(
    new TextInputComponent() // We create a Text Input Component
      .setCustomId('textinput-customid')
      .setLabel('Some text Here')
      .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG'
      .setMinLength(4)
      .setMaxLength(10)
      .setPlaceholder('Write a text here')
      .setRequired(true) // If it's required or not
  );

Yay! We have the full Modal & Text Input Component, 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,
  showModal,
} = require('@zerotwobot/discord-modals'); // Now we extract the showModal method

const modal = new Modal() // We create a Modal
  .setCustomId('modal-customid')
  .setTitle('Test of @zerotwobot/discord-modals!')
  .addComponents(
    new TextInputComponent() // We create a Text Input Component
      .setCustomId('textinput-customid')
      .setLabel('Some text Here')
      .setStyle('SHORT') //IMPORTANT: Text Input Component Style can be 'SHORT' or 'LONG'
      .setMinLength(4)
      .setMaxLength(10)
      .setPlaceholder('Write a text here')
      .setRequired(true) // If it's required or not
  );

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

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

📢 Events: Receiving Modal Submit Interaction

  • @zerotwobot/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.

Reply Examples

  • Usual Reply:
client.on('modalSubmit', (modal) => {
  if (modal.customId === 'modal-customid') {
    const firstResponse = modal.getTextInputValue('textinput-customid');
    modal.reply(
      'Congrats! Powered by @zerotwobot/discord-modals.' +
        `\`\`\`${firstResponse}\`\`\``
    );
  }
});
  • Ephemeral Reply:
client.on('modalSubmit', (modal) => {
  if (modal.customId === 'modal-customid') {
    const firstResponse = modal.getTextInputValue('textinput-customid');
    await modal.deferReply({ ephemeral: true });
    modal.followUp({
      content:
        'Congrats! Powered by @zerotwobot/discord-modals.' +
        `\`\`\`${firstResponse}\`\`\``,
      ephemeral: true,
    });
  }
});

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

Final Result

📚 Documentation

  • Check our documentation here.

🔨 Developers

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

⛔ Issues/Bugs?

Please report it on our GitHub Repository here to fix it inmmediately.