-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
64 lines (56 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { App } from 'modapp';
import loadConfig from './utils/loadConfig.js';
import loadModules from './utils/loadModules.js';
import parseArgs from './utils/parseArgs.js';
let moduleConfig = {};
try {
// Parse configuration from arguments
let { config, path } = parseArgs(process.argv.slice(2));
// Load configuration from .js, .mjs, or .json file
let fileConfig = await loadConfig(path);
// Merge argument and file configuration with priority to command arguments.
moduleConfig = Object.assign(moduleConfig, fileConfig);
if (config) {
for (let k in config) {
moduleConfig[k] = Object.assign({}, moduleConfig[k], config[k]);
}
}
console.log("Config: ", moduleConfig);
} catch (e) {
console.error("Loading configuration failed:", e);
process.exit(1);
}
let modules;
try {
// Load all modules into a bundle
modules = await loadModules("./modules");
} catch (e) {
console.error("Loading modules failed:", e);
process.exit(1);
}
function botUnsubscribed(ev, bot) {
console.log("Bot no longer logged in.");
bot.off('unsubscribe', botUnsubscribed);
process.exit(0);
}
// Start bot instance
try {
// Create the app. This will result in all the modules being created and
// initialized. The app in itself has no entry point, so it is up to the
// modules to do whatever needs to be done.
let app = new App(moduleConfig);
let result = await app.loadBundle(modules);
// Show results of loading the module bundle
console.log("Loaded modules: ", Object.keys(result.modules));
if (result.errors) {
console.error("Disabled modules: ", result.errors);
}
// Get bot module and try to login, to bootstrap it all.
let botModule = result.modules.bot;
let bot = await botModule.login();
console.log("Logged in with " + (bot.char?.name || "bot"));
// Exit once we are no longer logged in.
bot.on('unsubscribe', botUnsubscribed);
} catch (e) {
console.error("Starting bot failed:", e);
}