Refactor command handling to use CommandDefinition type and improve interaction processing
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||||
|
import { CommandDefinition } from "../../type";
|
||||||
|
|
||||||
export default {
|
const cmd : CommandDefinition = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('ping')
|
.setName('ping')
|
||||||
.setDescription('Pong again!'),
|
.setDescription('Pong again!'),
|
||||||
async execute(interaction : ChatInputCommandInteraction) {
|
async execute(interaction : ChatInputCommandInteraction) {
|
||||||
await interaction.reply('Pong !');
|
await interaction.reply('Pong !');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default cmd;
|
||||||
24
src/index.ts
24
src/index.ts
@@ -2,6 +2,7 @@ import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from "dis
|
|||||||
import { configDotenv } from "dotenv";
|
import { configDotenv } from "dotenv";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import { CommandDefinition } from "./type";
|
||||||
|
|
||||||
configDotenv();
|
configDotenv();
|
||||||
|
|
||||||
@@ -14,8 +15,7 @@ const client = new Client({
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
// @ts-expect-error
|
const commands = new Collection<string, CommandDefinition>();
|
||||||
client.commands = new Collection();
|
|
||||||
|
|
||||||
const foldersPath = path.join(__dirname, 'commands');
|
const foldersPath = path.join(__dirname, 'commands');
|
||||||
const commandFolders = fs.readdirSync(foldersPath);
|
const commandFolders = fs.readdirSync(foldersPath);
|
||||||
@@ -28,8 +28,7 @@ for (const folder of commandFolders) {
|
|||||||
const command = require(filePath);
|
const command = require(filePath);
|
||||||
const commandModule = command.default || command;
|
const commandModule = command.default || command;
|
||||||
if ('data' in commandModule && 'execute' in commandModule) {
|
if ('data' in commandModule && 'execute' in commandModule) {
|
||||||
// @ts-expect-error
|
commands.set(commandModule.data.name, commandModule);
|
||||||
client.commands.set(commandModule.data.name, commandModule);
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
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 => {
|
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;
|
if (!interaction.isChatInputCommand()) return;
|
||||||
|
|
||||||
// @ts-expect-error
|
const command = commands.get(interaction.commandName);
|
||||||
const command = interaction.client.commands.get(interaction.commandName);
|
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
console.error(`No command matching ${interaction.commandName} was found.`);
|
||||||
|
|||||||
1
src/type.d.ts
vendored
Normal file
1
src/type.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export type CommandDefinition = { data: SlashCommandBuilder, execute: (interaction: ChatInputCommandInteraction) => void, buttons?: { id: string, handle: (interaction: ButtonInteraction) => void}[]};
|
||||||
Reference in New Issue
Block a user