|
@ -75,23 +75,51 @@ class AbstractModule { |
|
|
this._recognizedCommandMap = new Map(); |
|
|
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) { |
|
|
addRecognizedCommand(command, methodName) { |
|
|
this._recognizedCommands.push(command); |
|
|
this._recognizedCommands.push(command); |
|
|
this._recognizedCommandMap.set(command, methodName); |
|
|
this._recognizedCommandMap.set(command, methodName); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Returns the list of recognized commands. |
|
|
|
|
|
*/ |
|
|
getRecognizedCommands() { |
|
|
getRecognizedCommands() { |
|
|
return this._recognizedCommandMap.keys(); |
|
|
return this._recognizedCommandMap.keys(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* The file path that the module configuration file is expected at. |
|
|
|
|
|
*/ |
|
|
getConfigFilePath() { |
|
|
getConfigFilePath() { |
|
|
return process.env.NODE_PATH + '/data/' + this.name.toLowerCase().replace(' ', '_') + '-config.json'; |
|
|
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() { |
|
|
getConfigSensitiveFields() { |
|
|
return []; |
|
|
return []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Return a global config value or a default. |
|
|
|
|
|
* |
|
|
|
|
|
* @param {*} key |
|
|
|
|
|
* @param {*} defaultValue |
|
|
|
|
|
*/ |
|
|
getGlobal(key, defaultValue = null) { |
|
|
getGlobal(key, defaultValue = null) { |
|
|
if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') { |
|
|
if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') { |
|
|
return this._global_config[key]; |
|
|
return this._global_config[key]; |
|
@ -99,6 +127,12 @@ class AbstractModule { |
|
|
return defaultValue |
|
|
return defaultValue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Return a module config value or a default. |
|
|
|
|
|
* |
|
|
|
|
|
* @param {*} key |
|
|
|
|
|
* @param {*} defaultValue |
|
|
|
|
|
*/ |
|
|
get(key, defaultValue = null) { |
|
|
get(key, defaultValue = null) { |
|
|
if (this._config !== null && typeof this._config[key] !== 'undefined') { |
|
|
if (this._config !== null && typeof this._config[key] !== 'undefined') { |
|
|
return this._config[key]; |
|
|
return this._config[key]; |
|
@ -123,8 +157,8 @@ class AbstractModule { |
|
|
args = bodyParts.slice(2); |
|
|
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( |
|
|
callback( |
|
|
room, |
|
|
room, |
|
@ -135,17 +169,17 @@ class AbstractModule { |
|
|
/* |
|
|
/* |
|
|
Call the command method with the args |
|
|
Call the command method with the args |
|
|
*/ |
|
|
*/ |
|
|
processMessage(command, ...args) { |
|
|
|
|
|
|
|
|
processMessage(event, command, ...args) { |
|
|
if (this._recognizedCommands.includes(command)) { |
|
|
if (this._recognizedCommands.includes(command)) { |
|
|
logger.debug("Calling %s with %s", this._recognizedCommandMap.get(command), args); |
|
|
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 { |
|
|
} else { |
|
|
if (this.defaultCommand != null) { |
|
|
if (this.defaultCommand != null) { |
|
|
logger.debug("Attempting to use default command %s", this.defaultCommand); |
|
|
logger.debug("Attempting to use default command %s", this.defaultCommand); |
|
|
try { |
|
|
try { |
|
|
let newArgs = [command].concat(...args); |
|
|
let newArgs = [command].concat(...args); |
|
|
logger.debug("Calling %s with %s", this._recognizedCommandMap.get(this.defaultCommand), newArgs); |
|
|
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) { |
|
|
} catch (e) { |
|
|
logger.error("Error while calling default command %s %s", this.defaultCommand, e); |
|
|
logger.error("Error while calling default command %s %s", this.defaultCommand, e); |
|
|
return this.cmd_help(); |
|
|
return this.cmd_help(); |
|
@ -162,7 +196,7 @@ class AbstractModule { |
|
|
/* |
|
|
/* |
|
|
return basic help information,. |
|
|
return basic help information,. |
|
|
*/ |
|
|
*/ |
|
|
cmd_help(...args) { |
|
|
|
|
|
|
|
|
cmd_help(event, ...args) { |
|
|
return message.createBasic(this.helpAndUsage); |
|
|
return message.createBasic(this.helpAndUsage); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -171,7 +205,7 @@ class AbstractModule { |
|
|
* |
|
|
* |
|
|
* @param {...any} args |
|
|
* @param {...any} args |
|
|
*/ |
|
|
*/ |
|
|
cmd_config(...args) { |
|
|
|
|
|
|
|
|
cmd_config(event, ...args) { |
|
|
if (this._config != null) { |
|
|
if (this._config != null) { |
|
|
let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2) |
|
|
let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2) |
|
|
return message.createMarkdown(configBody, "```" + configBody + "```"); |
|
|
return message.createMarkdown(configBody, "```" + configBody + "```"); |
|
|