|
@ -14,6 +14,7 @@ class Engine { |
|
|
this.bot = bot; |
|
|
this.bot = bot; |
|
|
this.modules = modules; |
|
|
this.modules = modules; |
|
|
this.moduleMap = new Map(); |
|
|
this.moduleMap = new Map(); |
|
|
|
|
|
this.commands = []; |
|
|
this.commandMap = new Map(); |
|
|
this.commandMap = new Map(); |
|
|
this.commandRadixTree = trie([]); |
|
|
this.commandRadixTree = trie([]); |
|
|
} |
|
|
} |
|
@ -34,6 +35,7 @@ class Engine { |
|
|
|
|
|
|
|
|
this.helpModule = help.create(this.moduleMap) |
|
|
this.helpModule = help.create(this.moduleMap) |
|
|
this.commandMap.set('help', this.helpModule); |
|
|
this.commandMap.set('help', this.helpModule); |
|
|
|
|
|
this.commands = Array.from(this.commandMap.keys()).sort() |
|
|
|
|
|
|
|
|
logger.info("Bound modules to keywords: %o", this.moduleMap); |
|
|
logger.info("Bound modules to keywords: %o", this.moduleMap); |
|
|
} |
|
|
} |
|
@ -56,20 +58,13 @@ class Engine { |
|
|
logger.debug("[%s] %s", room.name, messageBody); |
|
|
logger.debug("[%s] %s", room.name, messageBody); |
|
|
if (messageBody.indexOf(sentinelValue) === 0) { |
|
|
if (messageBody.indexOf(sentinelValue) === 0) { |
|
|
let command = messageBody.split(' ')[0].substring(1); |
|
|
let command = messageBody.split(' ')[0].substring(1); |
|
|
var responseMessage = null; |
|
|
|
|
|
if (engine.commandMap.has(command)) { |
|
|
if (engine.commandMap.has(command)) { |
|
|
responseMessage = engine.commandMap.get(command).handleMessage(event, room); |
|
|
|
|
|
|
|
|
engine.commandMap.get(command).handleMessage(event, room, sendResponseMessageCallback(engine.bot)); |
|
|
} else { |
|
|
} else { |
|
|
responseMessage = engine.helpModule.help(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logger.debug("Responding to room: %s", room.roomId); |
|
|
|
|
|
if (responseMessage instanceof Object) { |
|
|
|
|
|
engine.bot.client.sendMessage(room.roomId, responseMessage); |
|
|
|
|
|
} else if (utility.isString(responseMessage)) { |
|
|
|
|
|
engine.bot.client.sendMessage(room.roomId, message.createBasic(responseMessage)); |
|
|
|
|
|
} else { |
|
|
|
|
|
logger.error("Unable to process response message: %s", responseMessage); |
|
|
|
|
|
|
|
|
let responseMessage = "The following commands are recognized" |
|
|
|
|
|
responseMessage += "\n" + engine.commands.join(", ") |
|
|
|
|
|
responseMessage += "\nAdditional information can be discovered with !help <command>" |
|
|
|
|
|
sendResponseMessage(engine.bot, room, responseMessage); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -104,6 +99,35 @@ class Engine { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Handle the callback sending messages via the bot |
|
|
|
|
|
* |
|
|
|
|
|
* @param {*} bot |
|
|
|
|
|
* @param {*} room |
|
|
|
|
|
* @param {*} responseMessage |
|
|
|
|
|
*/ |
|
|
|
|
|
function sendResponseMessage(bot, room, responseMessage) { |
|
|
|
|
|
logger.debug("Responding to room: %s", room.roomId); |
|
|
|
|
|
if (responseMessage instanceof Object) { |
|
|
|
|
|
bot.client.sendMessage(room.roomId, responseMessage); |
|
|
|
|
|
} else if (utility.isString(responseMessage)) { |
|
|
|
|
|
bot.client.sendMessage(room.roomId, message.createBasic(responseMessage)); |
|
|
|
|
|
} else { |
|
|
|
|
|
logger.error("Unable to process response message: %s", responseMessage); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Wrapper to produce a callback function that can be passed to the modules |
|
|
|
|
|
* |
|
|
|
|
|
* @param {*} bot |
|
|
|
|
|
*/ |
|
|
|
|
|
function sendResponseMessageCallback(bot) { |
|
|
|
|
|
return (room, responseMessage) => { |
|
|
|
|
|
sendResponseMessage(bot, room, responseMessage); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function create(config, bot) { |
|
|
function create(config, bot) { |
|
|
return new Engine(config, bot, modules) |
|
|
return new Engine(config, bot, modules) |
|
|
} |
|
|
} |
|
|