Browse Source

Code cleanup

* Added engine module
* Moved tools into utility module
pull/10/head
Drew Short 5 years ago
parent
commit
fa91dc468b
  1. 76
      bot/bot.js
  2. 61
      bot/engine.js
  3. 22
      bot/utility.js
  4. 7
      index.js

76
bot/bot.js

@ -1,5 +1,3 @@
let fs = require('fs');
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');
@ -54,18 +52,6 @@ class Bot {
} }
} }
function getBuildInfo(buildInfoPath) {
try {
return fs.readFileSync(buildInfoPath, "utf8");
} catch (err) {
if (err.code === 'ENOENT') {
return "UNKNOWN_" + utility.toISODateString(new Date());
} else {
logger.error("Unexpected Error!", err);
}
}
}
function sanitizeConfig(config) { function sanitizeConfig(config) {
let clonedConfig = { ...config }; let clonedConfig = { ...config };
clonedConfig.accessToken = "******" clonedConfig.accessToken = "******"
@ -76,67 +62,9 @@ function create(configFile) {
let config = require(configFile); let config = require(configFile);
logger.info("Running with config:"); logger.info("Running with config:");
logger.debug("%o", sanitizeConfig(config)); logger.debug("%o", sanitizeConfig(config));
let buildInfo = 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, modules);
} }
function init(bot) {
logger.info("Initializing modules");
bot.initModules();
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();
});
process.exit(0);
});
});
process.on('exit', () => {
logger.info("Shutting Down");
});
return bot;
}
function run(bot) {
// logger.info("Initializing Crypto");
// await bot.client.initCrypto();
logger.info("Starting Matrix SDK Client");
bot.client.startClient();
}
exports.create = create;
exports.init = init;
exports.run = run;
exports.create = create;

61
bot/engine.js

@ -0,0 +1,61 @@
let sdk = require('matrix-js-sdk');
let { logger } = require('./logging');
function init(bot) {
logger.info("Initializing modules");
bot.initModules();
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();
});
process.exit(0);
});
});
process.on('exit', () => {
logger.info("Shutting Down");
});
return bot;
}
function run(bot) {
// logger.info("Initializing Crypto");
// await bot.client.initCrypto();
logger.info("Starting Matrix SDK Client");
bot.client.startClient();
}
exports.init = init;
exports.run = run;

22
bot/utility.js

@ -1,3 +1,6 @@
let fs = require('fs');
let { logger } = require('./logging');
function toISODateString(d) { function toISODateString(d) {
function pad(n) { return n < 10 ? '0' + n : n } function pad(n) { return n < 10 ? '0' + n : n }
return d.getUTCFullYear() + '-' return d.getUTCFullYear() + '-'
@ -8,11 +11,24 @@ function toISODateString(d) {
+ pad(d.getUTCSeconds()) + 'Z' + pad(d.getUTCSeconds()) + 'Z'
} }
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
function getBuildInfo(buildInfoPath) {
try {
return fs.readFileSync(buildInfoPath, "utf8");
} catch (err) {
if (err.code === 'ENOENT') {
return "UNKNOWN_" + toISODateString(new Date());
} else {
logger.error("Unexpected Error!", err);
}
}
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
}) })
} }
exports.toISODateString = toISODateString; exports.toISODateString = toISODateString;
exports.getBuildInfo = getBuildInfo;
exports.sleep = sleep; exports.sleep = sleep;

7
index.js

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