feat(command): enhance ping command with button interaction and improved response format

This commit is contained in:
2025-10-29 15:36:45 +01:00
parent 86a7429711
commit 7f9468bc99
2 changed files with 51 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from "discord.js";
import { ButtonInteraction, ChatInputCommandInteraction, Client, Collection, Events, GatewayIntentBits, MessageFlags, SlashCommandBuilder } from "discord.js";
import { configDotenv } from "dotenv";
import path from "path";
import fs from "fs";
@@ -24,8 +24,7 @@ const client = new Client({
]
});
// @ts-expect-error
client.commands = new Collection();
const commands = new Collection<string, {data: SlashCommandBuilder, execute: (interaction: ChatInputCommandInteraction) => void, buttons?: {id: string, handle: (interaction : ButtonInteraction) => void}[]}>();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
@@ -38,8 +37,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.`);
}
@@ -47,10 +45,26 @@ for (const folder of commandFolders) {
}
client.on(Events.InteractionCreate, async interaction => {
if(interaction.isButton()) {
const id = interaction.customId;
commands.forEach((value, key) => {
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.`);