commandhandler refactor

This commit is contained in:
yeongaori
2025-08-16 02:43:23 +09:00
parent f6b81ad909
commit b7311f0907
5 changed files with 164 additions and 143 deletions

View File

@@ -1,5 +1,6 @@
require('dotenv').config();
const { Client, GatewayIntentBits, Partials } = require('discord.js');
console.log('index.js is being executed');
const { Client, GatewayIntentBits, Partials, Collection } = require('discord.js');
const fs = require('fs');
const path = require('path');
const logger = require('./modules/colorfulLogger');
@@ -20,6 +21,35 @@ const client = new Client({
loadData();
setupProcessHandlers(client);
// Load commands
client.commands = new Collection();
client.legacyCommands = new Collection();
const commandFolders = fs.readdirSync(path.join(__dirname, 'src', 'commands'));
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(path.join(__dirname, 'src', 'commands', folder)).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(__dirname, 'src', 'commands', folder, file);
const command = require(filePath);
// Load slash command part
if (command.data && command.execute) {
client.commands.set(command.data.name, command);
}
// Load legacy command part
if (command.legacy) {
client.legacyCommands.set(command.legacy.name, command.legacy);
if (command.legacy.aliases) {
command.legacy.aliases.forEach(alias => {
client.legacyCommands.set(alias, command.legacy);
});
}
}
}
}
// Load event handlers
const eventsPath = path.join(__dirname, 'src', 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
@@ -38,4 +68,4 @@ logger.info('Attempting to log in with token...');
client.login(process.env.DISCORD_TOKEN).catch(e => {
logger.error(`Failed to login: ${e.message}`);
process.exit(1);
});
});