Browse Source

Functionality improvements and code cleanup

* Added message handling stub
* Cleaned up client init and config
* Added timestamp to logging
pull/10/head
Drew Short 5 years ago
parent
commit
08ea447add
  1. 37
      bot/bot.js
  2. 29
      bot/engine.js
  3. 1
      bot/logging.js

37
bot/bot.js

@ -13,13 +13,48 @@ class Bot {
/** /**
* Initialize the bot connection * Initialize the bot connection
*/ */
init() {
init(messageCallback) {
logger.info("Creating Matrix Client") logger.info("Creating Matrix Client")
this.client = sdk.createClient({ this.client = sdk.createClient({
baseUrl: this.config.baseUrl, baseUrl: this.config.baseUrl,
accessToken: this.config.accessToken, accessToken: this.config.accessToken,
userId: this.config.userId userId: this.config.userId
}); });
this.client.on("sync", (state, previousState, data) => {
switch (state) {
case "PREPARED":
this.connected = true;
this.sendStatusStartup();
break;
case "SYNCING":
logger.debug("Syncing")
break;
default:
logger.error("Unexpected sync state: %s", state);
process.exit(1);
}
});
this.client.on("RoomMember.membership", (event, member) => {
if (member.membership === "invite"
&& this.config.admin.indexOf(ember.userId) >= 0) {
this.client.joinRoom(member.roomId).done(() => {
logger.info("Auto-joined %s", member.roomId);
});
}
});
this.client.on("Room.timeline", messageCallback);
return this;
}
connect() {
// logger.info("Initializing Crypto");
// await bot.client.initCrypto();
logger.info("Starting Matrix SDK Client");
this.client.startClient();
} }
sendStatusStartup() { sendStatusStartup() {

29
bot/engine.js

@ -11,25 +11,15 @@ class Engine {
logger.info("Initializing modules"); logger.info("Initializing modules");
this.initModules(); 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;
/* Bind Message Parsing */
let handleMessages = function (event, room, toStartOfTimeline) {
if (event.getType() !== "m.room.message") {
return; // only use messages
} }
});
logger.debug("[%s] %s", room.name, event.event.content.body);
}
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);
});
}
});
this.bot.init(handleMessages);
/* Capture Exit Conditions */ /* Capture Exit Conditions */
@ -58,10 +48,7 @@ class Engine {
} }
run() { run() {
// logger.info("Initializing Crypto");
// await bot.client.initCrypto();
logger.info("Starting Matrix SDK Client");
this.bot.client.startClient();
this.bot.connect();
return this; return this;
} }
} }

1
bot/logging.js

@ -3,6 +3,7 @@ let winston = require('winston');
let logger = winston.createLogger({ let logger = winston.createLogger({
level: 'info', level: 'info',
format: winston.format.combine( format: winston.format.combine(
winston.format.timestamp(),
winston.format.splat(), winston.format.splat(),
winston.format.json() winston.format.json()
), ),

Loading…
Cancel
Save