Browse Source

Additional code cleanup for engine and bot classes

pull/10/head
Drew Short 5 years ago
parent
commit
f866db1fad
  1. 19
      bot/bot.js
  2. 56
      bot/engine.js
  3. 7
      index.js

19
bot/bot.js

@ -1,19 +1,24 @@
let sdk = require('matrix-js-sdk');
let message = require('./message'); let message = require('./message');
let utility = require('./utility'); let utility = require('./utility');
let { logger } = require('./logging'); let { logger } = require('./logging');
let { modules } = require('./module/index')
class Bot { class Bot {
constructor(config, buildInfo, modules) {
constructor(config, buildInfo) {
this.config = config; this.config = config;
this.buildInfo = buildInfo; this.buildInfo = buildInfo;
this.connected = false; this.connected = false;
this.modules = modules;
} }
initModules() {
modules.forEach((module) => {
logger.info("Loading module: %s", module.name);
/**
* Initialize the bot connection
*/
init() {
logger.info("Creating Matrix Client")
this.client = sdk.createClient({
baseUrl: this.config.baseUrl,
accessToken: this.config.accessToken,
userId: this.config.userId
}); });
} }
@ -64,7 +69,7 @@ function create(configFile) {
logger.debug("%o", sanitizeConfig(config)); logger.debug("%o", sanitizeConfig(config));
let buildInfo = utility.getBuildInfo("../build.info") let buildInfo = utility.getBuildInfo("../build.info")
logger.info("Running version: %s", buildInfo); logger.info("Running version: %s", buildInfo);
return new Bot(config, buildInfo, modules);
return new Bot(config, buildInfo);
} }
exports.create = create; exports.create = create;

56
bot/engine.js

@ -1,30 +1,31 @@
let sdk = require('matrix-js-sdk');
let { logger } = require('./logging'); let { logger } = require('./logging');
let { modules } = require('./module/index')
function init(bot) {
class Engine {
constructor(bot, modules) {
this.bot = bot;
this.modules = modules;
}
init() {
logger.info("Initializing modules"); logger.info("Initializing modules");
bot.initModules();
this.initModules();
logger.info("Creating Matrix Client")
bot.client = sdk.createClient({
baseUrl: bot.config.baseUrl,
accessToken: bot.config.accessToken,
userId: bot.config.userId
});
this.bot.init();
bot.client.on("sync", (state, previousState, data) => {
this.bot.client.on("sync", (state, previousState, data) => {
switch (state) { switch (state) {
case "PREPARED": case "PREPARED":
bot.connected = true;
bot.sendStatusStartup();
this.bot.connected = true;
this.bot.sendStatusStartup();
break; break;
} }
}); });
bot.client.on("RoomMember.membership", (event, member) => {
this.bot.client.on("RoomMember.membership", (event, member) => {
if (member.membership === "invite" if (member.membership === "invite"
&& bot.config.admin.indexOf(ember.userId) >= 0) {
bot.client.joinRoom(member.roomId).done(() => {
&& this.bot.config.admin.indexOf(ember.userId) >= 0) {
this.bot.client.joinRoom(member.roomId).done(() => {
logger.info("Auto-joined %s", member.roomId); logger.info("Auto-joined %s", member.roomId);
}); });
} }
@ -34,10 +35,10 @@ function init(bot) {
["SIGINT", "SIGTERM"].forEach((signature) => { ["SIGINT", "SIGTERM"].forEach((signature) => {
process.on(signature, async () => { process.on(signature, async () => {
await bot.sendStatusShutdown()
await this.bot.sendStatusShutdown()
.then(() => { .then(() => {
logger.info("Gracefully stopping Matrix SDK Client") logger.info("Gracefully stopping Matrix SDK Client")
bot.client.stopClient();
this.bot.client.stopClient();
}); });
process.exit(0); process.exit(0);
}); });
@ -47,15 +48,26 @@ function init(bot) {
logger.info("Shutting Down"); logger.info("Shutting Down");
}); });
return bot;
return this;
} }
function run(bot) {
initModules() {
this.modules.forEach((module) => {
logger.info("Loading module: %s", module.name);
});
}
run() {
// 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");
bot.client.startClient();
this.bot.client.startClient();
return this;
}
}
function create(bot) {
return new Engine(bot, modules)
} }
exports.init = init;
exports.run = run;
exports.create = create;

7
index.js

@ -1,7 +1,6 @@
let bot = require('./bot/bot'); let bot = require('./bot/bot');
let engine = require('./bot/engine') let engine = require('./bot/engine')
engine.run(
engine.init(
engine.create(
bot.create("../data/config.json") bot.create("../data/config.json")
)
);
).init().run();
Loading…
Cancel
Save