50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
const { readdirSync } = require("fs")
|
|
const { join } = require("path")
|
|
const color = require("../functions/colorCodes")
|
|
|
|
/**
|
|
* 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 = readdirSync(commandsPath).filter(file => file.endsWith(".js"))
|
|
|
|
let loadedCommands = 0
|
|
let failedCommands = 0
|
|
|
|
for (const file of commandFiles) {
|
|
try {
|
|
const command = require(join(commandsPath, 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
} |