From ae3a1be106c6aa824eb0a82263515f960b9fc947 Mon Sep 17 00:00:00 2001 From: ethanf Date: Thu, 1 Feb 2024 16:08:24 -0600 Subject: [PATCH] feat: roll command with file command handlers --- commands/roll.js | 20 ++++++++++++++++++++ index.js | 29 ++++++++++++++++++++++++++++- register.js | 20 ++++++++++++++++++-- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 commands/roll.js diff --git a/commands/roll.js b/commands/roll.js new file mode 100644 index 0000000..282ccb4 --- /dev/null +++ b/commands/roll.js @@ -0,0 +1,20 @@ +import { SlashCommandBuilder } from "discord.js"; + +const data = new SlashCommandBuilder() + .setName("roll") + .setDescription("Prints a random number between 1 and input") + .addIntegerOption((option) => + option + .setName("max") + .setDescription("Maximum output number") + .setRequired(true) + .setMinValue(1) + ); + +const execute = async (interaction) => { + const max = interaction.options.getInteger("max"); + const roll = Math.floor(Math.random() * max) + 1; + await interaction.reply(`🎲 Rolled a ${roll} (max: ${max})`); +}; + +export { data, execute }; diff --git a/index.js b/index.js index f3792fa..50f8deb 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ import fs from "fs"; import path, { parse } from "path"; -import { Client, GatewayIntentBits, Partials } from "discord.js"; +import { Client, Collection, GatewayIntentBits, Partials } from "discord.js"; const { TOKEN, GUILD_ID, @@ -27,6 +27,20 @@ const client = new Client({ partials: [Partials.Channel, Partials.Message], }); +client.fileCommands = new Collection(); + +const commandsPath = path.join(__dirname, "commands"); +const commandFiles = fs.readdirSync(commandsPath); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + if ("data" in command && "execute" in command) { + client.fileCommands.set(command.data.name, command); + } else { + console.error(`${__filename}: Invalid command file: ${file}`); + } +} + const backticks = "```"; const rankingsPath = path.join(__dirname, "rankings.json"); @@ -791,6 +805,19 @@ client.on("interactionCreate", async (interaction) => { }) .catch(console.error); } + + const fileCommand = client.fileCommands.get(command); + if (!fileCommand) return; + + try { + await fileCommand.execute(interaction); + } catch (error) { + console.error(error); + await interaction.reply({ + content: "Internal error executing command from file", + ephemeral: true, + }); + } }); /* diff --git a/register.js b/register.js index 4bd56d3..80a2044 100644 --- a/register.js +++ b/register.js @@ -1,6 +1,9 @@ import { REST, Routes } from "discord.js"; const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json"); +const fs = require("fs"); +const path = require("path"); +// set static commands const commands = [ { name: "hello", @@ -26,10 +29,10 @@ const commands = [ name: "fk", description: "Moves added players to picking and randomly selects fatkids", }, - { + /*{ name: "testfk", description: "debug fk", - }, + },*/ { name: "fklist", description: "Pulls addup channel members into fk channel and lists them", @@ -100,6 +103,19 @@ const commands = [ }, ]; +// get commands from files +const commandsPath = path.join(__dirname, "commands"); +const commandFiles = fs.readdirSync(commandsPath); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`); + if ("data" in command && "execute" in command) { + commands.push(command.data.toJSON()); + } else { + console.error(`${__filename}: Invalid command file: ${file}`); + } +} + const rest = new REST({ version: "10" }).setToken(TOKEN); try {