75 lines
2.3 KiB
JavaScript
75 lines
2.3 KiB
JavaScript
const { readdirSync, statSync } = require("fs")
|
|
const { join } = require("path")
|
|
const color = require("../functions/colorCodes")
|
|
|
|
/**
|
|
* Recursively gets all command files from a directory
|
|
* @param {string} dir - Directory to scan
|
|
* @returns {string[]} Array of command file paths
|
|
*/
|
|
function getCommandFiles(dir) {
|
|
const files = []
|
|
const items = readdirSync(dir)
|
|
|
|
for (const item of items) {
|
|
const path = join(dir, item)
|
|
const stat = statSync(path)
|
|
|
|
if (stat.isDirectory()) {
|
|
files.push(...getCommandFiles(path))
|
|
} else if (item.endsWith('.js')) {
|
|
files.push(path)
|
|
}
|
|
}
|
|
|
|
return files
|
|
}
|
|
|
|
/**
|
|
* Loads and registers all command files from the commands directory
|
|
* @param {Object} client - The Discord client instance
|
|
* @returns {Promise<void>}
|
|
*/
|
|
module.exports = async (client) => {
|
|
try {
|
|
const commandsPath = join(__dirname, "..", "commands")
|
|
const commandFiles = getCommandFiles(commandsPath)
|
|
|
|
let loadedCommands = 0
|
|
let failedCommands = 0
|
|
|
|
for (const file of commandFiles) {
|
|
try {
|
|
const command = require(file)
|
|
|
|
// Validate command structure
|
|
if (!command.config?.name) {
|
|
console.error(color("%", `%r[Command_Handler]%7 :: Command in ${file} is missing required config.name property`))
|
|
failedCommands++
|
|
continue
|
|
}
|
|
|
|
console.log(color("%", `%b[Command_Handler]%7 :: Loading %e${command.config.name}%7 command`))
|
|
|
|
// Register command and aliases
|
|
client.commands.set(command.config.name, command)
|
|
if (command.config.aliases?.length) {
|
|
command.config.aliases.forEach(alias => client.aliases.set(alias, command.config.name))
|
|
}
|
|
|
|
loadedCommands++
|
|
} catch (error) {
|
|
console.error(color("%", `%r[Command_Handler]%7 :: Failed to load command ${file}: ${error.message}`))
|
|
failedCommands++
|
|
}
|
|
}
|
|
|
|
console.log(color("%", `%b[Command_Handler]%7 :: Successfully loaded %e${loadedCommands}%7 commands`))
|
|
if (failedCommands > 0) {
|
|
console.warn(color("%", `%y[Command_Handler]%7 :: Failed to load %r${failedCommands}%7 commands`))
|
|
}
|
|
} catch (error) {
|
|
console.error(color("%", `%r[Command_Handler]%7 :: Critical error: ${error.message}`))
|
|
throw error // Re-throw to handle at higher level
|
|
}
|
|
} |