This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·103 lines (85 loc) · 2.66 KB
/
app.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
#!/usr/bin/env node
function load(persistObjs, startTime, isReload) {
require('./lib/polyfill'); //load polyfill
var AKP48 = require('./lib/AKP48');
var config;
var logger;
try {
config = require(getConfigFile());
} catch(e) {
//check for SyntaxError and quit if so.
if(e instanceof SyntaxError) {
console.log(`The provided config file includes a syntax error: ${e.message}`);
console.log(`Fix your JSON syntax and restart the program.`);
process.exit(0);
return;
}
console.log('No config file found.');
//no config, so set config to null.
config = null;
}
try {
logger = require('./lib/Logger')(config.logger.level || 'info', config.logger.fileLevel || 'info', getConfigDir());
} catch(e) {
logger = require('./lib/Logger')('info', 'info', getConfigDir());
}
try {
if(!config.productionMode) {
require('longjohn');
}
} catch(e) {}
logger.info('AKP48 is starting.');
logger.info(`Configuration loaded from ${getConfigFile()}.`);
//logger goes in global scope.
global.logger = logger;
//load the bot.
global.AKP48 = new AKP48(config, {dir: getConfigDir(), file: getConfigFile()}, reload, startTime);
//loads all available plugins.
global.AKP48.loadPlugins().then(function() {
//start the bot.
global.AKP48.start(persistObjs).then(function(){
global.AKP48.emit('loadFinished');
if(isReload) {
global.AKP48.sendAlert('Reload complete.');
}
});
});
}
function reload(persistObjs, startTime) {
delete global.AKP48;
global.logger.info('Reloading AKP48.');
delete global.logger;
load(persistObjs, startTime, true);
}
//flags contains a nice object with our arguments. See https://www.npmjs.com/package/minimist for more details.
var flags = require('minimist')(process.argv.slice(2), {alias: {
dir: 'd',
config: 'conf',
}, default: {
config: 'config.json',
}});
function getConfigDir() {
var path = require('path');
var homedir = require('homedir');
var dir = flags.dir;
if(dir) {
return path.resolve(path.dirname(dir));
}
switch(process.platform) {
case 'darwin':
return path.join(process.env.HOME, 'Library/Application Support/AKP48Squared');
case 'win32':
return path.join(process.env.APPDATA, 'AKP48Squared');
default:
return path.join(homedir(), '.akp48squared');
}
}
function getConfigFile() {
var path = require('path');
return path.resolve(getConfigDir(), flags.config);
}
load();
process.on('uncaughtException', function(err) {
global.logger.error(`Uncaught Exception! Error: ${err.name}, Code: ${err.code || 'null'}, Message: ${err.message || 'null'}`);
console.log(err.stack);
});