Browse Source

Clean up rounds.ts and add types

pull/41/head
Abheek Dhawan 3 years ago
parent
commit
6b64b967ff
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 48
      src/commands/rounds.ts

48
src/commands/rounds.ts

@ -1,5 +1,5 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed } from 'discord.js'; import { MessageEmbed, CommandInteraction } from 'discord.js';
import axios from 'axios'; import axios from 'axios';
@ -28,24 +28,24 @@ export const data = new SlashCommandBuilder()
return subcommand; return subcommand;
}); });
export async function execute(interaction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply();
const action = interaction.options.getSubcommand(); const action = interaction.options.getSubcommand();
if (action === 'generate') { switch (action) {
let i; case 'generate': {
interaction.deferReply({ ephemeral: true });
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 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 tossup_question : string;
let question_category; let question_category : string;
let tossup_format; let tossup_format : string;
let tossup_answer; let tossup_answer : string;
let bonus_question; let bonus_question : string;
let bonus_format; let bonus_format : string;
let bonus_answer; let bonus_answer : string;
let htmlContent = ''; let htmlContent = '';
await axios.post('https://scibowldb.com/api/questions', { categories: ['BIOLOGY', 'PHYSICS', 'CHEMISTRY', 'EARTH AND SPACE', 'ASTRONOMY', 'MATH'] }) await axios.post('https://scibowldb.com/api/questions', { categories: ['BIOLOGY', 'PHYSICS', 'CHEMISTRY', 'EARTH AND SPACE', 'ASTRONOMY', 'MATH'] })
.then((response) => { .then((response) => {
for (i = 1; i < 26; i++) { for (let i = 1; i < 26; i++) {
const questionData = response.data.questions[Math.floor(Math.random() * response.data.questions.length)]; const questionData = response.data.questions[Math.floor(Math.random() * response.data.questions.length)];
tossup_question = questionData.tossup_question; tossup_question = questionData.tossup_question;
tossup_answer = questionData.tossup_answer; tossup_answer = questionData.tossup_answer;
@ -72,15 +72,19 @@ export async function execute(interaction) {
log({ logger: 'rounds', content: `Saving round to DB failed: ${err}`, level: 'error' }); log({ logger: 'rounds', content: `Saving round to DB failed: ${err}`, level: 'error' });
return; return;
} }
interaction.followUp(`Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, { ephemeral: true }); interaction.followUp({ content: `Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, ephemeral: true });
}); });
}); });
break;
} }
else if (action === 'list') {
case 'list': {
interaction.deferReply({ ephemeral: true });
let roundsList = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); let roundsList = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 });
let finalMessage = ''; let finalMessage = '';
if (!roundsList) { if (!roundsList) {
interaction.followUp('You haven\'t requested any roundsList!'); interaction.followUp('You haven\'t requested any rounds!');
return; return;
} }
@ -94,7 +98,7 @@ export async function execute(interaction) {
const roundsListEmbed = new MessageEmbed() const roundsListEmbed = new MessageEmbed()
.setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() }) .setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() })
.setTitle('Last 5 Rounds Requested') .setTitle('Last 5 rounds requested')
.setDescription(finalMessage) .setDescription(finalMessage)
.setTimestamp(); .setTimestamp();
@ -102,11 +106,17 @@ export async function execute(interaction) {
embeds: [roundsListEmbed], embeds: [roundsListEmbed],
ephemeral: true, ephemeral: true,
}); });
break;
} }
else if (action === 'hit') {
case 'hit': {
await interaction.deferReply();
const totalCount = await generatedRound.countDocuments({}); const totalCount = await generatedRound.countDocuments({});
const userCount = await generatedRound.countDocuments({ requestedBy: interaction.user.id }); const userCount = await generatedRound.countDocuments({ requestedBy: interaction.user.id });
interaction.followUp(`Total Hits: ${totalCount}\nYour Hits: ${userCount}`); interaction.followUp(`Total Hits: ${totalCount}\nYour Hits: ${userCount}`);
break;
}
} }
} }
Loading…
Cancel
Save