index.js - // Require necessary modules const fs = require('fs'); // File system module for reading files const { Client, Collection, GatewayIntentBits } = require('discord.js'); // Discord.js modules for bot functionality require('dotenv').config(); // Load environment variables from .env file const path = require('path'); // Module for working with file and directory paths // Create a new Discord bot client const client = new Client({ intents: [GatewayIntentBits.Guilds] }); // Define bot intents for events // Collection to store bot commands client.commands = new Collection(); // Define the path to the commands folder const foldersPath = path.join(__dirname, 'commands'); // Read all folders within the commands directory const commandFolders = fs.readdirSync(foldersPath); // Loop through each folder to load commands for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); // Filter and retrieve JavaScript files from the folder const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); // Load each command file for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); // Check if the command has required properties if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); // Store the command in the collection } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } } }; // Event handler for when the bot is ready client.once('ready', () => { console.log(`Ready! Logged in as ${client.user.tag}`); }); // Event handler for interaction (slash commands) client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; // Check if it's a slash command // Retrieve the command based on the command name const command = client.commands.get(interaction.commandName); if (!command) { console.error(`No command matching ${interaction.commandName} was found.`); return; } try { await command.execute(interaction); // Execute the command } catch (error) { console.error(error); // Respond with an error message if command execution fails await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } }); //Modal Code Below const { ActionRowBuilder, Events, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js'); client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === 'ping') { // Create the modal const modal = new ModalBuilder() .setCustomId('createItem') .setTitle('Create Item'); // Create the text input components const itemNameInput = new TextInputBuilder() .setCustomId('itemNameInput') // The label is the prompt the user sees for this input .setLabel("What's the name of the Item?") // Short means only a single line of text .setStyle(TextInputStyle.Short); const itemDescInput = new TextInputBuilder() .setCustomId('itemDescInput') .setLabel("What is the description of the item?") // Paragraph means multiple lines of text. .setStyle(TextInputStyle.Paragraph); // An action row only holds one text input, // so you need one action row per text input. const firstActionRow = new ActionRowBuilder().addComponents(itemNameInput); const secondActionRow = new ActionRowBuilder().addComponents(itemDescInput); // Add inputs to the modal modal.addComponents(firstActionRow, secondActionRow); // Show the modal to the user await interaction.showModal(modal); } }); client.on(Events.InteractionCreate, interaction => { if (!interaction.isModalSubmit()) return; // Get the data entered by the user const itemName = interaction.fields.getTextInputValue('itemNameInput'); const itemDesc = interaction.fields.getTextInputValue('itemDescInput'); console.log({ itemName, itemDesc }); }); // Log in to Discord with the bot's token client.login(process.env.TOKEN); ping.js in bot/commands const { SlashCommandBuilder } = require('discord.js'); // Import the SlashCommandBuilder from Discord.js module.exports = { // Define the command data using SlashCommandBuilder data: new SlashCommandBuilder() .setName('ping') // Set the command name .setDescription('Replies with I heard you, Pong!'), // Set the command description // Execute function that runs when the command is invoked async execute(interaction) { // Reply to the interaction with 'I heard you, Pong!' await interaction.reply('I heard you, Pong!'); }, };