clean up module loaders

This commit is contained in:
Emily 2020-10-21 11:23:23 +11:00
parent 85d0255ccc
commit a5c7aaef5f
3 changed files with 22 additions and 27 deletions

View file

@ -41,19 +41,16 @@ class WoomyClient extends Eris.Client {
} }
loadCommands () { loadCommands () {
const nameRegexp = /[^/]*$/;
const catRegexp = /.+?(?=\/)/;
for (const file of this.commandFiles) { for (const file of this.commandFiles) {
try { try {
const props = new (require(this.path + '/commands/' + file))(this); const name = file.substr(file.indexOf('/') + 1).slice(0, -3);
props.help.name = nameRegexp.exec(file); const category = file.substr(0, file.indexOf('/'));
props.help.category = catRegexp.exec(file); const command = new (require(this.path + '/commands/' + file))(name, category);
this.commands.set(props.help.name, props); this.commands.set(command.name, command);
this.cooldowns.set(props.help.name, new Map()); this.cooldowns.set(command.name, new Map());
props.conf.aliases.forEach(alias => { command.aliases.forEach(alias => {
this.aliases.set(alias, props.help.name); this.aliases.set(alias, command.name);
}); });
} catch (error) { } catch (error) {
this.logger.error('COMMAND_LOADER_ERROR', `Failed to load ${file}: ${error}`); this.logger.error('COMMAND_LOADER_ERROR', `Failed to load ${file}: ${error}`);
@ -64,7 +61,7 @@ class WoomyClient extends Eris.Client {
} }
reloadCommands () { reloadCommands () {
for (const file of this.commandFiles) { for (const cmd of this.commands) {
try { try {
} catch (error) { } catch (error) {
@ -74,18 +71,16 @@ class WoomyClient extends Eris.Client {
} }
loadEventModules () { loadEventModules () {
const nameRegexp = /[^/]*$/;
const catRegexp = /.+?(?=\/)/;
for (const file of this.eventFiles) { for (const file of this.eventFiles) {
try { try {
const event = new (require(this.path + '/event_modules/' + file))(catRegexp.exec(file)); const name = file.substr(file.indexOf('/') + 1).slice(0, -3);
this.eventModules.set(nameRegexp.exec(file), event); const category = file.substr(0, file.indexOf('/'));
const event = new (require(this.path + '/event_modules/' + file))(category);
this.eventModules.set(name, event);
} catch (error) { } catch (error) {
this.logger.error('EVENT_LOADER_ERROR', `Failed to load ${file}: ${error}`); this.logger.error('EVENT_LOADER_ERROR', `Failed to load ${file}: ${error}`);
} }
} }
console.log(this.eventModules)
this.logger.success('EVENT_LOADER_SUCCESS', `Loaded ${this.eventModules.size}/${this.eventFiles.length} event modules.`); this.logger.success('EVENT_LOADER_SUCCESS', `Loaded ${this.eventModules.size}/${this.eventFiles.length} event modules.`);
} }

View file

@ -8,56 +8,56 @@ class EventHandler {
handle (wsEvent, param_1, param_2) { handle (wsEvent, param_1, param_2) {
switch (wsEvent) { switch (wsEvent) {
case 'ready': { case 'ready': {
const readyModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'ready'); const readyModules = this.client.eventModules.filter(module => module.wsEvent === 'ready');
readyModules.forEach(module => module.run(this.client)); readyModules.forEach(module => module.run(this.client));
break; break;
} }
// param_1 - error message // param_1 - error message
case 'error': { case 'error': {
const errorModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'error'); const errorModules = this.client.eventModules.filter(module => module.wsEvent === 'error');
errorModules.forEach(module => module.run(this.client, param_1)); errorModules.forEach(module => module.run(this.client, param_1));
break; break;
} }
// param_1 - message object // param_1 - message object
case 'messageCreate': { case 'messageCreate': {
const mCreateModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'messageCreate'); const mCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'messageCreate');
mCreateModules.forEach(module => module.run(this.client, param_1)); mCreateModules.forEach(module => module.run(this.client, param_1));
break; break;
} }
// param_1 - guild object // param_1 - guild object
case 'guildCreate': { case 'guildCreate': {
const gCreateModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'guildCreate'); const gCreateModules = this.client.eventModules.filter(module => module.wsEvent === 'guildCreate');
gCreateModules.forEach(module => module.run(this.client, param_1)); gCreateModules.forEach(module => module.run(this.client, param_1));
break; break;
} }
// param_1 - guild object // param_1 - guild object
case 'guildDelete': { case 'guildDelete': {
const gDeleteModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'guildDelete'); const gDeleteModules = this.client.eventModules.filter(module => module.wsEvent === 'guildDelete');
gDeleteModules.forEach(module => module.run(this.client, param_1)); gDeleteModules.forEach(module => module.run(this.client, param_1));
break; break;
} }
// param_1 - guild object | param_2 - member object // param_1 - guild object | param_2 - member object
case 'guildMemberAdd': { case 'guildMemberAdd': {
const gMemberAddModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'guildMemberAdd'); const gMemberAddModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberAdd');
gMemberAddModules.forEach(module => module.run(this.client, param_1, param_2)); gMemberAddModules.forEach(module => module.run(this.client, param_1, param_2));
break; break;
} }
// param_1 - guild object | param_2 - member object // param_1 - guild object | param_2 - member object
case 'guildMemberRemove': { case 'guildMemberRemove': {
const gMemberRemoveModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'guildMemberRemove'); const gMemberRemoveModules = this.client.eventModules.filter(module => module.wsEvent === 'guildMemberRemove');
gMemberRemoveModules.forEach(module => module.run(this.client, param_1, param_2)); gMemberRemoveModules.forEach(module => module.run(this.client, param_1, param_2));
break; break;
} }
// param_1 - old voice state | param_2 - new voice state // param_1 - old voice state | param_2 - new voice state
case 'voiceStateUpdate': { case 'voiceStateUpdate': {
const vStateUpdateModules = this.client.eventModules.filter(module => module.wsEvent[0] === 'voiceStateUpdate'); const vStateUpdateModules = this.client.eventModules.filter(module => module.wsEvent === 'voiceStateUpdate');
vStateUpdateModules.forEach(module => module.run(this.client)); vStateUpdateModules.forEach(module => module.run(this.client));
break; break;
} }