-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
224 lines (193 loc) · 7.39 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
let _ = require('lodash');
let telnet = require('telnet-client');
let decoder = new (require('string_decoder').StringDecoder)('ascii');
let telnet_client = new telnet();
let Player = require('./player');
let Players = new Array();
/*
Discord Config
*/
let Discord = require('discord.js');
let discord_client = new Discord.Client();
let discord_token = 'MjYzMzMxNDQ0ODU0NjIwMTgw.C0QeSg.SJbTc5eA-0EzXRnRhAnS5MbFGo4';
let discord_channel = 'bot';
let telnet_params = {
host: 'play.7daystociv.com',
port: 8081,
loginPrompt:/password[: ]*$/i,
shellPrompt: /Logon successful/i,
username:'VN39LTbXMIWjeRN067ZWFYqGJe8Q78oN',
timeout:10000,
execTimeout:5000,
sendTimeout:5000,
negotiationMandatory:false,
debug:true
};
// Telnet Client
telnet_client.on('ready', function(prompt) {
});
telnet_client.on('timeout', function() {
});
telnet_client.on('close', function() {
console.log('[Telnet Connection Closed]');
process.exit(1);
});
telnet_client.on('data', function(buffer) {
let response = decoder.end(buffer);
if (_.includes(response,'Player connected')) {
Server.parseIncoming(response, 'player_connected');
}
if (_.includes(response,'Player disconnected:')) {
Server.parseIncoming(response, 'player_disconnected');
}
//console.log(response);
});
telnet_client.connect(telnet_params);
// Discord Bot
discord_client.login(discord_token);
discord_client.on('ready', () => {
});
discord_client.on('disconnect', () => {
console.log('[Discord Connection Closed]');
process.exit(1);
});
discord_client.on('error', () => {
console.log('[Discord Error - Connection Closed]');
process.exit(1);
});
discord_client.on('message', message => {
if (message.content.startsWith('!ping')) {
message.reply('pong');
}
if (message.content.startsWith('!passthrough')) {
let passThruCommand = message.content.substr('!passthrough'.length+1);
Server.command(passThruCommand);
}
if (message.content.startsWith('!whoson')) {
Server.refreshPlayers().then((players) => {
if (players.length == 0) {
message.reply(`There are no players currently online.`);
} else {
if (players.length == 1) {
message.reply(`There is 1 player online: ${players[0].name}`);
} else {
let player_string = players.map((el) => {
return el.name;
}).join(', ');
message.reply(`There are ${players.length} players online: ${player_string}`);
}
}
});
}
if (message.channel.name == 'dev') {
// Only dev commands are allowed here
if (message.content.startsWith('!announce')) {
let announcementMessage = message.content.substr('!announce'.length+1);
Server.say(announcementMessage);
message.reply(`that announcement was displayed to the server.`);
}
if (message.content.startsWith('!restartbot')) {
process.exit(1);
}
if (message.content.startsWith('!help')) {
let helpBanner = [];
helpBanner.push(`Available Commands: `);
helpBanner.push(` !whoson`);
helpBanner.push(` Displays who is online on the game server`);
helpBanner.push(` !announce`);
helpBanner.push(` Displays a global message to the server (only available to users in #dev)`);
helpBanner.push(` !restartbot`);
helpBanner.push(` Restarts the Discord bot (does not restart server)`);
message.channel.sendMessage(helpBanner.join("\n"));
}
} else {
if (message.content.startsWith('!help')) {
let helpBanner = [];
helpBanner.push(`Available Commands: `);
helpBanner.push(` !whoson`);
helpBanner.push(` Displays who is online on the game server`);
message.channel.sendMessage(helpBanner.join("\n"));
}
}
});
// Server Tasks
let Server = {
refreshPlayers: () => {
return new Promise((resolve,reject) => {
Players = [];
telnet_client.send('listplayers',telnet_params,(err, response) => {
if (err) {
reject('An error occurred!');
}
if (_.includes(response, 'Total of 0 in the game')) {
resolve(Players);
} else {
let lines = response.split("\r\n");
let parsed_lines = Server.parseResponse(lines, 'listplayers');
for (line of parsed_lines) {
let p = new Player();
let attributes = line.split(", ");
p.name = attributes[1];
Players.push(p);
}
resolve(Players);
}
});
});
},
say: (message) => {
telnet_client.send(`say "${message}"`,telnet_params,(err, response) => {
console.log(`[Announcement] ${message}`);
});
},
command: (cmd) => {
telnet_client.send(`${cmd}`,telnet_params,(err, response) => {
console.log(`[Pass Through] ${cmd}`);
});
},
parseIncoming: (line, trigger) => {
switch (trigger) {
case 'player_connected':
// 2016-12-27T17:41:13 53676.631 INF Player connected, entityid=171, name=Wergo20, steamid=76561198114662210, steamOwner=76561198114662210, ip=::ffff:73.16.29.185
var name_pattern = new RegExp("name=(.*?),");
var name = name_pattern.exec(line)[1];
console.log(`${name} connected to the server.`);
discord_client.channels.find("name",discord_channel).sendMessage(`${name} connected to the server.`);
break;
case 'player_disconnected':
var name_pattern = new RegExp("PlayerName=\\'(\\S+)\\'");
var name = name_pattern.exec(line)[1];
discord_client.channels.find("name",discord_channel).sendMessage(`${name} disconnected from the server.`);
console.log(`${name} disconnected from the server.`);
break;
case 'player_died':
var name_pattern = new RegExp("INF GMSG: (\\S+): /.*");
var name = name_pattern.exec(line)[1];
discord_client.channels.find("name",discord_channel).sendMessage(`${name} died.`);
console.log(`${name} died.`);
break;
}
},
parseResponse: (response_lines, command) => {
switch (command) {
case 'listplayers':
let start = false;
let parsed_lines = [];
for (line of response_lines) {
if (_.includes(line, 'Total of ')) {
start = false;
}
if (start) {
if (_.includes(line, 'health=') && _.includes(line, 'steamid=')) {
parsed_lines.push(line);
}
}
if (_.includes(line, "INF Executing command 'listplayers' by Telnet ")) {
start = true;
}
}
return parsed_lines;
break;
}
}
};