From fc37049b1ea67a687035eae01ebe6b1204690453 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Thu, 9 Jan 2020 21:56:38 -0600 Subject: [PATCH] Adding event to the module message handling --- bot/module/abstract.js | 48 ++++++++++++++++++++++++++++++++++++------ bot/module/admin.js | 2 +- bot/module/example.js | 2 +- bot/module/giphy.js | 2 +- bot/module/help.js | 2 +- bot/module/index.js | 4 +++- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/bot/module/abstract.js b/bot/module/abstract.js index 7c32d0f..be27358 100644 --- a/bot/module/abstract.js +++ b/bot/module/abstract.js @@ -75,23 +75,51 @@ class AbstractModule { this._recognizedCommandMap = new Map(); } + /** + * Called after the module is initialized. + */ + postInit() { + return this; + } + + /** + * Adds a recognized command and method to the module. + * + * @param {*} command + * @param {*} methodName + */ addRecognizedCommand(command, methodName) { this._recognizedCommands.push(command); this._recognizedCommandMap.set(command, methodName); } + /** + * Returns the list of recognized commands. + */ getRecognizedCommands() { return this._recognizedCommandMap.keys(); } + /** + * The file path that the module configuration file is expected at. + */ getConfigFilePath() { return process.env.NODE_PATH + '/data/' + this.name.toLowerCase().replace(' ', '_') + '-config.json'; } + /** + * Fields that should be sanitized before printing the config information for the user. + */ getConfigSensitiveFields() { return []; } + /** + * Return a global config value or a default. + * + * @param {*} key + * @param {*} defaultValue + */ getGlobal(key, defaultValue = null) { if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') { return this._global_config[key]; @@ -99,6 +127,12 @@ class AbstractModule { return defaultValue } + /** + * Return a module config value or a default. + * + * @param {*} key + * @param {*} defaultValue + */ get(key, defaultValue = null) { if (this._config !== null && typeof this._config[key] !== 'undefined') { return this._config[key]; @@ -123,8 +157,8 @@ class AbstractModule { args = bodyParts.slice(2); } - logger.debug("Attempting to call %s with %s", command, args); - let responseMessage = this.processMessage(command, ...args); + logger.debug("[%s] Attempting to call %s with %s", trigger, command, args); + let responseMessage = this.processMessage(event, command, ...args); callback( room, @@ -135,17 +169,17 @@ class AbstractModule { /* Call the command method with the args */ - processMessage(command, ...args) { + processMessage(event, command, ...args) { if (this._recognizedCommands.includes(command)) { logger.debug("Calling %s with %s", this._recognizedCommandMap.get(command), args); - return this[this._recognizedCommandMap.get(command)](...args); + return this[this._recognizedCommandMap.get(command)](event, ...args); } else { if (this.defaultCommand != null) { logger.debug("Attempting to use default command %s", this.defaultCommand); try { let newArgs = [command].concat(...args); logger.debug("Calling %s with %s", this._recognizedCommandMap.get(this.defaultCommand), newArgs); - return this[this._recognizedCommandMap.get(this.defaultCommand)](...newArgs); + return this[this._recognizedCommandMap.get(this.defaultCommand)](event, ...newArgs); } catch (e) { logger.error("Error while calling default command %s %s", this.defaultCommand, e); return this.cmd_help(); @@ -162,7 +196,7 @@ class AbstractModule { /* return basic help information,. */ - cmd_help(...args) { + cmd_help(event, ...args) { return message.createBasic(this.helpAndUsage); } @@ -171,7 +205,7 @@ class AbstractModule { * * @param {...any} args */ - cmd_config(...args) { + cmd_config(event, ...args) { if (this._config != null) { let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2) return message.createMarkdown(configBody, "```" + configBody + "```"); diff --git a/bot/module/admin.js b/bot/module/admin.js index a1b3d00..106a78f 100644 --- a/bot/module/admin.js +++ b/bot/module/admin.js @@ -42,7 +42,7 @@ class AdminModule extends AbstractModule { * * @param {...any} args */ - cmd_config(...args) { + cmd_config(event, ...args) { if (this._global_config != null) { let configBody = JSON.stringify(sanitizeConfig(this._global_config), null, 2) return message.createMarkdown(configBody, "```" + configBody + "```"); diff --git a/bot/module/example.js b/bot/module/example.js index 6fc7a03..bebb944 100644 --- a/bot/module/example.js +++ b/bot/module/example.js @@ -29,7 +29,7 @@ class ExampleModule extends AbstractModule { * * @param {...any} args the individual words passed after the command */ - cmd_blah(...args) { + cmd_blah(event, ...args) { return "Bla blah bla" } } diff --git a/bot/module/giphy.js b/bot/module/giphy.js index d8449b1..e5f0244 100644 --- a/bot/module/giphy.js +++ b/bot/module/giphy.js @@ -38,7 +38,7 @@ class GiphyModule extends AbstractModule { * * @param {...any} args */ - cmd_search(...args) { + cmd_search(event, ...args) { return this.getGiphySearch(args[0]) .then((response) => { // logger.debug("Giphy response: %o", response.data.data[0].url); diff --git a/bot/module/help.js b/bot/module/help.js index 0dc32ba..ab3d9dd 100644 --- a/bot/module/help.js +++ b/bot/module/help.js @@ -27,7 +27,7 @@ class HelpModule extends AbstractModule { /* Commands */ - cmd_help(...args) { + cmd_help(event, ...args) { logger.debug("%o", args) if (args.length < 1) { return this._default_help_message(); diff --git a/bot/module/index.js b/bot/module/index.js index 898020c..3dd5517 100644 --- a/bot/module/index.js +++ b/bot/module/index.js @@ -4,11 +4,13 @@ let admin = require('./admin'); let giphy = require('./giphy') +let szurubooru = require('./szurubooru/index'); function getModules() { return [ admin.module, - giphy.module + giphy.module, + szurubooru.module ]; }