Browse Source

Send one request and locally find random instead of sending 25 requests

pull/9/head
Abheek Dhawan 4 years ago
parent
commit
dc47011945
  1. 69
      bot/awesomescibo.js
  2. 1519
      bot/package-lock.json

69
bot/awesomescibo.js

@ -308,7 +308,6 @@ async function generateRound(message) {
const generatingMessage = message.channel.send("Generating..."); const generatingMessage = message.channel.send("Generating...");
let i; let i;
let finalizedHTML = '<html><body style="width: 70%; margin-left: auto; margin-right: auto;"><h2 style="text-align: center; text-decoration: underline overline; padding: 7px;">ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API</h2>'; let finalizedHTML = '<html><body style="width: 70%; margin-left: auto; margin-right: auto;"><h2 style="text-align: center; text-decoration: underline overline; padding: 7px;">ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API</h2>';
for (i = 1; i < 26; i++) {
let tossup_question; let tossup_question;
let question_category; let question_category;
let tossup_format; let tossup_format;
@ -317,42 +316,40 @@ async function generateRound(message) {
let bonus_format; let bonus_format;
let bonus_answer; let bonus_answer;
let htmlContent = ""; let htmlContent = "";
await fetch("https://scibowldb.com/api/questions/random") await axios.post("https://scibowldb.com/api/questions", { categories: ["BIOLOGY", "PHYSICS", "EARTH AND SPACE", "ASTRONOMY", "MATH"] })
.then((response) => response.json()) .then((response) => {
.then((data) => { for (i = 1; i < 26; i++) {
tossup_question = data.question.tossup_question; data = response.data.questions[Math.floor(Math.random() * response.data.questions.length)];
tossup_answer = data.question.tossup_answer; tossup_question = data.tossup_question;
question_category = data.question.category; tossup_answer = data.tossup_answer;
tossup_format = data.question.tossup_format; question_category = data.category;
bonus_question = data.question.bonus_question; tossup_format = data.tossup_format;
bonus_answer = data.question.bonus_answer; bonus_question = data.bonus_question;
bonus_format = data.question.bonus_format; bonus_answer = data.bonus_answer;
htmlContent = `<br><br><h3 style="text-align: center;"><strong>TOSS-UP</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br>"; bonus_format = data.bonus_format;
htmlContent += `<br><br><h3 style="text-align: center;"><strong>BONUS</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br><hr><br>"; htmlContent = `<br><br><h3 style="text-align: center;"><strong>TOSS-UP</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br>";
htmlContent = htmlContent.replace(/\n/g, "<br>"); htmlContent += `<br><br><h3 style="text-align: center;"><strong>BONUS</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br><hr><br>";
finalizedHTML += htmlContent; htmlContent = htmlContent.replace(/\n/g, "<br>");
if (i === 25) { finalizedHTML += htmlContent;
newGeneratedRound = new generatedRound({ }
htmlContent: finalizedHTML, newGeneratedRound = new generatedRound({
requestedBy: message.author.id, htmlContent: finalizedHTML,
authorTag: message.author.tag, requestedBy: message.author.id,
timestamp: new Date().toISOString(), authorTag: message.author.tag,
timestamp: new Date().toISOString(),
});
newGeneratedRound.save((err, round) => {
if (err) {
console.log(err);
return;
}
message.channel.messages.fetch(generatingMessage.id)
.then(generatingMessage => {
const msg = generatingMessage.first();
msg.edit(`${message.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`);
}); });
newGeneratedRound.save((err, round) => { });
if (err) { }
console.log(err);
return;
}
message.channel.messages.fetch(generatingMessage.id)
.then(generatingMessage => {
const msg = generatingMessage.first();
msg.edit(`${message.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`);
});
});
}
});
}
}
async function startScoring(message) { async function startScoring(message) {
let scoreA = 0; let scoreA = 0;

1519
bot/package-lock.json

File diff suppressed because it is too large
Loading…
Cancel
Save