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 fs from "fs";
import path, { parse } from "path"; import path, { parse } from "path";
import { Client, GatewayIntentBits, Partials } from "discord.js"; import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
const { const {
TOKEN, TOKEN,
GUILD_ID, GUILD_ID,
@ -27,6 +27,20 @@ const client = new Client({
partials: [Partials.Channel, Partials.Message], 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 backticks = "```";
const rankingsPath = path.join(__dirname, "rankings.json"); const rankingsPath = path.join(__dirname, "rankings.json");
@ -791,6 +805,19 @@ client.on("interactionCreate", async (interaction) => {
}) })
.catch(console.error); .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"; import { REST, Routes } from "discord.js";
const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json"); const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json");
const fs = require("fs");
const path = require("path");
// set static commands
const commands = [ const commands = [
{ {
name: "hello", name: "hello",
@ -26,10 +29,10 @@ const commands = [
name: "fk", name: "fk",
description: "Moves added players to picking and randomly selects fatkids", description: "Moves added players to picking and randomly selects fatkids",
}, },
{ /*{
name: "testfk", name: "testfk",
description: "debug fk", description: "debug fk",
}, },*/
{ {
name: "fklist", name: "fklist",
description: "Pulls addup channel members into fk channel and lists them", 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); const rest = new REST({ version: "10" }).setToken(TOKEN);
try { try {