Files
ButlerBot/src/index.ts
Jack 3a9fc1897a
All checks were successful
Build and Publish Docker Image / build_and_push (push) Successful in 42s
Improve typing.
2024-09-16 14:13:37 +00:00

61 lines
1.6 KiB
TypeScript

import 'dotenv/config';
import {
Client,
Collection,
Events,
GatewayIntentBits,
Interaction,
} from 'discord.js';
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, 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 command = client.commands.get(interaction.commandName);
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,
});
}
}
});
client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.login(process.env.DISCORD_API_KEY);