-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
109 lines (85 loc) · 3.19 KB
/
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
const Logger = require('@lilywonhalf/pretty-logger');
const mainProcess = () => {
const ChildProcess = require('child_process');
process.on('uncaughtException', Logger.exception);
Logger.info('Spawning bot subprocess...');
const args = [process.argv[1], 'bot'];
let botProcess = ChildProcess.spawn(process.argv[0], args);
const stdLog = (callback) => {
return (data) => {
const wantToDie = data.toString().toLowerCase().indexOf('killbotpls') > -1;
const reboot = data.toString().toLowerCase().indexOf('reboot') > -1
|| data.toString().toLowerCase().indexOf('econnreset') > -1
|| data.toString().toLowerCase().indexOf('etimedout') > -1;
data = data.toString().replace(/\n$/, '').split('\n');
data.map(datum => callback('|-- ' + datum));
if (wantToDie) {
Logger.info('Asked to kill');
botProcess.kill('SIGHUP');
process.exit(0);
}
if (reboot) {
botProcess.kill();
}
};
};
const bindProcess = (subprocess) => {
subprocess.stdout.on('data', stdLog(console.log));
subprocess.stderr.on('data', stdLog(console.error));
subprocess.on('close', (code) => {
Logger.error(`Bot subprocess exited with code ${code}`);
if (code !== 0) {
botProcess = ChildProcess.spawn(
process.argv[0],
args.concat(['--reboot'])
);
bindProcess(botProcess);
}
});
};
bindProcess(botProcess);
Logger.info('Bot subprocess spawned');
};
const botProcess = () => {
const { Client } = require('discord.js');
global.bot = new Client({ fetchAllMembers: true });
global.isRightGuild = (guildSnowflake) => guildSnowflake === Config.guild;
const Config = require('./config.json');
const Command = require('./model/command');
const fs = require('fs');
require('./model/globals');
require('./model/timer');
const crashRecover = (exception) => {
Logger.exception(exception);
Logger.notice('Need reboot');
};
process.on('uncaughtException', crashRecover);
bot.on('error', crashRecover);
Command.init();
const help = require("./command/help");
bot.ws.on('INTERACTION_CREATE', help.interactionHandler);
bot.on('ready', () => {
fs.readdirSync('./event/')
.filter(filename => filename.endsWith('.js'))
.map(filename => filename.substr(0, filename.length - 3))
.forEach(filename => {
const event = filename.replace(/([_-][a-z])/gu, character => `${character.substr(1).toUpperCase()}`);
if (filename !== 'ready') {
bot.on(event, require(`./event/${filename}`));
} else {
require(`./event/${filename}`)();
}
});
});
Logger.info('--------');
Logger.info('Logging in...');
bot.login(Config.token);
};
switch (process.argv[2]) {
case 'bot':
botProcess();
break;
default:
mainProcess();
break;
}