|
|
@ -11,74 +11,82 @@ class AbstractModule { |
|
|
|
/* |
|
|
|
Name of the module used in help documentation and logging. |
|
|
|
*/ |
|
|
|
name = "AbstractModule"; |
|
|
|
name: string = "AbstractModule"; |
|
|
|
|
|
|
|
/* |
|
|
|
Short description of the module functionality. |
|
|
|
*/ |
|
|
|
description = "Base Module That All Other Modules Extend"; |
|
|
|
description: string = "Base Module That All Other Modules Extend"; |
|
|
|
|
|
|
|
/* |
|
|
|
A helpful multiline string that defines the module usage |
|
|
|
*/ |
|
|
|
helpAndUsage = `Example: !abstract_module <command>
|
|
|
|
helpAndUsage: string = `Example: !abstract_module <command>
|
|
|
|
!abstract_module <boo> : scares people`
|
|
|
|
|
|
|
|
/* |
|
|
|
The exported command used to invoke the module directly. |
|
|
|
*/ |
|
|
|
command = "abstract_module"; |
|
|
|
command: string = "abstract_module"; |
|
|
|
|
|
|
|
/* |
|
|
|
The default method to call when a command word is not recognized. |
|
|
|
*/ |
|
|
|
defaultCommand = null; |
|
|
|
defaultCommand?: string = null; |
|
|
|
|
|
|
|
/* |
|
|
|
The module should be hidden from help and command dialogs. |
|
|
|
*/ |
|
|
|
hidden = false; |
|
|
|
hidden: boolean = false; |
|
|
|
|
|
|
|
/* |
|
|
|
This module should receive all messages, regardless of whether |
|
|
|
the module was directly referenced with a command. |
|
|
|
*/ |
|
|
|
canHandleIndirectMessages = false; |
|
|
|
canHandleIndirectMessages: boolean = false; |
|
|
|
|
|
|
|
/* |
|
|
|
Indicates if the modules needs access to the global config |
|
|
|
*/ |
|
|
|
needGlobalConfig = false; |
|
|
|
needGlobalConfig: boolean = false; |
|
|
|
|
|
|
|
/* |
|
|
|
Indicates if the module requires a readable config file. |
|
|
|
*/ |
|
|
|
needConfig = false; |
|
|
|
needConfig: boolean = false; |
|
|
|
|
|
|
|
/* internal */ |
|
|
|
|
|
|
|
/* |
|
|
|
The global config passed in |
|
|
|
*/ |
|
|
|
_global_config = null; |
|
|
|
_global_config: any = null; |
|
|
|
|
|
|
|
/* |
|
|
|
The loaded config file, if it exists. |
|
|
|
*/ |
|
|
|
_config = null; |
|
|
|
_config: any = null; |
|
|
|
|
|
|
|
constructor(name, description, command) { |
|
|
|
/** |
|
|
|
* The recognized commands for the module |
|
|
|
*/ |
|
|
|
_recognizedCommands: Array<string> = []; |
|
|
|
|
|
|
|
/** |
|
|
|
* The map of commands to functions |
|
|
|
*/ |
|
|
|
_recognizedCommandMap: Map<string, string> = new Map(); |
|
|
|
|
|
|
|
constructor(name: string, description: string, command: string) { |
|
|
|
this.name = name; |
|
|
|
this.description = description; |
|
|
|
this.command = command; |
|
|
|
this._recognizedCommands = []; |
|
|
|
this._recognizedCommandMap = new Map(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Called after the module is initialized. |
|
|
|
*/ |
|
|
|
postInit() { |
|
|
|
postInit() : any { |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
@ -88,7 +96,7 @@ class AbstractModule { |
|
|
|
* @param {*} command |
|
|
|
* @param {*} methodName |
|
|
|
*/ |
|
|
|
addRecognizedCommand(command, methodName) { |
|
|
|
addRecognizedCommand(command: string, methodName: string) { |
|
|
|
this._recognizedCommands.push(command); |
|
|
|
this._recognizedCommandMap.set(command, methodName); |
|
|
|
} |
|
|
@ -96,21 +104,21 @@ class AbstractModule { |
|
|
|
/** |
|
|
|
* Returns the list of recognized commands. |
|
|
|
*/ |
|
|
|
getRecognizedCommands() { |
|
|
|
getRecognizedCommands() : Iterable<string> { |
|
|
|
return this._recognizedCommandMap.keys(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* The file path that the module configuration file is expected at. |
|
|
|
*/ |
|
|
|
getConfigFilePath() { |
|
|
|
getConfigFilePath() : string { |
|
|
|
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() : Array<string> { |
|
|
|
return []; |
|
|
|
} |
|
|
|
|
|
|
@ -120,7 +128,7 @@ class AbstractModule { |
|
|
|
* @param {*} key |
|
|
|
* @param {*} defaultValue |
|
|
|
*/ |
|
|
|
getGlobal(key, defaultValue = null) { |
|
|
|
getGlobal(key: string, defaultValue: string = null) { |
|
|
|
if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') { |
|
|
|
return this._global_config[key]; |
|
|
|
} |
|
|
@ -133,7 +141,7 @@ class AbstractModule { |
|
|
|
* @param {*} key |
|
|
|
* @param {*} defaultValue |
|
|
|
*/ |
|
|
|
get(key, defaultValue = null) { |
|
|
|
get(key: string, defaultValue: string = null) { |
|
|
|
if (this._config !== null && typeof this._config[key] !== 'undefined') { |
|
|
|
return this._config[key]; |
|
|
|
} |
|
|
@ -145,7 +153,7 @@ class AbstractModule { |
|
|
|
* |
|
|
|
* Override this if the module needs to do more complicated message processing. |
|
|
|
*/ |
|
|
|
handleMessage(event, room, callback) { |
|
|
|
handleMessage(event: any, room: any, callback: CallableFunction) { |
|
|
|
logger.debug("[%s] [%s] [%s]", this.name, room.name, event.event.content.body); |
|
|
|
|
|
|
|
let messageBody = event.event.content.body; |
|
|
@ -169,24 +177,24 @@ class AbstractModule { |
|
|
|
/* |
|
|
|
Call the command method with the args |
|
|
|
*/ |
|
|
|
processMessage(event, command, ...args) { |
|
|
|
processMessage(event: any, command: string, ...args: Array<string>) { |
|
|
|
if (this._recognizedCommands.includes(command)) { |
|
|
|
logger.debug("Calling %s with %s", 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); |
|
|
|
let newArgs = [command].concat(...args); |
|
|
|
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)](event, ...newArgs); |
|
|
|
} catch (e) { |
|
|
|
logger.error("Error while calling default command %s %s", this.defaultCommand, e); |
|
|
|
return this.cmd_help(); |
|
|
|
return this.cmd_help(event, ...newArgs); |
|
|
|
} |
|
|
|
} else { |
|
|
|
logger.debug("Unrecognized command %s", command); |
|
|
|
return this.cmd_help(); |
|
|
|
return this.cmd_help(event, ...[command].concat(...args)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -196,7 +204,7 @@ class AbstractModule { |
|
|
|
/* |
|
|
|
return basic help information,. |
|
|
|
*/ |
|
|
|
cmd_help(event, ...args) { |
|
|
|
cmd_help(event: any, ...args: Array<string>) { |
|
|
|
return message.createBasic(this.helpAndUsage); |
|
|
|
} |
|
|
|
|
|
|
@ -205,7 +213,7 @@ class AbstractModule { |
|
|
|
* |
|
|
|
* @param {...any} args |
|
|
|
*/ |
|
|
|
cmd_config(event, ...args) { |
|
|
|
cmd_config(event: any, ...args: Array<string>) { |
|
|
|
if (this._config != null) { |
|
|
|
let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2) |
|
|
|
return message.createMarkdown(configBody, "```" + configBody + "```"); |
|
|
@ -219,7 +227,7 @@ let abstractModulePrototype = Object.getPrototypeOf(new AbstractModule('', '', ' |
|
|
|
/* |
|
|
|
Initialization of a module. |
|
|
|
*/ |
|
|
|
function init(mod, globalConfig) { |
|
|
|
function init(mod: AbstractModule, globalConfig: any) { |
|
|
|
logger.debug("Initializing module %s", mod.name); |
|
|
|
if (mod.needConfig) { |
|
|
|
logger.debug("Loading config file %s", mod.getConfigFilePath()); |
|
|
@ -248,5 +256,5 @@ function init(mod, globalConfig) { |
|
|
|
mod.postInit(); |
|
|
|
} |
|
|
|
|
|
|
|
exports.AbstractModule = AbstractModule |
|
|
|
exports.initModule = init; |
|
|
|
export { AbstractModule }; |
|
|
|
export { init as initModule }; |