Browse Source

Adding event to the module message handling

develop
Drew Short 5 years ago
parent
commit
fc37049b1e
  1. 48
      bot/module/abstract.js
  2. 2
      bot/module/admin.js
  3. 2
      bot/module/example.js
  4. 2
      bot/module/giphy.js
  5. 2
      bot/module/help.js
  6. 4
      bot/module/index.js

48
bot/module/abstract.js

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

2
bot/module/admin.js

@ -42,7 +42,7 @@ class AdminModule extends AbstractModule {
* *
* @param {...any} args * @param {...any} args
*/ */
cmd_config(...args) {
cmd_config(event, ...args) {
if (this._global_config != null) { if (this._global_config != null) {
let configBody = JSON.stringify(sanitizeConfig(this._global_config), null, 2) let configBody = JSON.stringify(sanitizeConfig(this._global_config), null, 2)
return message.createMarkdown(configBody, "```" + configBody + "```"); return message.createMarkdown(configBody, "```" + configBody + "```");

2
bot/module/example.js

@ -29,7 +29,7 @@ class ExampleModule extends AbstractModule {
* *
* @param {...any} args the individual words passed after the command * @param {...any} args the individual words passed after the command
*/ */
cmd_blah(...args) {
cmd_blah(event, ...args) {
return "Bla blah bla" return "Bla blah bla"
} }
} }

2
bot/module/giphy.js

@ -38,7 +38,7 @@ class GiphyModule extends AbstractModule {
* *
* @param {...any} args * @param {...any} args
*/ */
cmd_search(...args) {
cmd_search(event, ...args) {
return this.getGiphySearch(args[0]) return this.getGiphySearch(args[0])
.then((response) => { .then((response) => {
// logger.debug("Giphy response: %o", response.data.data[0].url); // logger.debug("Giphy response: %o", response.data.data[0].url);

2
bot/module/help.js

@ -27,7 +27,7 @@ class HelpModule extends AbstractModule {
/* Commands */ /* Commands */
cmd_help(...args) {
cmd_help(event, ...args) {
logger.debug("%o", args) logger.debug("%o", args)
if (args.length < 1) { if (args.length < 1) {
return this._default_help_message(); return this._default_help_message();

4
bot/module/index.js

@ -4,11 +4,13 @@
let admin = require('./admin'); let admin = require('./admin');
let giphy = require('./giphy') let giphy = require('./giphy')
let szurubooru = require('./szurubooru/index');
function getModules() { function getModules() {
return [ return [
admin.module, admin.module,
giphy.module
giphy.module,
szurubooru.module
]; ];
} }
Loading…
Cancel
Save