'use strict'; const fs = require('fs'); const path = require('path'); const logger = require('../utils/logger'); /** * @param {import('discord.js').Client} client */ function loadCommands(client) { const commandsPath = path.join(__dirname, '..', 'commands'); function getAllCommandFiles(dir) { let results = []; const list = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of list) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { results = results.concat(getAllCommandFiles(fullPath)); } else if (entry.isFile() && entry.name.endsWith('.js')) { results.push(path.relative(commandsPath, fullPath)); } } return results; } const commandFiles = getAllCommandFiles(commandsPath); let loaded = 0; for (const file of commandFiles) { try { const command = require(path.join(commandsPath, file)); if (!command.name || typeof command.execute !== 'function') { logger.warn( `[commandhandler] skipping ${file}: missing "name" or "execute"`, ); continue; } client.commands.set(command.name.toLowerCase(), command); if (Array.isArray(command.aliases)) { for (const alias of command.aliases) { client.commands.set(alias.toLowerCase(), command); } } logger.debug(`[commandhandler] loaded command: ${command.name}`); loaded++; } catch (err) { logger.error(`[commandhandler] failed to load ${file}:`, err); } } logger.info(`[commandhandler] ${loaded} command(s) loaded`); } module.exports = { loadCommands };