diff --git a/commands/rounds.js b/commands/rounds.js new file mode 100644 index 0000000..a9167c0 --- /dev/null +++ b/commands/rounds.js @@ -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 = '

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++) { + 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 = '

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; + } + + 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}`); + } + }, +};