Browse Source

Merge branch 'feature-slash-commands' of github.com:ADawesomeguy/AwesomeSciBo into feature-slash-commands

pull/10/head
Abheek Dhawan 4 years ago
parent
commit
cd2548b285
  1. 141
      bot/awesomescibo.js

141
bot/awesomescibo.js

@ -36,7 +36,7 @@ client.once("ready", () => {
"description": "Replies with a help message explaining what the bot can do" "description": "Replies with a help message explaining what the bot can do"
}, },
{ {
"name": "round", "name": "rounds",
"options": [ "options": [
{ {
"type": 1, "type": 1,
@ -248,54 +248,6 @@ function sendHelpMessage(interaction) {
); );
} }
async function generateRound(interaction) {
const generatingMessage = interaction.reply("Generating...");
let i;
let finalizedHTML = '<html><head><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> </head><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 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 = `<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 += `<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 = htmlContent.replace(/\n/g, "<br>");
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) { async function startScoring(message) {
let scoreA = 0; let scoreA = 0;
let scoreB = 0; let scoreB = 0;
@ -413,29 +365,77 @@ function aboutMessage(message) {
); );
} }
async function userRounds(interaction) { async function rounds(action, interaction) {
let rounds = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); if (action === "generate") {
let finalMessage = ""; const generatingMessage = interaction.reply("Generating...");
if (!rounds) { let i;
interaction.reply("You haven't requested any rounds!"); let finalizedHTML = '<html><head><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> </head><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>';
return; 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 = `<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 += `<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 = htmlContent.replace(/\n/g, "<br>");
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) { if (rounds.length > 5) {
rounds = rounds.slice(0, 5); rounds = rounds.slice(0, 5);
} }
rounds.forEach(async (item, index) => { rounds.forEach(async (item, index) => {
finalMessage += `${index + 1}. [${item.timestamp.split("T")[0]}](https://api.adawesome.tech/round/${item._id.toString()})\n`; finalMessage += `${index + 1}. [${item.timestamp.split("T")[0]}](https://api.adawesome.tech/round/${item._id.toString()})\n`;
}); });
const roundsEmbed = new Discord.MessageEmbed() const roundsEmbed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL()) .setAuthor(message.author.tag, message.author.displayAvatarURL())
.setTitle(`Last 5 rounds requested by ${message.author.tag}`) .setTitle(`Last 5 rounds requested by ${message.author.tag}`)
.setDescription(finalMessage) .setDescription(finalMessage)
.setTimestamp(); .setTimestamp();
interaction.reply(roundsEmbed); interaction.reply(roundsEmbed, { ephemeral: true });
}
} }
async function hits(message) { async function hits(message) {
@ -515,12 +515,9 @@ client.on("interaction", interaction => {
case "train": case "train":
training(interaction.options[0] ? interaction.options[0].value : null, interaction); training(interaction.options[0] ? interaction.options[0].value : null, interaction);
break; break;
case "round generate": case "rounds":
generateRound(interaction); generateRound(interaction);
break; break;
case "round list":
userRounds(interaction);
break;
} }
}) })

Loading…
Cancel
Save