From 71703542242fac2fae7a2b3faf601b541edb58d5 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 15 May 2021 19:51:25 -0500 Subject: [PATCH] Fix round commands --- bot/awesomescibo.js | 141 ++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 72 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index ec8325e..711c522 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -36,7 +36,7 @@ client.once("ready", () => { "description": "Replies with a help message explaining what the bot can do" }, { - "name": "round", + "name": "rounds", "options": [ { "type": 1, @@ -247,54 +247,6 @@ function sendHelpMessage(interaction) { ); } -async function generateRound(interaction) { - const generatingMessage = interaction.reply("Generating..."); - let i; - let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; - let tossup_question; - let question_category; - let tossup_format; - let tossup_answer; - let bonus_question; - let bonus_format; - let bonus_answer; - let htmlContent = ""; - await axios.post("https://scibowldb.com/api/questions", { categories: ["BIOLOGY", "PHYSICS", "CHEMISTRY", "EARTH AND SPACE", "ASTRONOMY", "MATH"] }) - .then((response) => { - for (i = 1; i < 26; i++) { - data = response.data.questions[Math.floor(Math.random() * response.data.questions.length)]; - tossup_question = data.tossup_question; - tossup_answer = data.tossup_answer; - question_category = data.category; - tossup_format = data.tossup_format; - bonus_question = data.bonus_question; - bonus_answer = data.bonus_answer; - bonus_format = data.bonus_format; - htmlContent = `

TOSS-UP

\n
` + `${i}) ${question_category}` + " " + `${tossup_format}` + " " + tossup_question + "

" + "ANSWER: " + tossup_answer + "
"; - htmlContent += `

BONUS

\n
` + `${i}) ${question_category}` + " " + `${bonus_format}` + " " + bonus_question + "

" + "ANSWER: " + bonus_answer + "



"; - htmlContent = htmlContent.replace(/\n/g, "
"); - finalizedHTML += htmlContent; - } - newGeneratedRound = new generatedRound({ - htmlContent: finalizedHTML, - requestedBy: interaction.author.id, - authorTag: interaction.author.tag, - timestamp: new Date().toISOString(), - }); - newGeneratedRound.save((err, round) => { - if (err) { - console.log(err); - return; - } - interaction.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) { let scoreA = 0; let scoreB = 0; @@ -412,29 +364,77 @@ function aboutMessage(message) { ); } -async function userRounds(interaction) { - let rounds = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); - let finalMessage = ""; - if (!rounds) { - interaction.reply("You haven't requested any rounds!"); - return; - } +async function rounds(action, interaction) { + if (action === "generate") { + const generatingMessage = interaction.reply("Generating..."); + let i; + let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; + let tossup_question; + let question_category; + let tossup_format; + let tossup_answer; + let bonus_question; + let bonus_format; + let bonus_answer; + let htmlContent = ""; + await axios.post("https://scibowldb.com/api/questions", { categories: ["BIOLOGY", "PHYSICS", "CHEMISTRY", "EARTH AND SPACE", "ASTRONOMY", "MATH"] }) + .then((response) => { + for (i = 1; i < 26; i++) { + data = response.data.questions[Math.floor(Math.random() * response.data.questions.length)]; + tossup_question = data.tossup_question; + tossup_answer = data.tossup_answer; + question_category = data.category; + tossup_format = data.tossup_format; + bonus_question = data.bonus_question; + bonus_answer = data.bonus_answer; + bonus_format = data.bonus_format; + htmlContent = `

TOSS-UP

\n
` + `${i}) ${question_category}` + " " + `${tossup_format}` + " " + tossup_question + "

" + "ANSWER: " + tossup_answer + "
"; + htmlContent += `

BONUS

\n
` + `${i}) ${question_category}` + " " + `${bonus_format}` + " " + bonus_question + "

" + "ANSWER: " + bonus_answer + "



"; + htmlContent = htmlContent.replace(/\n/g, "
"); + finalizedHTML += htmlContent; + } + newGeneratedRound = new generatedRound({ + htmlContent: finalizedHTML, + requestedBy: interaction.author.id, + authorTag: interaction.author.tag, + timestamp: new Date().toISOString(), + }); + newGeneratedRound.save((err, round) => { + if (err) { + console.log(err); + return; + } + interaction.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()}`); + }); + }); + }); + } else if (action === "list"){ + let rounds = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); + let finalMessage = ""; + if (!rounds) { + interaction.reply("You haven't requested any rounds!"); + return; + } - if (rounds.length > 5) { - rounds = rounds.slice(0, 5); - } + if (rounds.length > 5) { + rounds = rounds.slice(0, 5); + } - rounds.forEach(async (item, index) => { - finalMessage += `${index + 1}. [${item.timestamp.split("T")[0]}](https://api.adawesome.tech/round/${item._id.toString()})\n`; - }); + rounds.forEach(async (item, index) => { + finalMessage += `${index + 1}. [${item.timestamp.split("T")[0]}](https://api.adawesome.tech/round/${item._id.toString()})\n`; + }); - const roundsEmbed = new Discord.MessageEmbed() - .setAuthor(message.author.tag, message.author.displayAvatarURL()) - .setTitle(`Last 5 rounds requested by ${message.author.tag}`) - .setDescription(finalMessage) - .setTimestamp(); + const roundsEmbed = new Discord.MessageEmbed() + .setAuthor(message.author.tag, message.author.displayAvatarURL()) + .setTitle(`Last 5 rounds requested by ${message.author.tag}`) + .setDescription(finalMessage) + .setTimestamp(); - interaction.reply(roundsEmbed); + interaction.reply(roundsEmbed, { ephemeral: true }); + } } async function hits(message) { @@ -514,12 +514,9 @@ client.on("interaction", interaction => { case "train": training(interaction.options[0] ? interaction.options[0].value : null, interaction); break; - case "round generate": + case "rounds": generateRound(interaction); break; - case "round list": - userRounds(interaction); - break; } })