Abheek Dhawan
3 years ago
1 changed files with 110 additions and 0 deletions
@ -0,0 +1,110 @@ |
|||
const { SlashCommandBuilder } = require('@discordjs/builders'); |
|||
const { MessageEmbed } = require('discord.js'); |
|||
|
|||
const axios = require('axios'); |
|||
|
|||
const generatedRound = require('../models/generateRound'); |
|||
|
|||
module.exports = { |
|||
data: new SlashCommandBuilder() |
|||
.setName('rounds') |
|||
.setDescription('Commands regarding the generation of rounds') |
|||
.addSubcommand(subcommand => { |
|||
subcommand |
|||
.setName('generate') |
|||
.setDescription('Generates a round with randomized questions from https://scibowldb.com/'); |
|||
return subcommand; |
|||
}) |
|||
.addSubcommand(subcommand => { |
|||
subcommand |
|||
.setName('list') |
|||
.setDescription('Lists your 5 most recently generated rounds with links'); |
|||
return subcommand; |
|||
}) |
|||
.addSubcommand(subcommand => { |
|||
subcommand |
|||
.setName('hit') |
|||
.setDescription('Shows the total number of rounds hit as well as the number for the specific user'); |
|||
return subcommand; |
|||
}), |
|||
async execute(interaction) { |
|||
const action = interaction.options.getSubcommand(); |
|||
if (action === 'generate') { |
|||
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++) { |
|||
const 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; |
|||
} |
|||
|
|||
const newGeneratedRound = new generatedRound({ |
|||
htmlContent: finalizedHTML, |
|||
requestedBy: interaction.user.id, |
|||
authorTag: interaction.user.tag, |
|||
timestamp: new Date().toISOString(), |
|||
}); |
|||
|
|||
newGeneratedRound.save((err, round) => { |
|||
if (err) { |
|||
console.log(err); |
|||
return; |
|||
} |
|||
interaction.reply(`Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, { ephemeral: true }); |
|||
}); |
|||
}); |
|||
} |
|||
else if (action === 'list') { |
|||
let roundsList = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); |
|||
let finalMessage = ''; |
|||
if (!roundsList) { |
|||
interaction.reply('You haven\'t requested any roundsList!'); |
|||
return; |
|||
} |
|||
|
|||
if (roundsList.length > 5) { |
|||
roundsList = roundsList.slice(0, 5); |
|||
} |
|||
|
|||
roundsList.forEach(async (item, index) => { |
|||
finalMessage += `${index + 1}. [${item.timestamp.split('T')[0]}](https://api.adawesome.tech/round/${item._id.toString()})\n`; |
|||
}); |
|||
|
|||
const roundsListEmbed = new MessageEmbed() |
|||
.setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() }) |
|||
.setTitle(`Last 5 roundsList requested by ${interaction.user.tag}`) |
|||
.setDescription(finalMessage) |
|||
.setTimestamp(); |
|||
|
|||
interaction.reply({ |
|||
embeds: [roundsListEmbed], |
|||
ephemeral: true, |
|||
}); |
|||
} |
|||
else if (action === 'hit') { |
|||
const totalCount = await generatedRound.countDocuments({}); |
|||
const userCount = await generatedRound.countDocuments({ requestedBy: interaction.user.id }); |
|||
|
|||
interaction.reply(`Total Hits: ${totalCount}\nYour Hits: ${userCount}`); |
|||
} |
|||
}, |
|||
}; |
Loading…
Reference in new issue