Drew Short
5 years ago
3 changed files with 78 additions and 62 deletions
-
19bot/bot.js
-
110bot/engine.js
-
9index.js
@ -1,61 +1,73 @@ |
|||
let sdk = require('matrix-js-sdk'); |
|||
let { logger } = require('./logging'); |
|||
let { modules } = require('./module/index') |
|||
|
|||
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); |
|||
}); |
|||
} |
|||
}); |
|||
class Engine { |
|||
constructor(bot, modules) { |
|||
this.bot = bot; |
|||
this.modules = modules; |
|||
} |
|||
|
|||
init() { |
|||
logger.info("Initializing modules"); |
|||
this.initModules(); |
|||
|
|||
this.bot.init(); |
|||
|
|||
/* Capture Exit Conditions */ |
|||
this.bot.client.on("sync", (state, previousState, data) => { |
|||
switch (state) { |
|||
case "PREPARED": |
|||
this.bot.connected = true; |
|||
this.bot.sendStatusStartup(); |
|||
break; |
|||
} |
|||
}); |
|||
|
|||
["SIGINT", "SIGTERM"].forEach((signature) => { |
|||
process.on(signature, async () => { |
|||
await bot.sendStatusShutdown() |
|||
.then(() => { |
|||
logger.info("Gracefully stopping Matrix SDK Client") |
|||
bot.client.stopClient(); |
|||
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"); |
|||
}); |
|||
}); |
|||
|
|||
process.on('exit', () => { |
|||
logger.info("Shutting Down"); |
|||
}); |
|||
return this; |
|||
} |
|||
|
|||
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; |
@ -1,7 +1,6 @@ |
|||
let bot = require('./bot/bot'); |
|||
let engine = require('./bot/engine') |
|||
engine.run( |
|||
engine.init( |
|||
bot.create("../data/config.json") |
|||
) |
|||
); |
|||
|
|||
engine.create( |
|||
bot.create("../data/config.json") |
|||
).init().run(); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue