Browse Source

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
pull/10/head
Drew Short 5 years ago
parent
commit
4cce06e2db
  1. 12
      bot/bot.js
  2. 2
      bot/config.js
  3. 48
      bot/engine.js
  4. 3
      bot/logging.js
  5. 7
      bot/module/abstract.js

12
bot/bot.js

@ -21,11 +21,15 @@ class Bot {
userId: this.config.userId userId: this.config.userId
}); });
this.client.on("sync", (state, previousState, data) => {
this.client.on("sync", async (state, previousState, data) => {
switch (state) { switch (state) {
case "PREPARED": case "PREPARED":
this.connected = true; this.connected = true;
this.sendStatusStartup();
await this.sendStatusStartup();
this.client.getJoinedRooms()
.done((rooms) => {
logger.info("Connected to: %o", rooms)
});
break; break;
case "SYNCING": case "SYNCING":
logger.debug("Syncing") logger.debug("Syncing")
@ -57,9 +61,7 @@ class Bot {
// logger.info("Initializing Crypto"); // logger.info("Initializing Crypto");
// await bot.client.initCrypto(); // await bot.client.initCrypto();
logger.info("Starting Matrix SDK Client"); logger.info("Starting Matrix SDK Client");
await this.client.startClient({
initialSyncLimit: 0
});
await this.client.startClient();
} }
sendStatusStartup() { sendStatusStartup() {

2
bot/config.js

@ -13,7 +13,7 @@ function getConfig(configFile) {
if (config === null) { if (config === null) {
configPath = configFile; configPath = configFile;
config = require(configFile); config = require(configFile);
logger.info("Leaded config:");
logger.info("Loaded config:");
logger.debug("%o", sanitizeConfig(config)); logger.debug("%o", sanitizeConfig(config));
} }
return config; return config;

48
bot/engine.js

@ -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)
} }

3
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 logger.level = process.env.LOG_LEVEL
} }

7
bot/module/abstract.js

@ -16,9 +16,12 @@ class AbstractModule {
this.command = command; this.command = command;
} }
handleMessage(event, room) {
handleMessage(event, room, callback) {
logger.debug("[%s] [%s] [%s]", this.name, room.name, event.event.content.body); 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) { help(event, room) {

Loading…
Cancel
Save