import fs from 'fs'; import path from 'path'; module.exports = function loadCommands(client: any, dir: string) { let count = 0; fs.readdirSync(path.join(__dirname, dir)).forEach((file: string) => { const filePath = path.join(__dirname, dir, file); if (fs.statSync(filePath).isDirectory()) { count += loadCommands(client, path.join(dir, file)); } else if (file.endsWith('.js') || file.endsWith('.ts')) { try { delete require.cache[require.resolve(filePath)]; const command = require(filePath); const fileName = file.replace(/\.js|\.ts/g, ''); command.name = fileName; if (!command.category) { const parentDir = path.basename(path.dirname(filePath)); command.category = parentDir === 'commands' ? 'other' : parentDir; } client.commands.set(fileName, command); if (command.aliases) { command.aliases.forEach((alias: string) => { client.commands.set(alias, command); }); } count++; } catch (error) { console.error(`Failed to load file: ${filePath}`); console.error(error); } } }); return count; }