From 4c16d8161e9af45eb7ed050830512fa09557cf44 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Mon, 30 Dec 2019 00:25:29 -0600 Subject: [PATCH] Fix broken command lookup * Migrate to helpAndUsage templates for standard help --- bot/module/abstract.js | 28 ++++++++++++++++++---------- bot/module/admin.js | 2 ++ bot/module/giphy.js | 2 ++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/bot/module/abstract.js b/bot/module/abstract.js index afb3187..de16bac 100644 --- a/bot/module/abstract.js +++ b/bot/module/abstract.js @@ -17,6 +17,12 @@ class AbstractModule { */ description = "Base Module That All Other Modules Extend"; + /* + A helpful multiline string that defines the module usage + */ + helpAndUsage = `Example: !abstract_module + !abstract_module : scares people` + /* The exported command used to invoke the module directly. */ @@ -40,15 +46,17 @@ class AbstractModule { this.name = name; this.description = description; this.command = command; - this._recognizedCommands = new Map(); + this._recognizedCommands = []; + this._recognizedCommandMap = new Map(); } addRecognizedCommand(command, methodName) { - this._recognizedCommands.set(command, methodName) + this._recognizedCommands.push(command); + this._recognizedCommandMap.set(command, methodName); } getRecognizedCommands() { - return this._recognizedCommands.keys(); + return this._recognizedCommandMap.keys(); } /** @@ -69,7 +77,7 @@ class AbstractModule { } logger.debug("Attempting to call %s with %s", command, args); - let responseMessage = this.processMessage(command, args); + let responseMessage = this.processMessage(command, ...args); callback( room, @@ -81,16 +89,16 @@ class AbstractModule { Call the command method with the args */ processMessage(command, ...args) { - if (command in this._recognizedCommands) { - logger.debug("Calling %s with %s", this._recognizedCommands.get(command), args); - return this[this._recognizedCommands.get(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); } 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._recognizedCommands.get(this.defaultCommand), newArgs); - return this[this._recognizedCommands.get(this.defaultCommand)](...newArgs); + logger.debug("Calling %s with %s", this._recognizedCommandMap.get(this.defaultCommand), newArgs); + return this[this._recognizedCommandMap.get(this.defaultCommand)](...newArgs); } catch (e) { logger.error("Error while calling default command %s %s", this.defaultCommand, e); return this.cmd_help(); @@ -108,7 +116,7 @@ class AbstractModule { return basic help information,. */ cmd_help(...args) { - return message.createBasic(this.name + " HELP!"); + return message.createBasic(this.helpAndUsage); } } diff --git a/bot/module/admin.js b/bot/module/admin.js index 08dff6b..fafd4bd 100644 --- a/bot/module/admin.js +++ b/bot/module/admin.js @@ -11,6 +11,8 @@ class AdminModule extends AbstractModule { "Support administration tasks", "admin" ); + this.helpAndUsage = `Usage: admin + ...` } } diff --git a/bot/module/giphy.js b/bot/module/giphy.js index b0a9698..5fb6fb8 100644 --- a/bot/module/giphy.js +++ b/bot/module/giphy.js @@ -11,6 +11,8 @@ class GiphyModule extends AbstractModule { "Insert Giphy Links/Media", "giphy" ); + this.helpAndUsage = `Usage: !giphy itsworking + ...` } }