-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
76 lines (61 loc) · 1.67 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
const Discord = require('discord.js');
const winston = require('winston');
const npid = require('npid');
const config = require('./config');
const replay = require('./replay');
const unit = require('./unit');
winston.level = 'debug';
winston.info('Launching...');
if (config.bot.pid_file) {
winston.info(`Creating PID file: ${config.bot.pid_file}`);
try {
const pid = npid.create(config.bot.pid_file);
pid.removeOnExit();
} catch (e) {
winston.error(e);
process.exit(1);
}
}
const client = new Discord.Client();
client.on('ready', () => {
winston.info('Ready.');
});
client.on('message', message => {
if (message.system || message.author.bot) {
return;
}
if (message.channel instanceof Discord.DMChannel) {
winston.info(`Private message from ${message.author.tag}: ${message.content}`);
if (message.content === 'ping') {
message.reply('pong');
return;
}
}
replay.handleMessage(message);
unit.handleMessage(message);
});
client.on('reconnecting', () => {
winston.info('Reconnecting.');
});
client.on('disconnect', () => {
winston.info('Disconnected. Shutting down.');
process.exit();
});
client.on('debug', info => {
winston.debug('Client:', info);
});
client.on('warn', info => {
winston.warn('Client:', info);
});
client.on('error', error => {
winston.error('Client:', error);
});
client.login(config.bot.login_token);
process.on('SIGINT', () => {
winston.warn('Caught interrupt signal.');
if (client.readyAt !== undefined && client.readyAt !== null) {
client.destroy();
} else {
process.exit();
}
});