move loaders out of mainfile
This commit is contained in:
parent
99e859dd48
commit
7b9f5bfa33
3 changed files with 109 additions and 81 deletions
44
bot/util/commandLoader.js
Normal file
44
bot/util/commandLoader.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
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);
|
||||
command.aliases.forEach(alias => {
|
||||
this.client.aliases.set(alias, command.name);
|
||||
});
|
||||
} 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 () {
|
||||
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;
|
40
bot/util/eventLoader.js
Normal file
40
bot/util/eventLoader.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const read = require('fs-readdir-recursive');
|
||||
|
||||
class EventLoader {
|
||||
constructor (client) {
|
||||
this.client = client;
|
||||
this.eventFiles = read('./event_modules').filter(file => file.endsWith('.js'));
|
||||
}
|
||||
|
||||
// Load all our event modules
|
||||
loadEventModules () {
|
||||
for (const file of this.eventFiles) {
|
||||
try {
|
||||
const name = file.substr(file.indexOf('/') + 1).slice(0, -3);
|
||||
const category = file.substr(0, file.indexOf('/'));
|
||||
const event = new (require(this.client.path + '/event_modules/' + file))(category);
|
||||
this.client.eventModules.set(name, event);
|
||||
} catch (error) {
|
||||
this.client.logger.error('EVENT_LOADER_ERROR', `Failed to load ${file}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.client.logger.success('EVENT_LOADER_SUCCESS', `Loaded ${this.client.eventModules.size}/${this.eventFiles.length} event modules.`);
|
||||
}
|
||||
|
||||
// Reloads all currently loaded events, so we don't need to restart to apply changes
|
||||
reloadEventModules () {
|
||||
this.client.eventModules.forEach((props, event) => {
|
||||
try {
|
||||
delete require.cache[require.resolve(`${this.client.path}/event_modules/${props.wsEvent}/${event}.js`)];
|
||||
this.client.eventModules.delete(event);
|
||||
} catch (error) {
|
||||
this.client.logger.error('EVENT_LOADER_ERROR', `Failed to unload ${event}: ${error}`);
|
||||
}
|
||||
});
|
||||
|
||||
this.loadEventModules();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EventLoader;
|
Loading…
Add table
Add a link
Reference in a new issue