forked from WerewolvesRevamped/Werewolves-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
442 lines (428 loc) · 16.3 KB
/
stats.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
/*
Module for handelling things regarding stats:
- Modifying options
- Gamephase
- Cacheing stats
- Convert gamephase id to name
*/
module.exports = function() {
/* Variables */
this.stats = {};
/* Caches stats everytime they are changed or the bot is (re)started */
this.getStats = function() {
var doLog = false;
// Get Log Channel & Guild
sqlGetStat(11, result => {
stats.log_guild = result;
if(doLog) log("Stats > Cached log guild id as `" + result + "`!")
}, () => {
stats.log_guild = false;
log("Stats > ❗❗❗ Unable to cache log guild id!")
});
sqlGetStat(12, result => {
stats.log_channel = result;
if(doLog) log("Stats > Cached log channel id as `" + result + "`!")
}, () => {
stats.log_channel = false;
log("Stats > ❗❗❗ Unable to cache log channel id!")
});
// Get Gamephase
sqlGetStat(1, result => {
stats.gamephase = result;
getEmojis();
if(doLog) log("Stats > Cached gamephase as `" + result + "`!")
}, () => {
stats.gamephase = gp.NONE;
log("Stats > ❗❗❗ Unable to cache gamephase!")
});
// Get Prefix
sqlGetStat(2, result => {
stats.prefix = result;
if(doLog) log("Stats > Cached prefix as `" + result + "`!")
}, () => {
stats.prefix = "$";
log("Stats > ❗❗❗ Unable to cache prefix!")
});
// Get Role Ids
sqlGetStat(3, result => {
stats.participant = result;
if(doLog) log("Stats > Cached participant role id as `" + result + "`!")
}, () => {
stats.participant = false;
log("Stats > ❗❗❗ Unable to cache participant role id!")
});
sqlGetStat(4, result => {
stats.gamemaster = result;
if(doLog) log("Stats > Cached gamemaster role id as `" + result + "`!")
}, () => {
stats.gamemaster = false;
log("Stats > ❗❗❗ Unable to cache gamemaster role id!")
});
sqlGetStat(5, result => {
stats.spectator = result;
if(doLog) log("Stats > Cached spectator role id as `" + result + "`!")
}, () => {
stats.spectator = false;
log("Stats > ❗❗❗ Unable to cache spectator role id!")
});
sqlGetStat(6, result => {
stats.signed_up = result;
if(doLog) log("Stats > Cached signed up role id as `" + result + "`!")
}, () => {
stats.signed_up = false;
log("Stats > ❗❗❗ Unable to cache signed up role id!")
});
sqlGetStat(7, result => {
stats.dead_participant = result;
if(doLog) log("Stats > Cached dead participant role id as `" + result + "`!")
}, () => {
stats.dead_participant = false;
log("Stats > ❗❗❗ Unable to cache dead participant role id!")
});
sqlGetStat(8, result => {
stats.bot = result;
if(doLog) log("Stats > Cached bot role id as `" + result + "`!")
}, () => {
stats.bot = false;
log("Stats > ❗❗❗ Unable to cache bot role id!")
});
// Cache Elected roles
sqlGetStat(16, result => {
stats.mayor = result;
if(doLog) log("Stats > Cached mayor role id as `" + result + "`!")
}, () => {
stats.mayor = false;
log("Stats > ❗❗❗ Unable to cache mayor role id!")
});
sqlGetStat(17, result => {
stats.reporter = result;
if(doLog) log("Stats > Cached reporter role id as `" + result + "`!")
}, () => {
stats.reporter = false;
log("Stats > ❗❗❗ Unable to cache reporter role id!")
});
sqlGetStat(18, result => {
stats.guardian = result;
if(doLog) log("Stats > Cached guardian role id as `" + result + "`!")
}, () => {
stats.guardian = false;
log("Stats > ❗❗❗ Unable to cache guardian role id!")
});
sqlGetStat(19, result => {
stats.game = result;
if(doLog) log("Stats > Cached game name as `" + result + "`!")
}, () => {
stats.game = "WWR";
log("Stats > ❗❗❗ Unable to cache game name!")
});
sqlGetStat(21, result => {
stats.gamemaster_ingame = result;
if(doLog) log("Stats > Cached game master ingame role id as `" + result + "`!")
}, () => {
stats.gamemaster_ingame = false;
log("Stats > ❗❗❗ Unable to cache game master ingame role id!")
});
sqlGetStat(22, result => {
stats.admin = result;
if(doLog) log("Stats > Cached admin role id as `" + result + "`!")
}, () => {
stats.admin = false;
log("Stats > ❗❗❗ Unable to cache admin role id!")
});
sqlGetStat(23, result => {
stats.admin_ingame = result;
if(doLog) log("Stats > Cached admin ingame role id as `" + result + "`!")
}, () => {
stats.admin_ingame = false;
log("Stats > ❗❗❗ Unable to cache admin ingame role id!")
});
sqlGetStat(24, result => {
stats.yes_emoji = result;
if(doLog) log("Stats > Cached yes emoji as `" + result + "`!")
}, () => {
stats.yes_emoji = "false";
log("Stats > ❗❗❗ Unable to cache yes emoji!")
});
sqlGetStat(25, result => {
stats.no_emoji = result;
if(doLog) log("Stats > Cached no emoji as `" + result + "`!")
}, () => {
stats.no_emoji = false;
log("Stats > ❗❗❗ Unable to cache no emoji!")
});
sqlGetStat(26, result => {
stats.new_game_ping = result;
if(doLog) log("Stats > Cached new game ping as `" + result + "`!")
}, () => {
stats.new_game_ping = false;
log("Stats > ❗❗❗ Unable to cache new game ping!")
});
sqlGetStat(27, result => {
stats.game_status = result;
if(doLog) log("Stats > Cached game status vc as `" + result + "`!")
}, () => {
stats.game_status = false;
log("Stats > ❗❗❗ Unable to cache game status vc!")
});
sqlGetStat(28, result => {
stats.cc_limit = result;
if(doLog) log("Stats > Cached cc limit as `" + result + "`!")
}, () => {
stats.cc_limit = 0;
log("Stats > ❗❗❗ Unable to cache game status vc!")
});
sqlGetStat(29, result => {
stats.theme = result;
if(doLog) log("Stats > Cached theme as `" + result + "`!");
cacheTheme();
}, () => {
stats.theme = "default";
log("Stats > ❗❗❗ Unable to cache theme!")
});
// Cache Elected roles
sqlGetStat(30, result => {
stats.mayor2 = result;
if(doLog) log("Stats > Cached mayor2 role id as `" + result + "`!")
}, () => {
stats.mayor2 = false;
log("Stats > ❗❗❗ Unable to cache mayor2 role id!")
});
// Cache Elected roles
sqlGetStat(31, result => {
stats.poll = result;
if(doLog) log("Stats > Cached poll mode as `" + result + "`!")
}, () => {
stats.poll = 0;
log("Stats > ❗❗❗ Unable to cache poll mode!")
});
// Sub role
sqlGetStat(32, result => {
stats.sub = result;
if(doLog) log("Stats > Cached substitute player role as `" + result + "`!")
}, () => {
stats.sub = false;
log("Stats > ❗❗❗ Unable to cache substitute player role mode!")
});
// gif ping
sqlGetStat(33, result => {
stats.ping = result;
if(doLog) log("Stats > Cached gif ping as `" + result + "`!")
}, () => {
stats.ping = stats.gamemaster ? stats.gamemaster : false;
log("Stats > ❗❗❗ Unable to cache gif ping!")
});
// secret mode
sqlGetStat(34, result => {
stats.secret_mode = result === 'true';
if(doLog) log("Stats > Cached secret mode as `" + stats.secret_mode + "`!")
}, () => {
stats.secret_mode = false;
log("Stats > ❗❗❗ Unable to cache secret mode!")
});
// fancy mode
sqlGetStat(35, result => {
stats.fancy_mode = result === 'true';
if(doLog) log("Stats > Cached fancy mode as `" + stats.fancy_mode + "`!")
}, () => {
stats.fancy_mode = false;
log("Stats > ❗❗❗ Unable to cache fancy mode!")
});
// icon version
sqlGetStat(36, result => {
stats.icon_version = result;
if(doLog) log("Stats > Cached icon version as `" + result + "`!")
}, () => {
stats.icon_version = 0;
log("Stats > ❗❗❗ Unable to cache icon version!")
});
}
/* Gets the name of a gamephase by id */
this.getPhaseName = function(id) {
switch(+id) {
case gp.NONE: return "NOTHING";
case gp.SIGNUP: return "SIGNUP";
case gp.SETUP: return "SETUP";
case gp.INGAME: return "INGAME";
case gp.POSTGAME: return "POST GAME";
default: return "INVALID";
}
}
/* Handles option command */
this.cmdOptions = function(message, args) {
// Check subcommand
if(!args[0]) {
message.channel.send("⛔ Syntax error. Not enough parameters!");
return;
}
let stat;
if(isNaN(args[0])) {
// Convert stat
stat = 0;
switch(args[0]) {
case "prefix": stat = 2; break;
case "participant": stat = 3; break;
case "gamemaster": stat = 4; break;
case "spectator": stat = 5; break;
case "signed_up": stat = 6; break;
case "dead_participant": stat = 7; break;
case "bot": stat = 8; break;
case "log_guild": stat = 11; break;
case "log_channel": stat = 12; break;
case "mayor": stat = 16; break;
case "reporter": stat = 17; break;
case "guardian": stat = 18; break;
case "game": stat = 19; break;
case "gamemaster_ingame": stat = 21; break;
case "admin": stat = 22; break;
case "admin_ingame": stat = 23; break;
case "yes_emoji": stat = 24; break;
case "no_emoji": stat = 25; break;
case "new_game_ping": stat = 26; break;
case "game_status": stat = 27; break;
case "cc_limit": stat = 28; break;
case "theme": stat = 29; break;
case "mayor2": stat = 30; break;
case "poll": stat = 31; break;
case "sub": stat = 32; break;
case "ping": stat = 33; break;
case "secret_mode": stat = 34; break;
case "fancy_mode": stat = 35; break;
case "icon": stat = 36; break;
default: message.channel.send("⛔ Syntax error. Invalid parameter!"); return;
}
} else {
stat = args[0];
}
// Find subcommand
if(args[1]) cmdOptionsSet(message.channel, args, stat);
else cmdOptionsGet(message.channel, args, stat);
}
/* Sets a stat in the database */
this.cmdOptionsSet = function(channel, args, stat) {
// Set value
sqlSetStat(stat, args[1], result => {
channel.send("✅ Successfully updated `" + args[0] + "` to `" + args[1] + "`!");
getStats();
}, () => {
// Db error
channel.send("⛔ Database error. Could not update `" + args[0] + "`!");
});
}
this.helpStats = function(member, args) {
let help = "";
switch(args[0]) {
case "":
if(isGameMaster(member)) help += stats.prefix + "options <Option Name> - Manages options\n";
if(isGameMaster(member)) help += stats.prefix + "gamephase [get|set] - Manages gamephase\n";
break;
case "options":
if(!isGameMaster(member)) break;
help += "```yaml\nSyntax\n\n" + stats.prefix + "options <Option Name> <New Value>\n```";
help += "```\nFunctionality\n\nReturns or sets (if <New Value> is set) the value of a bot option <Option Name>. A bot option can be a numeric id, or an option name.\n\nList of Option Names:\nprefix: The prefix the bot uses for commands\nparticipant: The id of the participant role\ngamemaster: The id of the gamemaster role\nspectator: The id of the spectator role\nsigned_up: The id of the signed up role\ndead_participant: The id of the dead participant role\nbot: The id of the bot role\nlog_guild: The id of the guild to use for logs\nlog_channel: The id of the channel to use for logs\nmayor: The id of the mayor role\nreporter: The id of the reporter role\nguardian: The id of the guardian role\ngame: The name of the game\ngamemaster_ingame: The id of the gamemaster ingame role\nadmin: The id of the admin role\nadmin_ingame: The id of the admin ingame role\nyes_emoji: The id of the yes emoji\nno_emoji: The id of the no emoji\nnew_game_ping: Role that gets pinged with certain commands\ngame_status: A VC that shows the status of the game\ncc_limit: Maximum amount of ccs one person can create (-1 for none)\nmayor2: The id of the second mayor role (which doesn't give extra votes)\npoll: The poll mode (0 -> default, 1 -> cancel, 2 -> private random)\nsub: role for substitute players\nping: ping for gifs and deleted messages\nsecret_mode: Unfinished mode that keeps players true name secret.\nfancy_mode: Changes info messages to fancy versions if set to true.\nicon: the version to use for icon images.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "options mayor\n< ✅ mayor currently is set to 588125889611431946!\n\n> " + stats.prefix + "options mayor 588125889611431946\n< ✅ Successfully updated mayor to 588125889611431946!```";
help += "```diff\nAliases\n\n- stat\n- stats\n- option\n```";
break;
case "gamephase":
if(!isGameMaster(member)) break;
switch(args[1]) {
default:
help += "```yaml\nSyntax\n\n" + stats.prefix + "gamephase [get|set]\n```";
help += "```\nFunctionality\n\nGroup of commands to handle the gamephase. " + stats.prefix + "help gamephase <sub-command> for detailed help. Also serves as an alias for " + stats.prefix + "gamephase get\n\nList of Gamephases:\nNothing, Signups, Ingame, Postgame```";
help += "```diff\nAliases\n\n- gp\n- game-phase\n- game_phase\n```";
break;
case "get":
help += "```yaml\nSyntax\n\n" + stats.prefix + "gamephase get\n```";
help += "```\nFunctionality\n\nReturns the current gamephase\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "gamephase get\n< ✅ Game Phase is INGAME (2)\n```";
break;
case "set":
help += "```yaml\nSyntax\n\n" + stats.prefix + "gamephase set <Value>\n```";
help += "```\nFunctionality\n\nSets the gamephase to <Value>, which has to be an integer between 0 and 3\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "gamephase set 2\n< ✅ Game Phase is now INGAME (2)!\n```";
break;
}
break;
}
return help;
}
/* Gets a stat from the database */
this.cmdOptionsGet = function(channel, args, stat) {
// Get value
sqlGetStat(stat, result => {
if(result.length > 0) {
// Print value
channel.send("✅ `" + args[0] + "` currently is set to `" + result + "`!");
getStats();
} else {
// Value unset
channel.send("⛔ Database error. Could not get `" + stat + "`!");
}
}, () => {
// Db error
channel.send("⛔ Database error. Could not access stats!");
});
}
/* Handle Gamephase Command */
this.cmdGamephase = function(message, args) {
// Find subcommand
switch(args[0]) {
default:
case "get": cmdGamephaseGet(message.channel); break;
case "set": cmdGamephaseSet(message.channel, args); break;
}
}
this.gp = {
NONE: 0,
SIGNUP: 1,
SETUP: 2,
INGAME: 3,
POSTGAME: 4,
MIN: 0,
MAX: 4
};
/* Set gamephase */
this.cmdGamephaseSet = function(channel, args) {
// Check arguments
if(!args[1] && args[1] !== 0) {
channel.send("⛔ Syntax error. Not enough parameters! Correct usage: `gamephase set <phase>`");
return;
} else if(args[1] >= gp.MIN && args[1] <= gp.MAX) {
// Saved verified gamephase
sqlSetStat(1, args[1], result => {
let phase = getPhaseName(args[1]);
channel.send("✅ Game Phase is now `" + phase + "` (" + args[1] + ")!");
getStats();
updateGameStatus(channel.guild);
}, () => {
// Database didn't update gamephase
channel.send("⛔ Database error. Game Phase could not be set to `" + args[1] + "`!");
});
} else {
// Invalid gamephase value
channel.send("⛔ Syntax error. Game Phase could not be set to `" + args[1] + "`!");
}
}
/* Get gamephase */
this.cmdGamephaseGet = function(channel) {
// Get gamephase from db
sqlGetStat(1, result => {
let phase = getPhaseName(result);
channel.send("✅ Game Phase is `" + phase + "` (" + result + ")");
}, () => {
// Couldn't get gamephase value
channel.send("⛔ Database error. Could not find gamephase.");
});
}
this.updateGameStatus = function(guild) {
sql("SELECT alive FROM players", result => {
let gameStatus = guild.channels.cache.get(stats.game_status);
switch(+stats.gamephase) {
case gp.NONE: gameStatus.setName("⛔ No Game"); break;
case gp.SIGNUP: gameStatus.setName("📰 Signups Open (" + result.length + ")"); break;
case gp.SETUP: gameStatus.setName("📝 Game Setup (" + result.length + ")"); break;
case gp.INGAME: gameStatus.setName("🔁 Game Running (" + result.filter(el => el.alive).length + "/" + result.length + ")"); break;
case gp.POSTGAME: gameStatus.setName("✅ Game Concluded"); break;
}
});
}
}