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. 112
      bot/engine.js
  3. 9
      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;

112
bot/engine.js

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

9
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(
bot.create("../data/config.json")
)
);
engine.create(
bot.create("../data/config.json")
).init().run();
Loading…
Cancel
Save