From a31234b717858e97bd214b4e0f655b5b930d0386 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 22 Apr 2023 23:08:04 +0200 Subject: [PATCH] adding discord modal example --- commands/Info/modalExample.js | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 commands/Info/modalExample.js diff --git a/commands/Info/modalExample.js b/commands/Info/modalExample.js new file mode 100644 index 0000000..9b136c7 --- /dev/null +++ b/commands/Info/modalExample.js @@ -0,0 +1,66 @@ +const { EmbedBuilder } = require('discord.js'); +const { ActionRowBuilder, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js'); + +module.exports = { + name: "modal-example", + description: "Show a demo modal!", + + run: async (client, interaction) => { + + // Declare a random var for the main modal - Each Session + let rand = Math.floor(Math.random() * 99999999999999).toString(); + + // Check if this is a chatInput + if (!interaction.isChatInputCommand()) return; + + // await interaction.deferReply(); + const modal = new ModalBuilder() + .setCustomId(rand) + .setTitle('This is an example modal'); + + // TODO: Add components to modal... + const modalInputData = new TextInputBuilder() + .setCustomId('modalInput') + // The label is the prompt the user sees for this input + .setLabel("What text do you want to send?") + // Short means only a single line of text + .setStyle(TextInputStyle.Paragraph); + + // An action row only holds one text input, + // so you need one action row per text input. + const modalInputRow = new ActionRowBuilder().addComponents([modalInputData]); + + // Add inputs to the modal + modal.addComponents([modalInputRow]); + + await interaction.showModal(modal); + + client.on('interactionCreate', interaction => { + + // Do not continue if its a modal + if (interaction.type == "modal") return + // Interaction type is 5 == Modal + if (interaction.type === 5) { + + // Make sure we are working with our users modal only + if (interaction.customId === rand) { + + // Get the data entered by the user + let modalInputDataString = interaction.fields.getTextInputValue('modalInput'); + + console.log(modalInputDataString) + + const embed = new EmbedBuilder() + // Set color to blue + .setColor("#FF0000") + .setTitle("Your input!") + .setDescription(`You said: ${modalInputDataString}`) + .setTimestamp() + .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` }); + interaction.reply({ embeds: [embed] }); + } + } + }) + + } +}