This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
479 lines (396 loc) · 13.3 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/******************************
* REQUIRED DISCORD MODULE *
******************************/
const Discord = require('discord.js');
/******************
* BOT CLIENT *
******************/
const bot = new Discord.Client();
/**************
* MODULES *
**************/
require("./prototype_functions.js");
const fs = require('fs');
const config = require("./config.json");
const emitter = require("./emitter.js");
const rw = require("./reader_writer.js");
const timer = require("./timer.js");
const timeEventsEmitter = require("./time_events_emitter.js");
const gameHub = require("./game_hub.js");
const commands = require("./process_commands.js");
const slaveServersModule = require("./slave_server.js");
const handleGameError = require("./game_runtime_error_handler.js");
var guildModule; //to be initialized after bot is logged in.
var hoster; //to be initialized after loading all data.
/**********************************
* ESTABLISH AN EXPRESS SERVER *
**********************************/
var express = require("express");
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
//Will switch to true when the on.ready event first triggers so that certain bits of code don't get re-executed when the bot reconnects and goes through on.ready again
var wasInitialized = false;
var didNotReconnect = false;
//server swapping occurs when a game's original server is not available, so it
//gets assigned a new server from the other ones. This feature is disabled for now
//as it still must be implemented (TODO)
var serverSwappingEnabled = false;
var masterOwner;
var games = {};
/****************************************
* CALLED WHEN bot.login() RESOLVES *
****************************************/
bot.on("ready", () =>
{
didNotReconnect = false;
guildModule = require("./guild_data").init(bot);
reviveGames();
rw.log("general", "Bot logged in.");
});
/******************
* BOT EVENTS *
******************/
//Triggers when the bot is added to a guild or when a guild becomes available (i.e. after a Discord outage)
bot.on("guildCreate", guild =>
{
//guild already existed in the bot, so it means that it's available again after a Discord outage
if (guildModule.botHasGuild(guild.id) === true)
{
rw.log("general", `The guild ${guild.name} is available again. Launching its games.`);
guild.owner.send(`The guild ${guild.name} is available again. Launching its games.`).catch(console.error);
for (var name in games)
{
rw.log("general", `name ${name}`);
if (games[name].guild.id !== guild.id)
{
continue;
}
games[name].host(null, function(err)
{
if (err)
{
rw.log("error", {Game: name, error: err});
return;
}
if (games[name].organizer != null)
{
games[name].organizer.send(`${name} was launched, as the guild ${guild.name} became available again.`);
}
});
}
}
else guild.owner.send(`Thank you for using this bot. To get started, you must give me permissions to manage channels and roles, and then use the command \`${config.prefix}deploy\` in any of the guild's channels which the bot can see. It will create a few necessary roles and categories.`);
});
bot.on("guildDelete", guild =>
{
})
bot.on("guildUnavailable", (guild) =>
{
didNotReconnect = true;
rw.log("general", `The guild ${guild.name} is unavailable, probably due to a Discord outage. Shutting down its games.`);
guild.owner.send(`The guild ${guild.name} is unavailable, probably due to a Discord outage. Shutting down its games.`).catch(console.error);
for (var name in games)
{
if (games[name].guild.id !== guild.id)
{
continue;
}
games[name].kill(function(err)
{
if (err)
{
rw.log("error", true, {Game: name}, err);
return;
}
if (games[name].organizer != null)
{
games[name].organizer.send(`${name} was shut down due to the guild ${guild.name} becoming unavailable. This is probably caused by a Discord outage.`);
}
});
}
});
bot.on("channelDelete", channel =>
{
hoster.updateAndDeletePendingGameChannel(channel.id);
});
bot.on('message', message =>
{
var isDM = (message.channel.type === "dm");
var args = message.content.split(/ +/);
var command = args.shift().toLowerCase();
if (message.author.bot === true)
{
return;
}
if (isDM === true)
{
emitter.emit("DM", message);
commands.tryCommand(message, command, args, games, null, isDM);
return;
}
message.guild.fetchMember(message.author).then(function(member)
{
emitter.emit("guildMessage", {guild: message.guild, member: member, message: message});
commands.tryCommand(message, command, args, games, member, isDM);
//processChannelMessage(message, member);
});
});
//only triggers on messages sent after the bot was started
bot.on("messageDelete", message =>
{
if (message.channel.type === "text")
{
message.guild.fetchMember(message.author).then(function(member)
{
//handle deleted message
});
}
});
bot.on("disconnect", () =>
{
didNotReconnect = true;
rw.log("general", "I have been disconnected!");
setTimeout (reconnect.bind(null, config.token), reconnectInterval);
if (masterOwner)
{
masterOwner.send("I have been disconnected!").catch((err) => {rw.log("error", err);});
}
});
bot.on("reconnecting", () =>
{
rw.log("general", "Trying to reconnect...");
if (masterOwner)
{
//masterOwner.send("Trying to reconnect...").catch((err) => {rw.log("error", err);});
}
});
bot.on("debug", info =>
{
//rw.log("general", "DEBUG: " + info);
});
bot.on("warn", warning =>
{
rw.log("error", `Warning:`, warning);
});
bot.on("error", (err) =>
{
if (err.error.code === "ECONNRESET")
{
rw.log("general", `ECONNRESET Error:`, err.error);
return;
}
rw.log("error", `Bot Error:`, err.message);
if (masterOwner)
{
masterOwner.send(`Bot Error: \n\n${rw.JSONStringify(err.message)}`).catch((error) => {rw.log("error", true, `Could not send message:`, error);});
}
});
function initializeServer()
{
server.listen(3000);
app.get("/", function (req, res)
{
});
//app.use("/slaveServer", express.static(__dirname + "/slaveServer"));
}
function listenToSlaves()
{
io.on("connection", function(socket)
{
rw.log("general", `Socket connected with id: ${socket.id}.`);
socket.emit("init", null, function(data)
{
try
{
slaveServersModule.verifySlave(socket, data);
}
catch(err)
{
rw.log("error", err);
return;
}
let slave = slaveServersModule.instanceSlave(socket, data, games);
slave.hostGames();
rw.log("general", `Server ${data.name} authenticated. Current capacity is ${slave.hostedGameNames.length}/${slave.capacity}.`);
});
// Disconnect listener
socket.on("disconnect", function()
{
var server = slaveServersModule.getSlave(socket.id);
if (server != null)
{
rw.log("general", `Server ${server.name} (socket id ${socket.id}) disconnected.`);
hoster.notifyAssistedHostingUsersOfDisconnection(server);
server.disconnect();
}
else rw.log("general", `Unidentified server (socket id ${socket.id}) disconnected.`);
});
//emitted by a slave server when one of the hosted games' stdio is closed
socket.on("stdioClosed", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} reported to have closed its stdio with code ${data.code} and signal ${data.signal}, but it cannot be found in the list of active games.`);
}
else rw.log("general", `The game ${data.name} reported to have closed its stdio with code ${data.code} and signal ${data.signal}.`);
});
//emitted by a slave server when one of the hosted games exits itself
//These do not receive a signal, only a code
socket.on("gameExited", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} reported to have exited with code ${data.code}, but it cannot be found in the list of active games.`);
return;
}
rw.log("general", `The game ${data.name} reported to have exited with code ${data.code}.`);
games[data.name.toLowerCase()].isOnline = false;
games[data.name.toLowerCase()].organizer.send(`Your game ${data.name} has shut down by itself (perhaps a game-related crash occurred). You can try launching it again (see \`${config.prefix}help\` for the command).`);
});
//emitted by a slave server when one of the hosted games gets terminated unexpectedly (i.e. not by a kill command)
socket.on("gameTerminated", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} reported to have been abnormally terminated with signal ${data.signal}, but it cannot be found in the list of active games.`);
return;
}
rw.log("general", `The game ${data.name} reported to have been abnormally terminated with signal ${data.signal}.`);
games[data.name.toLowerCase()].isOnline = false;
games[data.name.toLowerCase()].organizer.send(`Your game ${data.name}'s process has been abnormally terminated. You can try launching it again (see \`${config.prefix}help\` for the command).`);
});
socket.on("stderrData", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} emitted stderr data, but it cannot be found in the list of active games:\n\n`, data.data);
}
else
{
rw.log("general", `The game ${data.name} emitted stderr data:\n\n`, data.data);
handleGameError(games[data.name.toLowerCase()], data.data);
}
});
socket.on("stderrError", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} emitted an stderr error, but it cannot be found in the list of active games:\n\n`, data.error);
}
else
{
rw.log("general", `The game ${data.name} emitted an stderr error:\n\n`, data.error);
}
});
socket.on("stdoutData", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} emitted stdout data, but it cannot be found in the list of active games:\n\n`, data.data);
}
else rw.log("general", `The game ${data.name} emitted stdout data:\n\n`, data.data);
});
socket.on("stdoutError", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} emitted an stdout error, but it cannot be found in the list of active games:\n\n`, data.error);
}
else rw.log("general", `The game ${data.name} emitted an stdout error:\n\n`, data.error);
});
socket.on("stdinError", function(data)
{
if (games[data.name.toLowerCase()] == null)
{
rw.log("general", `The game ${data.name} emitted an stdin error, but it cannot be found in the list of active games:\n\n`, data.error);
}
else rw.log("general", `The game ${data.name} emitted an stdin error:\n\n`, data.error);
});
});
}
function reviveGames()
{
var directories;
fs.readdir(config.pathToGameData, function(err, filenames)
{
if (err)
{
rw.log("error", true, {path: config.pathToGameData}, err);
throw `A file system related error occurred while reading the games directory in the reviveGames() function:\n\n${err}`;
}
//only grab the filenames that are directories
directories = filenames.filter(function(name)
{
return fs.lstatSync(config.pathToGameData + "/" + name).isDirectory() === true;
});
directories.forEachAsync(function(dir, index, next)
{
rw.readJSON(`${config.pathToGameData}/${dir}/data.json`, null, function callback(err, gameData)
{
var revivedGame;
var hostServer;
if (err)
{
rw.log("error", true, {Path: `${config.pathToGameData}/${dir}/data.json`}, err);
next();
return;
}
revivedGame = gameHub.fromJSON(gameData, guildModule.getGuildObject(gameData.guild), (err, revivedGame) =>
{
if (err)
{
rw.log("error", `ERROR: ${gameData.name} could not be revived:`, err);
next();
return;
}
games[revivedGame.name.toLowerCase()] = revivedGame;
rw.log("general", `Revived the game ${revivedGame.name}.`);
});
//continue loop
next();
});
}, function callback()
{
//reviving games finished
finalizeInitialization();
});
});
}
function finalizeInitialization()
{
bot.fetchUser(config.masterOwner).then(function(master)
{
masterOwner = master;
hoster = require("./hoster.js").init(games, slaveServersModule, gameHub);
//start tracking time
timeEventsEmitter.start();
/*******************************************************************
* START UP THE LISTENING SERVER AND LISTEN TO SLAVE CONNECTIONS *
*******************************************************************/
initializeServer();
listenToSlaves();
wasInitialized = true;
masterOwner.send("Initialization complete.").catch((err) => {rw.log("error", true, `Error when sending message:`, err);});
rw.log("general", "Initialization complete.");
});
}
function reconnect (token)
{
if (didNotReconnect == true)
{
bot.login(token);
rw.log("general", "Manual attempt to reconnect...");
}
}
//Login to the server, always goes at the end of the document, don't ask me why
bot.login(config.token);
//This simple piece of code catches those pesky unhelpful errors and gives you the line number that caused it!
process.on("unhandledRejection", err =>
{
rw.log("general", "Uncaught Promise Error: \n" + err.stack);
if (masterOwner)
{
masterOwner.send("Uncaught Promise Error: \n" + err.stack);
}
});