-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
420 lines (335 loc) · 14.3 KB
/
index.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
const Discord = require('discord.js');
const client = new Discord.Client();
const disbut = require('discord-buttons')(client);
const config = require('./config.json');
let channelInfo;
let roleInfo;
let serverInfo;
let messageEmbed;
let i;
let j;
let contestants = {
"user": "",
"answer": ""
}
let contestantsArray = new Array ();
let questions = {
"questions": ["In which continent is Mongolia located?", "What type of meat is on a traditional Reuben sandwich?", "What part of the world was once known as Cathay?", "What kind of animal is a peregrine?", "What is your astrological sign if you were born on Halloween?"],
"answer1": ["Asia", "Turkey", "Japan", "Bird", "Capricorn"],
"answer2": ["Europe", "Bologna", "China", "Fish", "Scorpio"],
"answer3": ["Oceania", "Corned Beef", "India", "Cat", "Libra"],
"answer": ["Asia", "Corned Beef", "China", "Bird", "Scorpio"],
"category": ["Geography", "Food", "History", "Animals", "Astrology"]
}
let numContestants = 0;
let numAnswers = 0;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === '!start') {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setDescription('Trivia starts in 1 hour! Sign up using the button below!')
.setTimestamp()
.setFooter(`${numContestants} registered`, 'https://i.imgur.com/dhA5PXS.png');
const button = new disbut.MessageButton()
.setStyle('green')
.setLabel('Sign up')
.setID('signup')
msg.channel.send({ button: button, embed: exampleEmbed }).then (msg2 => {
messageEmbed = msg2;
});
} else if (msg.content === '!start2') {
game (msg);
}
});
client.on('clickButton', async (button) => {
button.defer ()
// Remember to put this listener in my signup function when I make it because it won't work otherwise
if (button.id === 'signup') {
let roleExist = false;
let i;
for (i = 0; i < numContestants; i ++) {
if (button.clicker.user.id == contestantsArray [i].user.user.id) {
roleExist = true;
}
}
if (roleExist == false) {
// First give the member the contestant role
let role = await button.clicker.member.guild.roles.cache.find(r => r.name === "Trivia Contestant");
await button.clicker.member.roles.add (role).catch(console.error);
numContestants ++;
// Then update the embed with the number of registrants
const receivedEmbed = messageEmbed [0];
const newEmbed = new Discord.MessageEmbed (receivedEmbed)
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setDescription('Trivia starts in 1 hour! Sign up using the button below!')
.setTimestamp()
.setFooter (`${numContestants} registered`, 'https://i.imgur.com/dhA5PXS.png');
messageEmbed.edit (newEmbed);
// Add user to the array of contestants
let temp = {
"user": button.clicker,
"answer": "blank"
}
contestantsArray.push (temp);
}
}
});
client.on("guildCreate", guild => {
serverInfo = guild;
const channel = guild.channels.create ('trivia', { reason: 'Need a dedicated trivia channel' })
.then(result => {
channelInfo = result;
})
.catch(console.error);
const role = guild.roles.create({
data: {
name: 'Trivia Contestant',
color: 'BLUE',
},
reason: 'Need a role for the contestants to keep track of things',
})
.then(result => {
roleInfo = result;
})
.catch(console.error);
});
client.on('error', (err) => {
let date = new Date().toLocaleString();
console.log (`Internet outage at: ${date}`);
});
async function game (msg) {
// Disable the signup button and edit the message
const button = new disbut.MessageButton()
.setStyle('green')
.setLabel('Sign up')
.setID('signup')
.setDisabled ();
messageEmbed.edit ({ button: button, embed: messageEmbed [0] });
numAnswers = 0;
client.on('clickButton', async (button) => {
if (button.id === 'answer1') {
for (j = 0; j < numContestants; j ++) {
if (button.clicker.user.id == contestantsArray [j].user.user.id) {
if (contestantsArray [j].answer.localeCompare ("blank") == 0) {
numAnswers ++;
}
contestantsArray [j].answer = questions.answer1 [i];
}
}
} else if (button.id === 'answer2') {
for (j = 0; j < numContestants; j ++) {
if (button.clicker.user.id == contestantsArray [j].user.user.id) {
if (contestantsArray [j].answer.localeCompare ("blank") == 0) {
numAnswers ++;
}
contestantsArray [j].answer = questions.answer2 [i];
}
}
} else if (button.id === 'answer3') {
for (j = 0; j < numContestants; j ++) {
if (button.clicker.user.id == contestantsArray [j].user.user.id) {
if (contestantsArray [j].answer.localeCompare ("blank") == 0) {
numAnswers ++;
}
contestantsArray [j].answer = questions.answer3 [i];
}
}
}
});
for (i = 0; i < 5; i ++) {
// Set all player answers to blank again and set numAnswers to 0
numAnswers = 0;
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle (`Round ${(i + 1)} of 5`) // Change the 5 depending on the number of questions
.setDescription(`The category is: ${questions.category [i]}!`)
.setTimestamp()
msg.channel.send (exampleEmbed);
await sleep (5000);
const exampleEmbed2 = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle (`${questions.questions [i]}`)
.setDescription ('You have 15 seconds to answer')
.setTimestamp()
.setFooter(`${numAnswers} answered`, 'https://i.imgur.com/dhA5PXS.png');
let answer1 = new disbut.MessageButton()
.setStyle('green')
.setLabel(`${questions.answer1 [i]}`)
.setID('answer1')
let answer2 = new disbut.MessageButton()
.setStyle('blurple')
.setLabel(`${questions.answer2 [i]}`)
.setID('answer2')
let answer3 = new disbut.MessageButton()
.setStyle('red')
.setLabel(`${questions.answer3 [i]}`)
.setID('answer3')
msg.channel.send({
buttons: [
answer1, answer2, answer3
],
embed: exampleEmbed2 }).then (msg2 => {
messageEmbed = msg2;
});
let timer;
let newEmbed;
for (timer = 12; timer > -1; timer -= 3) {
await sleep (3000);
newEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle (`${questions.questions [i]}`)
.setDescription (`You have ${timer} seconds to answer`)
.setTimestamp()
.setFooter(`${numAnswers} answered`, 'https://i.imgur.com/dhA5PXS.png');
messageEmbed.edit ({
buttons: [
answer1, answer2, answer3
],
embed: newEmbed }).then (msg2 => {
messageEmbed = msg2;
});
}
answer1 = new disbut.MessageButton()
.setStyle('green')
.setLabel(`${questions.answer1 [i]}`)
.setID('answer1')
.setDisabled ()
answer2 = new disbut.MessageButton()
.setStyle('blurple')
.setLabel(`${questions.answer2 [i]}`)
.setID('answer2')
.setDisabled ()
answer3 = new disbut.MessageButton()
.setStyle('red')
.setLabel(`${questions.answer3 [i]}`)
.setID('answer3')
.setDisabled ()
newEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle (`${questions.questions [i]}`)
.setDescription (`You have 0 seconds to answer`)
.setTimestamp()
.setFooter(`${numAnswers} answered`, 'https://i.imgur.com/dhA5PXS.png');
messageEmbed.edit ({
buttons: [
answer1, answer2, answer3
],
embed: newEmbed }).then (msg2 => {
messageEmbed = msg2;
});
const exampleEmbed3 = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle ('Time\'s up!')
.setDescription (`The answer was: ${questions.answer [i]}`)
msg.channel.send (exampleEmbed3);
await sleep (7000);
// Figure out who got it right, kick out the ones that didn't, then ping the ones who did and say how many got it right
let anyCorrect = false;
for (j = 0; j < numContestants; j ++) {
if (contestantsArray [j].answer.localeCompare (questions.answer [i]) == 0) {
anyCorrect = true;
break;
}
}
if (anyCorrect == false) {
// If nobody got it correct, the winners are everyone from who made it to this round
let mention = (`<@${contestantsArray [0].user.user.id}>`);
for (j = 1; j < numContestants; j ++) {
let concatString = (`\n<@${contestantsArray [j].user.user.id}>`);
mention = mention.concat (concatString);
}
const winnerEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle ('The game is over!')
.addField ('The winners are:', mention)
.setTimestamp()
msg.channel.send (winnerEmbed);
for (j = numContestants - 1; j > -1; j --) {
let role = contestantsArray [j].user.member.guild.roles.cache.find(r => r.name === "Trivia Contestant");
contestantsArray [j].user.member.roles.remove (role).catch(console.error);
contestantsArray.splice (j, 1);
numContestants --;
}
break;
} else {
// Otherwise find the people who got it wrong and kick them out
for (j = numContestants - 1; j > -1; j --) {
if (contestantsArray [j].answer.localeCompare (questions.answer [i]) != 0) {
let role = contestantsArray [j].user.member.guild.roles.cache.find(r => r.name === "Trivia Contestant");
contestantsArray [j].user.member.roles.remove (role).catch(console.error);
// Figure out how to remove element from an array
contestantsArray.splice (j, 1);
numContestants --;
}
}
if (numContestants == 1) {
// If there's one person left then the game is over with 1 winner
let mention = (`<@${contestantsArray [0].user.user.id}>`);
const winnerEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle ('The game is over!')
.addField ('The winner is:', mention)
.setTimestamp()
msg.channel.send (winnerEmbed);
let role = contestantsArray [0].user.member.guild.roles.cache.find(r => r.name === "Trivia Contestant");
contestantsArray [0].user.member.roles.remove (role).catch(console.error);
break;
}
}
// Print the role and announce how many people got the right answer here
if ((i + 1) != 5) {
const nextRoundEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle (`${numContestants} contestants got that question right!`)
.setDescription ('1 minute until the next round!\nIf you receive a ping then you are eligible for the next round!')
.setTimestamp()
msg.channel.send (nextRoundEmbed);
let role = contestantsArray [0].user.member.guild.roles.cache.find(r => r.name === "Trivia Contestant");
msg.channel.send (`<@&${role.id}>`);
for (j = 0; j < numContestants; j ++) {
contestantsArray [j].answer = "blank";
}
await sleep (5000);
}
}
// If there are still players left after all questions have been exhausted then the game is over
// Write all winners here
if (numContestants > 1) {
let mention = (`<@${contestantsArray [0].user.user.id}>`);
for (j = 1; j < numContestants; j ++) {
let concatString = (`\n<@${contestantsArray [j].user.user.id}>`);
mention = mention.concat (concatString);
}
const winnerEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setAuthor('Trivia Contest', 'https://i.imgur.com/dhA5PXS.png')
.setTitle ('The game is over!')
.addField ('The winners are:', mention)
.setTimestamp()
msg.channel.send (winnerEmbed);
// Then remove the contestant role from everyone
for (j = numContestants - 1; j > -1; j --) {
let role = contestantsArray [j].user.member.guild.roles.cache.find(r => r.name === "Trivia Contestant");
contestantsArray [j].user.member.roles.remove (role).catch(console.error);
contestantsArray.splice (j, 1);
numContestants --;
}
}
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
client.login(config.discord.token);