forked from LakeYS/Discord-Trivia-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord-trivia-func.js
151 lines (121 loc) · 4.67 KB
/
discord-trivia-func.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
/*jshint esversion: 6 */
const https = require("https");
const entities = require("html-entities").AllHtmlEntities;
const letters = ["A", "B", "C", "D"];
game = {};
exports.parse = function(str, msg) {
var id = msg.channel.id;
if(str == "TRIVIA HELP") {
https.get("https://opentdb.com/api_count_global.php", (res) => {
res.on('data', function(data) {
var json = JSON.parse(data.toString());
msg.channel.send("Let's play trivia! Type 'trivia play' to start a game.\nThere are " + json.overall.total_num_of_verified_questions + " verified questions.\nBot by Lake Y (http://LakeYS.net). Powered by OpenTDB (https://opentdb.com/).");
});
});
}
if(str == "TRIVIA START")
doTriviaQuestion(msg);
if(str == "TRIVIA PLAY")
doTriviaQuestion(msg);
if(str == "TRIVIA QUESTION")
doTriviaQuestion(msg);
if(game[id] !== undefined) {
if(str.toUpperCase() == letters[game[id].correct_id] && game[id].inProgress) {
// Only counts if this is the first time they type an answer
if(game[id].participants.indexOf(msg.author.id)) {
game[id].correct_users.push(msg.author.id);
game[id].correct_names.push(msg.author.username);
}
}
if(game[id].inProgress && (str == "A" || str == "B" || game[id].isTrueFalse != 1 && (str == "C"|| str == "D")))
game[id].participants.push(msg.author.id);
}
};
function doTriviaQuestion(msg) {
var id = msg.channel.id;
if(game[id] === undefined || game[id].inProgress != 1)
game[id] = {};
else
return;
https.get("https://opentdb.com/api.php?amount=1", (res) => {
res.on('data', function(data) {
var json = JSON.parse(data.toString());
var answers = [];
if(json.response_code !== 0) {
msg.channel.send("An error occurred.");
return;
}
answers[0] = json.results[0].correct_answer;
answers = answers.concat(json.results[0].incorrect_answers);
if(json.results[0].incorrect_answers.length == 1)
game[id].isTrueFalse = 1;
var color = 3447003;
switch(json.results[0].difficulty) {
case "easy":
color = 4249664;
break;
case "medium":
color = 12632064;
break;
case "hard":
color = 14164000;
break;
}
// Sort the answers in reverse alphabetical order.
answers.sort();
answers.reverse();
var answerString = "";
for(var i = 0; i <= answers.length-1; i++) {
if(answers[i] == json.results[0].correct_answer)
game[id].correct_id = i;
answerString = answerString + "**" + letters[i] + ":** " + entities.decode(answers[i]) + "\n";
}
var categoryString = entities.decode(json.results[0].category);
msg.channel.send({embed: {
color: color,
description: "*" + categoryString + "*\n**" + entities.decode(json.results[0].question) + "**\n" + answerString
}});
game[id].answer = json.results[0].correct_answer;
game[id].inProgress = 1;
game[id].participants = [];
game[id].correct_users = [];
game[id].correct_names = [];
game[id].correct_times = []; // Not implemented
// After eight seconds, we reveal the answer!
setTimeout(function() {
var correct_users_str = "**Correct answers:**\n";
if(game[id].correct_names.length == 0)
correct_users_str = correct_users_str + "Nobody!";
else {
if(game[id].correct_names.length == 1)
correct_users_str = "Correct!"; // Only one player, make things simple.
else if(game[id].correct_names.length > 10) {
// More than 10 players, player names are separated by comma
var comma = ", ";
for(var i = 0; i <= game[id].correct_names.length-1; i++) {
if(i == game[id].correct_names.length-1)
comma = "";
correct_users_str = correct_users_str + game[id].correct_names[i] + comma;
}
}
else {
// Less than 10 players, all names are on their own line.
for(var i2 = 0; i2 <= game[id].correct_names.length-1; i2++) {
correct_users_str = correct_users_str + game[id].correct_names[i2] + "\n";
}
}
}
msg.channel.send({embed: {
color: color,
description: "**" + letters[game[id].correct_id] + ":** " + entities.decode(game[id].answer) + "\n\n" + correct_users_str
}});
var participants = game[id].participants;
game[id] = {};
if(participants.length != 0)
setTimeout(() => {
doTriviaQuestion(msg);
}, 3500);
}, 12000);
});
});
}