DiscordJS-v14-Template/commands/Info/modalExample.js
2023-04-22 23:08:04 +02:00

67 lines
2.4 KiB
JavaScript

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] });
}
}
})
}
}