-
Notifications
You must be signed in to change notification settings - Fork 4
/
start_bot.js
182 lines (167 loc) · 4.58 KB
/
start_bot.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as fs from 'fs';
import * as path from'path';
import { spawn } from 'child_process';
import { Interlinked, InterlinkedDiscordJSON } from './lib/interlinked.js';
const configPath = path.join(process.cwd(), 'config.json');
const discordBotTokenErrMsg = 'F:Discord bot token not found in \'config.json\'\n';
const steamAPIKeyErrMsg = 'F:Steam API key not found in \'config.json\'\n';
const prefsFilePath = path.join(process.cwd(), 'usrprefs.json');
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() {
function onSave(json) {
fs.writeFileSync(prefsFilePath, JSON.stringify(json));
}
try {
fs.accessSync(configPath);
} catch (err) {
log('F:Config not found under \'config.json\'\n');
process.exit(1);
}
var _config = null;
try {
_config = JSON.parse(fs.readFileSync('config.json'));
}
catch (e) {
log(`F:Invalid JSON in \'config.json\'\n${e}\n`);
process.exit(1);
}
if (_config.discordBotToken == null) {
log(discordBotTokenErrMsg);
process.exit(1);
}
if (_config.discordBotToken == '') {
log('W:Discord bot token is empty, will not use Discord bot features.\n');
}
if (_config.steamAPIKey == null) {
log(steamAPIKeyErrMsg);
process.exit(1);
}
if (_config.steamAPIKey == '') {
log(steamAPIKeyErrMsg);
process.exit(1);
}
try {
fs.accessSync(prefsFilePath);
} catch (e) {
var _fd = fs.openSync(prefsFilePath, 'w+');
fs.writeSync(_fd, JSON.stringify(new InterlinkedDiscordJSON().toJSON()));
fs.closeSync(_fd);
}
var _interlinked = new Interlinked(
_config,
fs.readFileSync(prefsFilePath, { encoding: 'utf8', flag: 'r' }),
(json) => onSave(json),
console.log,
);
_interlinked.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 child = spawn('node', ['start_bot.js', '--raw']);
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 (onClose != null) onClose();
});
}
if (shouldRestart) {
function _restartBot() {
log('S:Restarting.\n');
runBot(() => _restartBot());
}
runBot(() => _restartBot());
return;
}
runBot();
}
main();