diff --git a/src/commands/utility/ping.ts b/src/commands/utility/ping.ts index de52178..86f955b 100644 --- a/src/commands/utility/ping.ts +++ b/src/commands/utility/ping.ts @@ -1,10 +1,13 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js"; +import { CommandDefinition } from "../../type"; -export default { +const cmd : CommandDefinition = { data: new SlashCommandBuilder() .setName('ping') .setDescription('Pong again!'), async execute(interaction : ChatInputCommandInteraction) { await interaction.reply('Pong !'); } -} \ No newline at end of file +} + +export default cmd; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index b102b8b..e4fb9cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from "dis import { configDotenv } from "dotenv"; import path from "path"; import fs from "fs"; +import { CommandDefinition } from "./type"; configDotenv(); @@ -14,8 +15,7 @@ const client = new Client({ ] }); -// @ts-expect-error -client.commands = new Collection(); +const commands = new Collection(); const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); @@ -28,8 +28,7 @@ for (const folder of commandFolders) { const command = require(filePath); const commandModule = command.default || command; if ('data' in commandModule && 'execute' in commandModule) { - // @ts-expect-error - client.commands.set(commandModule.data.name, commandModule); + commands.set(commandModule.data.name, commandModule); } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } @@ -37,10 +36,23 @@ for (const folder of commandFolders) { } client.on(Events.InteractionCreate, async interaction => { + + if(interaction.isButton()) { + const id = interaction.customId; + commands.forEach((value) => { + if(value.buttons) { + const button = value.buttons.filter((b) => b.id == id); + if(button.length == 1) { + button[0]?.handle(interaction); + } + } + }); + return; + } + if (!interaction.isChatInputCommand()) return; - // @ts-expect-error - const command = interaction.client.commands.get(interaction.commandName); + const command = commands.get(interaction.commandName); if (!command) { console.error(`No command matching ${interaction.commandName} was found.`); diff --git a/src/type.d.ts b/src/type.d.ts new file mode 100644 index 0000000..9dd4278 --- /dev/null +++ b/src/type.d.ts @@ -0,0 +1 @@ +export type CommandDefinition = { data: SlashCommandBuilder, execute: (interaction: ChatInputCommandInteraction) => void, buttons?: { id: string, handle: (interaction: ButtonInteraction) => void}[]}; \ No newline at end of file