Improve typing.
All checks were successful
Build and Publish Docker Image / build_and_push (push) Successful in 42s
All checks were successful
Build and Publish Docker Image / build_and_push (push) Successful in 42s
This commit is contained in:
35
src/index.ts
35
src/index.ts
@@ -6,25 +6,50 @@ import {
|
||||
GatewayIntentBits,
|
||||
Interaction,
|
||||
} from 'discord.js';
|
||||
import { commands } from './commands';
|
||||
import { getCommands, registerSlashCommands } from './utils/commands';
|
||||
import { Command } from './utils/types';
|
||||
import config from './config';
|
||||
|
||||
// Define an extended version of the Client interface to include commands
|
||||
interface ExtendedClient extends Client {
|
||||
commands: Collection<string, any>;
|
||||
commands: Collection<string, Command>;
|
||||
}
|
||||
|
||||
const client: ExtendedClient = new Client({
|
||||
intents: [GatewayIntentBits.Guilds],
|
||||
}) as ExtendedClient;
|
||||
|
||||
// Add the commands to the client
|
||||
client.commands = getCommands();
|
||||
|
||||
// If REGISTER_SLASH_COMMANDS is set to true, register the commands.
|
||||
if (config.registerSlashCommands) {
|
||||
registerSlashCommands(client.commands)
|
||||
.then(() => {
|
||||
console.log('Successfully registered slash commands');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Failed to register slash commands:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
// Register the event listener for command interactions.
|
||||
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
const { commandName } = interaction;
|
||||
const command = client.commands.get(interaction.commandName);
|
||||
|
||||
if (commands[commandName as keyof typeof commands]) {
|
||||
commands[commandName as keyof typeof commands].execute(interaction);
|
||||
if (command) {
|
||||
try {
|
||||
await command.execute(interaction);
|
||||
} catch (error) {
|
||||
console.error(`Error executing ${interaction.commandName}:`, error);
|
||||
await interaction.reply({
|
||||
content: 'There was an error executing that command!',
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user