-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart_bot.js
More file actions
156 lines (145 loc) · 3.85 KB
/
start_bot.js
File metadata and controls
156 lines (145 loc) · 3.85 KB
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
import * as fs from 'fs';
import * as path from'path';
import { spawn } from 'child_process';
import { CompRetty } from './lib/compretty.js';
import * as dotenv from 'dotenv';
dotenv.config();
var isQuiet = false;
var logPath = path.join(process.cwd(), 'bot.log');
var shouldRestart = false;
function _displayHelp() {
console.log(
'Options:' +
'\n-h / --help Display this page.' +
'\n-q / --quiet\n Do not log info and warnings into console.\n The \'-s\' option will not be affected.' +
'\n-s / --save-log\n Save script log to a file.\n Default path is \'bot.log\' in parent directory, custom path can be specified with \'-o\'.' +
'\n-o / --output <filepath> Specify a custom log output file path instead of \'bot.log\'.' +
'\n-r / --restart Automatically restart this script on crash.' +
'\n--raw\n Execute a raw run.\n The script will ignore all options and its output will not be formatted.'
);
}
function log(value) {
if (typeof value != 'string') {
value = value.toString();
}
if (isQuiet) {
var _0 = value[0];
if (_0 != 'F' && _0 != 'S') return;
if (value[1] != ':') return;
}
console.log(value.slice(0, -1));
}
function runRaw() {
var _exitDone = false;
var _compretty;
function exit() {
if (_exitDone) return;
_exitDone = true;
_compretty.stop();
console.log('S:Waiting 30 seconds for any file operations to finish.');
setTimeout((function() {
return process.exit(0);
}), 30000);
}
process.on('SIGINT', () => exit());
process.on('SIGTERM', () => exit());
_compretty = CompRetty.fromENV();
_compretty.start();
}
function main() {
function onSaveLog() {
log = (value) => {
if (typeof value != 'string') {
value = value.toString();
}
fs.appendFileSync(logPath, value);
if (isQuiet) {
var _0 = value[0];
if (_0 != 'F' && _0 != 'S') return;
if (value[1] != ':') return;
}
console.log(value.slice(0, -1));
};
}
for (var i = 0; i != process.argv.length; i++) {
function _onOutput(onMissing) {
if (i == process.argv.length - 1) {
log(`F:Command option \'${process.argv[i]}\' is missing a value.\n`);
process.exit(1);
}
logPath = process.argv[++i];
}
switch (process.argv[i]) {
case '--raw':
runRaw();
return;
case 'help':
_displayHelp();
return;
case '-h':
_displayHelp();
return;
case '--help':
_displayHelp();
return;
case '-q':
isQuiet = true;
break;
case '--quiet':
isQuiet = true;
break;
case '-s':
onSaveLog();
break;
case '--save-log':
onSaveLog();
break;
case '-o':
_onOutput();
break;
case '--output':
_onOutput();
break;
case '-r':
shouldRestart = true;
break;
case '--restart':
shouldRestart = true;
break;
}
}
async function runBot(onClose = null) {
var _exiting = false;
var child = spawn('node', ['start_bot.js', '--raw']);
process.on('SIGINT', () => {
child.kill('SIGINT');
_exiting = true;
});
process.on('SIGTERM', () => {
child.kill('SIGTERM');
_exiting = true;
});
child.stdout.on('data', (data) => {
log(data);
});
child.stderr.on('data', (data) => {
log(`F:\n ${data}`);
});
child.on('close', (code) => {
log(`S:Bot exited with code ${code}.\n`);
if (code == 0) process.exit(0);
if (onClose != null) onClose(code);
if (_exiting) process.exit(code);
});
}
if (shouldRestart) {
function _restartBot() {
log('S:Restarting.\n');
runBot(() => _restartBot());
}
runBot(() => _restartBot());
return;
}
runBot((code) => process.exit(code));
}
main();