|
|
@ -1,30 +1,31 @@ |
|
|
|
let sdk = require('matrix-js-sdk'); |
|
|
|
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"); |
|
|
|
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) { |
|
|
|
case "PREPARED": |
|
|
|
bot.connected = true; |
|
|
|
bot.sendStatusStartup(); |
|
|
|
this.bot.connected = true; |
|
|
|
this.bot.sendStatusStartup(); |
|
|
|
break; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
bot.client.on("RoomMember.membership", (event, member) => { |
|
|
|
this.bot.client.on("RoomMember.membership", (event, member) => { |
|
|
|
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); |
|
|
|
}); |
|
|
|
} |
|
|
@ -34,10 +35,10 @@ function init(bot) { |
|
|
|
|
|
|
|
["SIGINT", "SIGTERM"].forEach((signature) => { |
|
|
|
process.on(signature, async () => { |
|
|
|
await bot.sendStatusShutdown() |
|
|
|
await this.bot.sendStatusShutdown() |
|
|
|
.then(() => { |
|
|
|
logger.info("Gracefully stopping Matrix SDK Client") |
|
|
|
bot.client.stopClient(); |
|
|
|
this.bot.client.stopClient(); |
|
|
|
}); |
|
|
|
process.exit(0); |
|
|
|
}); |
|
|
@ -47,15 +48,26 @@ function init(bot) { |
|
|
|
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");
|
|
|
|
// await bot.client.initCrypto();
|
|
|
|
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; |