2020-10-21 03:43:05 +00:00
|
|
|
const read = require('fs-readdir-recursive');
|
|
|
|
|
|
|
|
class CommandLoader {
|
|
|
|
constructor (client) {
|
|
|
|
this.client = client;
|
|
|
|
this.commandFiles = read('./commands').filter(file => file.endsWith('.js'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads up all commands
|
|
|
|
loadCommands () {
|
|
|
|
for (const file of this.commandFiles) {
|
|
|
|
try {
|
|
|
|
const name = file.substr(file.indexOf('/') + 1).slice(0, -3);
|
|
|
|
const category = file.substr(0, file.indexOf('/'));
|
|
|
|
const command = new (require(this.client.path + '/commands/' + file))(name, category);
|
|
|
|
this.client.commands.set(command.name, command);
|
2020-10-21 07:57:35 +00:00
|
|
|
this.client.cooldowns.set(command.name, new Map());
|
2020-10-21 03:43:05 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.client.logger.error('COMMAND_LOADER_ERROR', `Failed to load ${file}: ${error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client.logger.success('COMMAND_LOADER_SUCCESS', `Loaded ${this.client.commands.size}/${this.commandFiles.length} commands.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reloads all currently loaded commands, so we don't need to restart to apply changes
|
|
|
|
reloadCommands () {
|
2020-10-21 07:57:35 +00:00
|
|
|
this.client.cooldowns.clear();
|
2020-10-21 03:43:05 +00:00
|
|
|
this.client.commands.forEach(cmd => {
|
|
|
|
try {
|
|
|
|
delete require.cache[require.resolve(`${this.client.path}/commands/${cmd.category}/${cmd.name}.js`)];
|
|
|
|
this.client.commands.delete(cmd.name);
|
|
|
|
} catch (error) {
|
|
|
|
this.client.logger.error('COMMAND_LOADER_ERROR', `Failed to unload ${cmd}: ${error}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.loadCommands();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CommandLoader;
|