From a5c7aaef5fac0f651a293962f6572262a096c393 Mon Sep 17 00:00:00 2001 From: Emily J Date: Wed, 21 Oct 2020 11:23:23 +1100 Subject: [PATCH] clean up module loaders --- bot/index.js | 33 +++++++++++++------------------ bot/util/handlers/eventHandler.js | 16 +++++++-------- bot/util/messageHandler.js | 0 3 files changed, 22 insertions(+), 27 deletions(-) delete mode 100644 bot/util/messageHandler.js diff --git a/bot/index.js b/bot/index.js index 062d170..ea0299b 100644 --- a/bot/index.js +++ b/bot/index.js @@ -41,19 +41,16 @@ class WoomyClient extends Eris.Client { } loadCommands () { - const nameRegexp = /[^/]*$/; - const catRegexp = /.+?(?=\/)/; - for (const file of this.commandFiles) { try { - const props = new (require(this.path + '/commands/' + file))(this); - props.help.name = nameRegexp.exec(file); - props.help.category = catRegexp.exec(file); + const name = file.substr(file.indexOf('/') + 1).slice(0, -3); + const category = file.substr(0, file.indexOf('/')); + const command = new (require(this.path + '/commands/' + file))(name, category); - this.commands.set(props.help.name, props); - this.cooldowns.set(props.help.name, new Map()); - props.conf.aliases.forEach(alias => { - this.aliases.set(alias, props.help.name); + this.commands.set(command.name, command); + this.cooldowns.set(command.name, new Map()); + command.aliases.forEach(alias => { + this.aliases.set(alias, command.name); }); } catch (error) { this.logger.error('COMMAND_LOADER_ERROR', `Failed to load ${file}: ${error}`); @@ -64,9 +61,9 @@ class WoomyClient extends Eris.Client { } reloadCommands () { - for (const file of this.commandFiles) { + for (const cmd of this.commands) { try { - + } catch (error) { } @@ -74,25 +71,23 @@ class WoomyClient extends Eris.Client { } loadEventModules () { - const nameRegexp = /[^/]*$/; - const catRegexp = /.+?(?=\/)/; - for (const file of this.eventFiles) { try { - const event = new (require(this.path + '/event_modules/' + file))(catRegexp.exec(file)); - this.eventModules.set(nameRegexp.exec(file), event); + const name = file.substr(file.indexOf('/') + 1).slice(0, -3); + const category = file.substr(0, file.indexOf('/')); + const event = new (require(this.path + '/event_modules/' + file))(category); + this.eventModules.set(name, event); } catch (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.`); } reloadEventModules () { for (const file of this.eventFiles) { try { - + } catch (error) { } diff --git a/bot/util/handlers/eventHandler.js b/bot/util/handlers/eventHandler.js index 73becbc..a943203 100644 --- a/bot/util/handlers/eventHandler.js +++ b/bot/util/handlers/eventHandler.js @@ -8,56 +8,56 @@ class EventHandler { handle (wsEvent, param_1, param_2) { switch (wsEvent) { 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)); break; } // param_1 - error message 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)); break; } // param_1 - message object 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)); break; } // param_1 - guild object 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)); break; } // param_1 - guild object 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)); break; } // param_1 - guild object | param_2 - member object 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)); break; } // param_1 - guild object | param_2 - member object 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)); break; } // param_1 - old voice state | param_2 - new voice state 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)); break; } diff --git a/bot/util/messageHandler.js b/bot/util/messageHandler.js deleted file mode 100644 index e69de29..0000000