feat: roll command with file command handlers

This commit is contained in:
ethanf 2024-02-01 16:08:24 -06:00
parent 8fd097ba67
commit ae3a1be106
3 changed files with 66 additions and 3 deletions

20
commands/roll.js Normal file
View File

@ -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 };

View File

@ -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,
});
}
});
/*

View File

@ -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 {