This repository has been archived by the owner on Jul 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
164 lines (136 loc) · 4.29 KB
/
application.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/** @ignore */
const utils = require('util');
/** @ignore */
const _ = require('lodash');
/** @ignore */
const directory = require('require-directory');
/** @ignore */
const Discordie = require('discordie');
class Application {
/**
* Bootstraps the application and prepares it for use.
*
* @return {Promise}
*/
bootstrap() {
global.app = require('./app');
app.bot.jobs = {};
app.logger.info(`Bootstraping AvaCentral v${app.version}`);
this.prepareConfig();
this.prepareDiscordie();
return this.prepareDatabase();
}
/**
* Registers and prepares the events, jobs and services that Ava uses.
*/
register() {
app.logger.info('Registering events & services');
this.registerEvents();
this.registerJobs();
}
/**
* Connects the bot to the Discord network.
*/
connect() {
app.logger.info('Connecting to the Discord network...');
bot.connect({token: app.config.token});
bot.User.setStatus(null, 'avairebot.com');
}
/**
* Loads the config from file and stores it in the global app variable.
*/
prepareConfig() {
app.config = app.configLoader.loadConfiguration('config.json');
}
/**
* Loads and prepares the database by storing it in the global app variable,
* connecting to the database, and running any migrations needs to be run.
*
* @return {Promise}
*/
prepareDatabase() {
app.logger.info(' - Setting up database and tables');
const Database = require('./app/database/Database');
app.database = new Database();
return app.database.runMigrations();
}
/**
* Prepares the bot instance and stores it in the global bot variable.
*/
prepareDiscordie() {
app.logger.info(' - Creating bot instance');
let options = this.buildDiscordOptions();
options.autoReconnect = true;
global.bot = new Discordie(options);
}
/**
* Registers the Discordie and process events.
*/
registerEvents() {
app.logger.info(` - Registering ${Object.keys(app.bot.handlers).length + 1} event handlers`);
bot.Dispatcher.onAny((type, socket) => {
if (app.bot.handlers.hasOwnProperty(type)) {
return app.bot.handlers[type].handle(socket);
}
});
process.on('unhandledRejection', (reason, p) => {
return app.logger.info(`Unhandled promise: ${utils.inspect(p, {depth: 3})}: ${reason}`);
});
}
/**
* Registers the jobs.
*/
registerJobs() {
_.each(directory(module, './app/bot/jobs'), (Job, key) => {
if (key !== 'Job') {
app.bot.jobs[key] = app.scheduler.registerJob(new Job);
}
});
app.logger.info(` - Registering ${Object.keys(app.bot.jobs).length} jobs`);
}
/**
* Builds the Discordie options object based off the
* provided arguments parsed to the start.js file.
*/
buildDiscordOptions() {
let options = {};
let parsers = [
{
trigger: ['--shard-id', '--shardid', '-sid'],
value: 'shardId'
},
{
trigger: ['--shard-count', '--shardcount', '-scount', '-sc'],
value: 'shardCount'
},
{
trigger: ['--message-cache-limit', '--message-limit', '-mcl'],
value: 'messageCacheLimit'
}
];
for (let i in process.argv) {
let opt = process.argv[i].toLowerCase();
if (opt.indexOf('=') < 0) {
continue;
}
let parserValue = null;
let parts = opt.split('=');
for (let x in parsers) {
if (parserValue !== null) {
continue;
}
let parser = parsers[x];
if (parser.trigger.indexOf(parts[0]) < 0) {
continue;
}
parserValue = parser.value;
}
if (parserValue === null) {
continue;
}
options[parserValue] = parseInt(parts[1], 10);
}
return options;
}
}
module.exports = new Application;