|
|
@ -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 + "```"); |
|
|
|