From 4cce06e2db81fae29baf4093913c855512ac6582 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Sun, 29 Dec 2019 20:15:13 -0600 Subject: [PATCH] Making module respones a callback * Moved responding code into a callback * Fixed the broken LOG_LEVEL setup * Spelling corrections * Printing rooms joined to logs on startup --- bot/bot.js | 16 ++++++++------ bot/config.js | 2 +- bot/engine.js | 48 +++++++++++++++++++++++++++++++----------- bot/logging.js | 3 ++- bot/module/abstract.js | 7 ++++-- 5 files changed, 53 insertions(+), 23 deletions(-) diff --git a/bot/bot.js b/bot/bot.js index 20d0707..55ab2c0 100644 --- a/bot/bot.js +++ b/bot/bot.js @@ -21,11 +21,15 @@ class Bot { userId: this.config.userId }); - this.client.on("sync", (state, previousState, data) => { + this.client.on("sync", async (state, previousState, data) => { switch (state) { case "PREPARED": this.connected = true; - this.sendStatusStartup(); + await this.sendStatusStartup(); + this.client.getJoinedRooms() + .done((rooms) => { + logger.info("Connected to: %o", rooms) + }); break; case "SYNCING": logger.debug("Syncing") @@ -38,11 +42,11 @@ class Bot { process.exit(1); } }); - + this.client.on("RoomMember.membership", (event, member) => { if (member.membership === "invite" && this.config.admin.indexOf(ember.userId) >= 0) { - this.client.joinRoom(member.roomId).done(() => { + this.client.joinRoom(member.roomId).done(() => { logger.info("Auto-joined %s", member.roomId); }); } @@ -57,9 +61,7 @@ class Bot { // logger.info("Initializing Crypto"); // await bot.client.initCrypto(); logger.info("Starting Matrix SDK Client"); - await this.client.startClient({ - initialSyncLimit: 0 - }); + await this.client.startClient(); } sendStatusStartup() { diff --git a/bot/config.js b/bot/config.js index ad6fb89..9a00593 100644 --- a/bot/config.js +++ b/bot/config.js @@ -13,7 +13,7 @@ function getConfig(configFile) { if (config === null) { configPath = configFile; config = require(configFile); - logger.info("Leaded config:"); + logger.info("Loaded config:"); logger.debug("%o", sanitizeConfig(config)); } return config; diff --git a/bot/engine.js b/bot/engine.js index d5f879f..f4a6958 100644 --- a/bot/engine.js +++ b/bot/engine.js @@ -14,6 +14,7 @@ class Engine { this.bot = bot; this.modules = modules; this.moduleMap = new Map(); + this.commands = []; this.commandMap = new Map(); this.commandRadixTree = trie([]); } @@ -34,6 +35,7 @@ class Engine { this.helpModule = help.create(this.moduleMap) this.commandMap.set('help', this.helpModule); + this.commands = Array.from(this.commandMap.keys()).sort() logger.info("Bound modules to keywords: %o", this.moduleMap); } @@ -56,20 +58,13 @@ class Engine { logger.debug("[%s] %s", room.name, messageBody); if (messageBody.indexOf(sentinelValue) === 0) { let command = messageBody.split(' ')[0].substring(1); - var responseMessage = null; if (engine.commandMap.has(command)) { - responseMessage = engine.commandMap.get(command).handleMessage(event, room); + engine.commandMap.get(command).handleMessage(event, room, sendResponseMessageCallback(engine.bot)); } 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 " + 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) { return new Engine(config, bot, modules) } diff --git a/bot/logging.js b/bot/logging.js index 2b0a863..ca3eea6 100644 --- a/bot/logging.js +++ b/bot/logging.js @@ -22,7 +22,8 @@ if (process.env.NODE_ENV !== 'production') { })); } -if (process.env.LOG_LEVEL !== '') { +if ('LOG_LEVE' in process.env) { + logger.info('LOG_LEVEL:', process.env.LOG_LEVEL) logger.level = process.env.LOG_LEVEL } diff --git a/bot/module/abstract.js b/bot/module/abstract.js index 91233f7..e98fba4 100644 --- a/bot/module/abstract.js +++ b/bot/module/abstract.js @@ -16,9 +16,12 @@ class AbstractModule { this.command = command; } - handleMessage(event, room) { + handleMessage(event, room, callback) { logger.debug("[%s] [%s] [%s]", this.name, room.name, event.event.content.body); - return message.createBasic(this.name + " processed the message"); + callback( + room, + message.createBasic(this.name + " processed the message") + ); } help(event, room) {