let { logger } = require('./logging'); let { modules } = require('./module/index') class Engine { constructor(config, bot, modules) { this.config = config this.bot = bot; this.modules = modules; } init() { logger.info("Initializing modules"); this.initModules(); /* Bind Message Parsing */ let handleMessages = function (event, room, toStartOfTimeline) { if (event.getType() !== "m.room.message") { logger.debug("Recieved message of type: %s", event.getType()); 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() .then(() => { logger.info("Gracefully stopping Matrix SDK Client") this.bot.client.stopClient(); }); process.exit(0); }); }); process.on('exit', () => { logger.info("Shutting Down"); }); return this; } initModules() { this.modules.forEach((module) => { logger.info("Loading module: %s", module.name); }); } run() { this.bot.connect(); return this; } } function create(config, bot) { return new Engine(config, bot, modules) } exports.create = create;