UNPKG

makecord-create

Version:

Create advanced Discord Bots with Makecord - Now with TypeScript support

101 lines (84 loc) 4.01 kB
import { Collection } from 'discord.js'; import { fileURLToPath } from 'url'; import path from 'path'; import fs from 'fs'; import chalk from 'chalk'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export class CommandHandler { constructor(client) { this.client = client; this.commands = new Collection(); this.categories = new Collection(); } async loadCommands() { try { const categoriesPath = path.join(__dirname); const categories = fs.readdirSync(categoriesPath) .filter(file => fs.statSync(path.join(categoriesPath, file)).isDirectory()); for (const category of categories) { const categoryPath = path.join(categoriesPath, category); const commandFiles = fs.readdirSync(categoryPath) .filter(file => file.endsWith('.js')); const categoryCommands = []; for (const file of commandFiles) { const filePath = path.join(categoryPath, file); const command = await import(`file://${filePath}`); if ('data' in command.default && 'execute' in command.default) { this.commands.set(command.default.data.name, command.default); categoryCommands.push(command.default.data.name); console.log(chalk.green(`Loaded command: ${command.default.data.name}`)); } else { console.log(chalk.yellow(`Invalid command in ${filePath}`)); } } this.categories.set(category, categoryCommands); console.log(chalk.blue(`Loaded category: ${category} with ${categoryCommands.length} commands`)); } } catch (error) { console.error(chalk.red('Error loading commands:', error)); } } async registerCommands() { try { const commands = Array.from(this.commands.values()).map(cmd => cmd.data.toJSON()); if (process.env.NODE_ENV === 'development') { // In development, register commands to a specific guild for instant updates const guild = await this.client.guilds.fetch(process.env.DEVELOPMENT_GUILD_ID); await guild.commands.set(commands); console.log(chalk.green(`Registered ${commands.length} commands to development guild`)); } else { // In production, register commands globally await this.client.application.commands.set(commands); console.log(chalk.green(`Registered ${commands.length} commands globally`)); } } catch (error) { console.error(chalk.red('Error registering commands:', error)); } } getCommand(name) { return this.commands.get(name); } getCategoryCommands(category) { return this.categories.get(category) || []; } getAllCategories() { return Array.from(this.categories.keys()); } async handleInteraction(interaction) { if (!interaction.isCommand()) return; const command = this.getCommand(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(chalk.red(`Error executing command ${interaction.commandName}:`, error)); const errorMessage = 'There was an error executing this command!'; if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: errorMessage, ephemeral: true }); } else { await interaction.reply({ content: errorMessage, ephemeral: true }); } } } }