From 08ea447adde4a78e5350f527a8774d5320de1f65 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Sun, 29 Dec 2019 15:23:30 -0600 Subject: [PATCH] Functionality improvements and code cleanup * Added message handling stub * Cleaned up client init and config * Added timestamp to logging --- bot/bot.js | 37 ++++++++++++++++++++++++++++++++++++- bot/engine.js | 41 ++++++++++++++--------------------------- bot/logging.js | 1 + 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/bot/bot.js b/bot/bot.js index f78aeec..437d8a2 100644 --- a/bot/bot.js +++ b/bot/bot.js @@ -13,13 +13,48 @@ class Bot { /** * Initialize the bot connection */ - init() { + init(messageCallback) { logger.info("Creating Matrix Client") this.client = sdk.createClient({ baseUrl: this.config.baseUrl, accessToken: this.config.accessToken, userId: this.config.userId }); + + this.client.on("sync", (state, previousState, data) => { + switch (state) { + case "PREPARED": + this.connected = true; + this.sendStatusStartup(); + break; + case "SYNCING": + logger.debug("Syncing") + break; + default: + logger.error("Unexpected sync state: %s", state); + 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(() => { + logger.info("Auto-joined %s", member.roomId); + }); + } + }); + + this.client.on("Room.timeline", messageCallback); + + return this; + } + + connect() { + // logger.info("Initializing Crypto"); + // await bot.client.initCrypto(); + logger.info("Starting Matrix SDK Client"); + this.client.startClient(); } sendStatusStartup() { diff --git a/bot/engine.js b/bot/engine.js index b206052..a55fb84 100644 --- a/bot/engine.js +++ b/bot/engine.js @@ -10,29 +10,19 @@ class Engine { init() { logger.info("Initializing modules"); this.initModules(); - - this.bot.init(); - - this.bot.client.on("sync", (state, previousState, data) => { - switch (state) { - case "PREPARED": - this.bot.connected = true; - this.bot.sendStatusStartup(); - break; - } - }); - - this.bot.client.on("RoomMember.membership", (event, member) => { - if (member.membership === "invite" - && this.bot.config.admin.indexOf(ember.userId) >= 0) { - this.bot.client.joinRoom(member.roomId).done(() => { - logger.info("Auto-joined %s", member.roomId); - }); + + /* Bind Message Parsing */ + let handleMessages = function (event, room, toStartOfTimeline) { + if (event.getType() !== "m.room.message") { + return; // only use messages } - }); - + logger.debug("[%s] %s", room.name, event.event.content.body); + } + + this.bot.init(handleMessages); + /* Capture Exit Conditions */ - + ["SIGINT", "SIGTERM"].forEach((signature) => { process.on(signature, async () => { await this.bot.sendStatusShutdown() @@ -43,11 +33,11 @@ class Engine { process.exit(0); }); }); - + process.on('exit', () => { logger.info("Shutting Down"); }); - + return this; } @@ -58,10 +48,7 @@ class Engine { } run() { - // logger.info("Initializing Crypto"); - // await bot.client.initCrypto(); - logger.info("Starting Matrix SDK Client"); - this.bot.client.startClient(); + this.bot.connect(); return this; } } diff --git a/bot/logging.js b/bot/logging.js index 0c5c87b..2b0a863 100644 --- a/bot/logging.js +++ b/bot/logging.js @@ -3,6 +3,7 @@ let winston = require('winston'); let logger = winston.createLogger({ level: 'info', format: winston.format.combine( + winston.format.timestamp(), winston.format.splat(), winston.format.json() ),