Browse Source

Update line endings

development
Abheek Dhawan 3 years ago
parent
commit
6ac88895c5
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 154
      src/commands/about.ts
  2. 28
      src/commands/help.ts
  3. 242
      src/commands/rounds.ts
  4. 80
      src/commands/score.ts
  5. 124
      src/commands/top.ts
  6. 456
      src/commands/train.ts
  7. 94
      src/helpers/db.ts
  8. 3852
      yarn.lock

154
src/commands/about.ts

@ -1,77 +1,77 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed, CommandInteraction } from 'discord.js'; import { MessageEmbed, CommandInteraction } from 'discord.js';
import gitlog from 'gitlog'; import gitlog from 'gitlog';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('about') .setName('about')
.setDescription('Commands regarding the creation/development of the bot') .setDescription('Commands regarding the creation/development of the bot')
.addSubcommand(subcommand => { .addSubcommand(subcommand => {
subcommand subcommand
.setName('contributors') .setName('contributors')
.setDescription('Lists contributors to the AwesomeSciBo bot'); .setDescription('Lists contributors to the AwesomeSciBo bot');
return subcommand; return subcommand;
}) })
.addSubcommand(subcommand => { .addSubcommand(subcommand => {
subcommand subcommand
.setName('changelog') .setName('changelog')
.setDescription('Lists the 5 most recent changes in a "git log" type format'); .setDescription('Lists the 5 most recent changes in a "git log" type format');
return subcommand; return subcommand;
}) })
.addSubcommand(subcommand => { .addSubcommand(subcommand => {
subcommand subcommand
.setName('bot') .setName('bot')
.setDescription('Lists information about AwesomeSciBo'); .setDescription('Lists information about AwesomeSciBo');
return subcommand; return subcommand;
}); });
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply(); await interaction.deferReply();
const client = interaction.client; const client = interaction.client;
const action = interaction.options.getSubcommand(); const action = interaction.options.getSubcommand();
if (action === 'contributors') { if (action === 'contributors') {
const contributorEmbed = new MessageEmbed().setTitle('Contributors') const contributorEmbed = new MessageEmbed().setTitle('Contributors')
.addField('Creator', '<@745063586422063214> [ADawesomeguy#3602]', true) .addField('Creator', '<@745063586422063214> [ADawesomeguy#3602]', true)
.addField('Contributors', '<@650525101048987649> [tEjAs#8127]\n<@426864344463048705> [tetrident#9396]', true) // Add more contributors here, first one is Abheek, second one is Tejas .addField('Contributors', '<@650525101048987649> [tEjAs#8127]\n<@426864344463048705> [tetrident#9396]', true) // Add more contributors here, first one is Abheek, second one is Tejas
.setTimestamp() .setTimestamp()
.setColor('#ffffff'); .setColor('#ffffff');
interaction.followUp({ embeds: [contributorEmbed] }); interaction.followUp({ embeds: [contributorEmbed] });
} }
else if (action === 'changelog') { else if (action === 'changelog') {
const gitRepoLocation = __dirname; const gitRepoLocation = __dirname;
const commits = gitlog({ const commits = gitlog({
repo: gitRepoLocation, repo: gitRepoLocation,
number: 5, number: 5,
fields: ['hash', 'abbrevHash', 'subject', 'authorName', 'authorDateRel'], fields: ['hash', 'abbrevHash', 'subject', 'authorName', 'authorDateRel'],
}); });
const changelogEmbed = new MessageEmbed() const changelogEmbed = new MessageEmbed()
.setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() }) .setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() })
.setTitle('Changelog') .setTitle('Changelog')
.setColor('#ffffff') .setColor('#ffffff')
.setTimestamp(); .setTimestamp();
commits.forEach(commit => { commits.forEach(commit => {
changelogEmbed.addField(commit.abbrevHash, `> \`Hash:\`${commit.hash}\n> \`Subject:\`${commit.subject}\n> \`Author:\`${commit.authorName}\n> \`Date:\`${commit.authorDateRel}\n> \`Link\`: [GitHub](https://github.com/ADawesomeguy/AwesomeSciBo/commit/${commit.hash})\n`); changelogEmbed.addField(commit.abbrevHash, `> \`Hash:\`${commit.hash}\n> \`Subject:\`${commit.subject}\n> \`Author:\`${commit.authorName}\n> \`Date:\`${commit.authorDateRel}\n> \`Link\`: [GitHub](https://github.com/ADawesomeguy/AwesomeSciBo/commit/${commit.hash})\n`);
}); });
interaction.followUp({ embeds: [changelogEmbed] }); interaction.followUp({ embeds: [changelogEmbed] });
} }
else if (action === 'bot') { else if (action === 'bot') {
await client.guilds.fetch(); await client.guilds.fetch();
const trainingDocuments = await userScore.countDocuments({}); const trainingDocuments = await userScore.countDocuments({});
const aboutBotEmbed = new MessageEmbed() const aboutBotEmbed = new MessageEmbed()
.setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() }) .setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() })
.setTitle('About AwesomeSciBo') .setTitle('About AwesomeSciBo')
.addField('Servers', `${client.guilds.cache.size}`, true) .addField('Servers', `${client.guilds.cache.size}`, true)
.addField('Training Users', `${trainingDocuments}`, true) .addField('Training Users', `${trainingDocuments}`, true)
.setTimestamp(); .setTimestamp();
interaction.followUp({ embeds: [aboutBotEmbed] }); interaction.followUp({ embeds: [aboutBotEmbed] });
} }
} }

28
src/commands/help.ts

@ -1,15 +1,15 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed, CommandInteraction } from 'discord.js'; import { MessageEmbed, CommandInteraction } from 'discord.js';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('help') .setName('help')
.setDescription('Replies with a help message explaining what the bot can do'); .setDescription('Replies with a help message explaining what the bot can do');
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply(); await interaction.deferReply();
const helpEmbed = new MessageEmbed() const helpEmbed = new MessageEmbed()
.setDescription('AwesomeSciBo has migrated to using slash commands! You can take a look at the different commands by typing `/` and clicking on the AwesomeSciBo icon.') .setDescription('AwesomeSciBo has migrated to using slash commands! You can take a look at the different commands by typing `/` and clicking on the AwesomeSciBo icon.')
.setColor('#ffffff'); .setColor('#ffffff');
interaction.followUp({ embeds: [helpEmbed] }); interaction.followUp({ embeds: [helpEmbed] });
} }

242
src/commands/rounds.ts

@ -1,122 +1,122 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed, CommandInteraction } from 'discord.js'; import { MessageEmbed, CommandInteraction } from 'discord.js';
import axios from 'axios'; import axios from 'axios';
import log from '../helpers/log'; import log from '../helpers/log';
import generatedRound from '../models/generatedRound'; import generatedRound from '../models/generatedRound';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('rounds') .setName('rounds')
.setDescription('Commands regarding the generation of rounds') .setDescription('Commands regarding the generation of rounds')
.addSubcommand(subcommand => { .addSubcommand(subcommand => {
subcommand subcommand
.setName('generate') .setName('generate')
.setDescription('Generates a round with randomized questions from https://scibowldb.com/'); .setDescription('Generates a round with randomized questions from https://scibowldb.com/');
return subcommand; return subcommand;
}) })
.addSubcommand(subcommand => { .addSubcommand(subcommand => {
subcommand subcommand
.setName('list') .setName('list')
.setDescription('Lists your 5 most recently generated rounds with links'); .setDescription('Lists your 5 most recently generated rounds with links');
return subcommand; return subcommand;
}) })
.addSubcommand(subcommand => { .addSubcommand(subcommand => {
subcommand subcommand
.setName('hit') .setName('hit')
.setDescription('Shows the total number of rounds hit as well as the number for the specific user'); .setDescription('Shows the total number of rounds hit as well as the number for the specific user');
return subcommand; return subcommand;
}); });
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
const action = interaction.options.getSubcommand(); const action = interaction.options.getSubcommand();
switch (action) { switch (action) {
case 'generate': { case 'generate': {
interaction.deferReply({ ephemeral: true }); 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 : string; let tossup_question : string;
let question_category : string; let question_category : string;
let tossup_format : string; let tossup_format : string;
let tossup_answer : string; let tossup_answer : string;
let bonus_question : string; let bonus_question : string;
let bonus_format : string; let bonus_format : string;
let bonus_answer : string; 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 (let 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;
question_category = questionData.category; question_category = questionData.category;
tossup_format = questionData.tossup_format; tossup_format = questionData.tossup_format;
bonus_question = questionData.bonus_question; bonus_question = questionData.bonus_question;
bonus_answer = questionData.bonus_answer; bonus_answer = questionData.bonus_answer;
bonus_format = questionData.bonus_format; bonus_format = questionData.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>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 += '<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>'); htmlContent = htmlContent.replace(/\n/g, '<br>');
finalizedHTML += htmlContent; finalizedHTML += htmlContent;
} }
const newGeneratedRound = new generatedRound({ const newGeneratedRound = new generatedRound({
htmlContent: finalizedHTML, htmlContent: finalizedHTML,
requestedBy: interaction.user.id, requestedBy: interaction.user.id,
authorTag: interaction.user.tag, authorTag: interaction.user.tag,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
}); });
newGeneratedRound.save((err, round) => { newGeneratedRound.save((err, round) => {
if (err) { if (err) {
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({ content: `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; break;
} }
case 'list': { case 'list': {
interaction.deferReply({ ephemeral: true }); 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 rounds!'); interaction.followUp('You haven\'t requested any rounds!');
return; return;
} }
if (roundsList.length > 5) { if (roundsList.length > 5) {
roundsList = roundsList.slice(0, 5); roundsList = roundsList.slice(0, 5);
} }
roundsList.forEach(async (item, index) => { roundsList.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 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();
interaction.followUp({ interaction.followUp({
embeds: [roundsListEmbed], embeds: [roundsListEmbed],
ephemeral: true, ephemeral: true,
}); });
break; break;
} }
case 'hit': { case 'hit': {
await interaction.deferReply(); 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; break;
} }
} }
} }

80
src/commands/score.ts

@ -1,40 +1,40 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { CommandInteraction, MessageEmbed } from 'discord.js'; import { CommandInteraction, MessageEmbed } from 'discord.js';
import log from '../helpers/log'; import log from '../helpers/log';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('score') .setName('score')
.setDescription('Returns the score of the current user or another') .setDescription('Returns the score of the current user or another')
.addUserOption(option => { .addUserOption(option => {
option option
.setName('user') .setName('user')
.setDescription('The user to find the score for') .setDescription('The user to find the score for')
.setRequired(false); .setRequired(false);
return option; return option;
}); });
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
const scoreEmbed = new MessageEmbed() const scoreEmbed = new MessageEmbed()
.setColor('#ffffff'); .setColor('#ffffff');
const user = interaction.options.getUser('user') || interaction.user; const user = interaction.options.getUser('user') || interaction.user;
userScore.findOne({ authorID: user.id }, async (err, score) => { userScore.findOne({ authorID: user.id }, async (err, score) => {
if (err) { if (err) {
log({ logger: 'db', content: `Unable to obtain user: ${err}`, level: 'info' }); log({ logger: 'db', content: `Unable to obtain user: ${err}`, level: 'info' });
} }
if (!score) { if (!score) {
await interaction.reply({ content: 'Unfortunately, that user does not seem to have used AwesomeSciBo yet.', ephemeral: true }); await interaction.reply({ content: 'Unfortunately, that user does not seem to have used AwesomeSciBo yet.', ephemeral: true });
return; return;
} }
scoreEmbed scoreEmbed
.setAuthor({ name: user.tag, iconURL: user.displayAvatarURL() }) .setAuthor({ name: user.tag, iconURL: user.displayAvatarURL() })
.setDescription(`Score: \`${score.score}\``); .setDescription(`Score: \`${score.score}\``);
await interaction.reply({ embeds: [scoreEmbed] }); await interaction.reply({ embeds: [scoreEmbed] });
}); });
} }

124
src/commands/top.ts

@ -1,63 +1,63 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { CommandInteraction, MessageEmbed } from 'discord.js'; import { CommandInteraction, MessageEmbed } from 'discord.js';
import log from '../helpers/log'; import log from '../helpers/log';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('top') .setName('top')
.setDescription('Lists top ten scores across servers (server specific leaderboard WIP)'); .setDescription('Lists top ten scores across servers (server specific leaderboard WIP)');
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply(); await interaction.deferReply();
userScore userScore
.find({}) .find({})
.sort({ score: -1 }) // Sort by descending order .sort({ score: -1 }) // Sort by descending order
.exec(async (err, obj) => { .exec(async (err, obj) => {
if (err) { if (err) {
log({ logger: 'top', content: `Getting top players failed: ${err}`, level: 'error' }); log({ logger: 'top', content: `Getting top players failed: ${err}`, level: 'error' });
console.log(err); console.log(err);
} }
if (obj.length < 10) { if (obj.length < 10) {
// Need at least 10 scores for top 10 // Need at least 10 scores for top 10
return interaction.followUp( return interaction.followUp(
`There are only ${obj.length} users, we need at least 10!`, `There are only ${obj.length} users, we need at least 10!`,
); );
} }
const embeds : MessageEmbed[] = []; const embeds : MessageEmbed[] = [];
let lbMessageContent = ''; let lbMessageContent = '';
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
lbMessageContent += `${i + 1}: <@${obj[i].authorID}>: ${obj[i].score}\n`; // Loop through each user and add their name and score to leaderboard content lbMessageContent += `${i + 1}: <@${obj[i].authorID}>: ${obj[i].score}\n`; // Loop through each user and add their name and score to leaderboard content
} }
const leaderboardEmbed = new MessageEmbed() const leaderboardEmbed = new MessageEmbed()
.setTitle('Top Ten!') .setTitle('Top Ten!')
.setDescription(lbMessageContent) .setDescription(lbMessageContent)
.setColor('#ffffff'); .setColor('#ffffff');
embeds.push(leaderboardEmbed); embeds.push(leaderboardEmbed);
let sMessageContent = ''; let sMessageContent = '';
const members = await interaction.guild?.members.fetch(); const members = await interaction.guild?.members.fetch();
const serverLeaderBoardArray = await obj.filter(o => members?.some(m => m.user.id === o.authorID)); const serverLeaderBoardArray = await obj.filter(o => members?.some(m => m.user.id === o.authorID));
if (serverLeaderBoardArray.length > 10) { if (serverLeaderBoardArray.length > 10) {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
sMessageContent += `${i + 1}: <@${serverLeaderBoardArray[i].authorID}>: ${serverLeaderBoardArray[i].score}\n`; sMessageContent += `${i + 1}: <@${serverLeaderBoardArray[i].authorID}>: ${serverLeaderBoardArray[i].score}\n`;
} }
const sLeaderboardEmbed = new MessageEmbed() const sLeaderboardEmbed = new MessageEmbed()
.setTitle(`Top Ten in ${interaction.guild?.name}!`) .setTitle(`Top Ten in ${interaction.guild?.name}!`)
.setDescription(sMessageContent) .setDescription(sMessageContent)
.setColor('#ffffff'); .setColor('#ffffff');
embeds.push(sLeaderboardEmbed); embeds.push(sLeaderboardEmbed);
} }
interaction.followUp({ embeds: embeds }); interaction.followUp({ embeds: embeds });
}); });
} }

456
src/commands/train.ts

@ -1,228 +1,228 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed, MessageActionRow, MessageButton, CommandInteraction, Message } from 'discord.js'; import { MessageEmbed, MessageActionRow, MessageButton, CommandInteraction, Message } from 'discord.js';
import { decode } from 'html-entities'; import { decode } from 'html-entities';
import axios from 'axios'; import axios from 'axios';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
import log from '../helpers/log.js'; import log from '../helpers/log.js';
import { updateScore } from '../helpers/db.js'; import { updateScore } from '../helpers/db.js';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('train') .setName('train')
.setDescription('Sends a training question to be answered') .setDescription('Sends a training question to be answered')
.addStringOption(option => { .addStringOption(option => {
option option
.setName('subject') .setName('subject')
.setDescription('Optional subject to be used as a filter') .setDescription('Optional subject to be used as a filter')
.setRequired(false) .setRequired(false)
.addChoice('astro', 'astro') .addChoice('astro', 'astro')
.addChoice('bio', 'bio') .addChoice('bio', 'bio')
.addChoice('ess', 'ess') .addChoice('ess', 'ess')
.addChoice('chem', 'chem') .addChoice('chem', 'chem')
.addChoice('phys', 'phys') .addChoice('phys', 'phys')
.addChoice('math', 'math') .addChoice('math', 'math')
.addChoice('energy', 'energy') .addChoice('energy', 'energy')
.setRequired(false); .setRequired(false);
return option; return option;
}); });
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
await interaction.deferReply(); await interaction.deferReply();
const subject = interaction.options.get('subject') ? interaction.options.get('subject')?.value : null; const subject = interaction.options.get('subject') ? interaction.options.get('subject')?.value : null;
const authorId = interaction.user.id; const authorId = interaction.user.id;
let score; let score;
userScore userScore
.findOne({ authorID: authorId }) .findOne({ authorID: authorId })
.lean() .lean()
.then((obj, err) => { .then((obj, err) => {
if (!obj) { if (!obj) {
score = 0; score = 0;
} }
else if (obj) { else if (obj) {
score = obj.score; score = obj.score;
} }
else { else {
log({ logger: 'train', content: `Getting user score failed: ${err}`, level: 'error' }); log({ logger: 'train', content: `Getting user score failed: ${err}`, level: 'error' });
} }
}); });
let categoryArray : string[] = []; let categoryArray : string[] = [];
switch (subject) { switch (subject) {
case null: case null:
categoryArray = ['BIOLOGY', 'PHYSICS', 'CHEMISTRY', 'EARTH AND SPACE', 'ASTRONOMY', 'MATH']; categoryArray = ['BIOLOGY', 'PHYSICS', 'CHEMISTRY', 'EARTH AND SPACE', 'ASTRONOMY', 'MATH'];
break; break;
case 'astro': case 'astro':
case 'astronomy': case 'astronomy':
categoryArray = ['ASTRONOMY']; categoryArray = ['ASTRONOMY'];
break; break;
case 'bio': case 'bio':
case 'biology': case 'biology':
categoryArray = ['BIOLOGY']; categoryArray = ['BIOLOGY'];
break; break;
case 'ess': case 'ess':
case 'earth science': case 'earth science':
case 'es': case 'es':
categoryArray = ['EARTH SCIENCE']; categoryArray = ['EARTH SCIENCE'];
break; break;
case 'chem': case 'chem':
case 'chemistry': case 'chemistry':
categoryArray = ['CHEMISTRY']; categoryArray = ['CHEMISTRY'];
break; break;
case 'phys': case 'phys':
case 'physics': case 'physics':
categoryArray = ['PHYSICS']; categoryArray = ['PHYSICS'];
break; break;
case 'math': case 'math':
categoryArray = ['MATH']; categoryArray = ['MATH'];
break; break;
case 'energy': case 'energy':
categoryArray = ['ENERGY']; categoryArray = ['ENERGY'];
break; break;
default: default:
interaction.followUp({ interaction.followUp({
embeds: [new MessageEmbed() embeds: [new MessageEmbed()
.setDescription('<:red_x:816791117671825409> Not a valid subject!') .setDescription('<:red_x:816791117671825409> Not a valid subject!')
.setColor('#ffffff')], .setColor('#ffffff')],
}); });
return; return;
} }
axios axios
.post('https://scibowldb.com/api/questions/random', { categories: categoryArray }) .post('https://scibowldb.com/api/questions/random', { categories: categoryArray })
.then((res) => { .then((res) => {
const questionData = res.data.question; const questionData = res.data.question;
const tossupQuestion = questionData.tossup_question; const tossupQuestion = questionData.tossup_question;
const tossupAnswer = questionData.tossup_answer; const tossupAnswer = questionData.tossup_answer;
const tossupFormat = questionData.tossup_format; const tossupFormat = questionData.tossup_format;
let answers = tossupAnswer.split(' (ACCEPT: '); let answers = tossupAnswer.split(' (ACCEPT: ');
if (answers.length > 1) { if (answers.length > 1) {
answers[1] = answers[1].slice(0, answers[1].length - 1); // If there are multiple elements, it means there was an 'accept' and therefore a trailing ')' which should be removed answers[1] = answers[1].slice(0, answers[1].length - 1); // If there are multiple elements, it means there was an 'accept' and therefore a trailing ')' which should be removed
answers = [answers[0], ...answers[1].split(new RegExp(' OR ', 'i'))]; // Use the first element plus the last element split by 'OR' case insensitive answers = [answers[0], ...answers[1].split(new RegExp(' OR ', 'i'))]; // Use the first element plus the last element split by 'OR' case insensitive
} }
interaction.followUp({ content: decode(tossupQuestion), fetchReply: true }) interaction.followUp({ content: decode(tossupQuestion), fetchReply: true })
.then(q => { .then(q => {
const questionMessage = q as Message; const questionMessage = q as Message;
const sourceButton = new MessageActionRow() const sourceButton = new MessageActionRow()
.addComponents( .addComponents(
new MessageButton() new MessageButton()
.setURL(questionData.uri) .setURL(questionData.uri)
.setLabel('Source') .setLabel('Source')
.setStyle('LINK'), .setStyle('LINK'),
); );
switch (tossupFormat) { switch (tossupFormat) {
case 'Short Answer': { case 'Short Answer': {
// eslint-disable-next-line no-case-declarations // eslint-disable-next-line no-case-declarations
const messageFilter = m => m.author.id === interaction.user.id || m.author.id === interaction.client.user?.id; const messageFilter = m => m.author.id === interaction.user.id || m.author.id === interaction.client.user?.id;
interaction.channel?.awaitMessages({ interaction.channel?.awaitMessages({
filter: messageFilter, filter: messageFilter,
max: 1, max: 1,
}) })
.then(collected => { .then(collected => {
const answerMsg = collected.first(); const answerMsg = collected.first();
if (answerMsg?.author.id === interaction.client.user?.id) return; if (answerMsg?.author.id === interaction.client.user?.id) return;
let predicted = ''; let predicted = '';
if (answerMsg?.content.toLowerCase() === tossupAnswer.toLowerCase() || answers.includes(answerMsg?.content.toUpperCase())) { if (answerMsg?.content.toLowerCase() === tossupAnswer.toLowerCase() || answers.includes(answerMsg?.content.toUpperCase())) {
predicted = 'correct'; predicted = 'correct';
} }
else { else {
predicted = 'incorrect'; predicted = 'incorrect';
} }
if (predicted === 'correct') { if (predicted === 'correct') {
updateScore(true, score, authorId).then((msgToReply) => updateScore(true, score, authorId).then((msgToReply) =>
answerMsg?.reply(msgToReply), answerMsg?.reply(msgToReply),
); );
} }
else { else {
const overrideEmbed = new MessageEmbed() const overrideEmbed = new MessageEmbed()
.setAuthor({ name: answerMsg?.author.tag ? answerMsg.author.tag : '', iconURL: answerMsg?.author.displayAvatarURL() }) .setAuthor({ name: answerMsg?.author.tag ? answerMsg.author.tag : '', iconURL: answerMsg?.author.displayAvatarURL() })
.addField('Correct answer', `\`${tossupAnswer}\``) .addField('Correct answer', `\`${tossupAnswer}\``)
.setDescription('It seems your answer was incorrect. Please react with <:override:955265585086857236> to override your answer if you think you got it right.') .setDescription('It seems your answer was incorrect. Please react with <:override:955265585086857236> to override your answer if you think you got it right.')
.setColor('#ffffff') .setColor('#ffffff')
.setTimestamp(); .setTimestamp();
const overrideButton = new MessageActionRow() const overrideButton = new MessageActionRow()
.addComponents( .addComponents(
new MessageButton() new MessageButton()
.setCustomId('override') .setCustomId('override')
.setEmoji('<:override:955265585086857236>') .setEmoji('<:override:955265585086857236>')
.setStyle('SECONDARY'), .setStyle('SECONDARY'),
); );
answerMsg?.channel.send({ answerMsg?.channel.send({
embeds: [overrideEmbed], embeds: [overrideEmbed],
components: [overrideButton], components: [overrideButton],
}) })
.then(overrideMsg => { .then(overrideMsg => {
const overrideFilter = i => { const overrideFilter = i => {
return ( return (
['override'].includes(i.customId) && ['override'].includes(i.customId) &&
i.user.id === answerMsg.author.id i.user.id === answerMsg.author.id
); );
}; };
overrideMsg overrideMsg
.awaitMessageComponent({ .awaitMessageComponent({
filter: overrideFilter, filter: overrideFilter,
}) })
.then(i => { .then(i => {
updateScore(true, score, authorId).then(async msgToReply => { updateScore(true, score, authorId).then(async msgToReply => {
await i.reply(msgToReply); await i.reply(msgToReply);
overrideMsg.edit({ components: [] }); overrideMsg.edit({ components: [] });
}); });
}).catch(err => log({ logger: 'train', content: `Failed to override score: ${err}`, level: 'error' })); }).catch(err => log({ logger: 'train', content: `Failed to override score: ${err}`, level: 'error' }));
}).catch(err => log({ logger: 'train', content: `Failed to send override message: ${err}`, level: 'error' })); }).catch(err => log({ logger: 'train', content: `Failed to send override message: ${err}`, level: 'error' }));
} }
interaction.editReply({ components: [sourceButton] }); interaction.editReply({ components: [sourceButton] });
}).catch(err => log({ logger: 'train', content: `${err}`, level: 'error' })); }).catch(err => log({ logger: 'train', content: `${err}`, level: 'error' }));
break; break;
} }
case 'Multiple Choice': { case 'Multiple Choice': {
const choices = new MessageActionRow() const choices = new MessageActionRow()
.addComponents( .addComponents(
new MessageButton() new MessageButton()
.setCustomId('w') .setCustomId('w')
.setLabel('W') .setLabel('W')
.setStyle('SECONDARY'), .setStyle('SECONDARY'),
new MessageButton() new MessageButton()
.setCustomId('x') .setCustomId('x')
.setLabel('X') .setLabel('X')
.setStyle('SECONDARY'), .setStyle('SECONDARY'),
new MessageButton() new MessageButton()
.setCustomId('y') .setCustomId('y')
.setLabel('Y') .setLabel('Y')
.setStyle('SECONDARY'), .setStyle('SECONDARY'),
new MessageButton() new MessageButton()
.setCustomId('z') .setCustomId('z')
.setLabel('Z') .setLabel('Z')
.setStyle('SECONDARY'), .setStyle('SECONDARY'),
); );
interaction.editReply({ components: [choices] }); interaction.editReply({ components: [choices] });
const mcFilter = i => ['w', 'x', 'y', 'z'].includes(i.customId) && i.user.id === interaction.user.id; const mcFilter = i => ['w', 'x', 'y', 'z'].includes(i.customId) && i.user.id === interaction.user.id;
questionMessage.awaitMessageComponent({ filter: mcFilter }) questionMessage.awaitMessageComponent({ filter: mcFilter })
.then(mcChoice => { .then(mcChoice => {
if (tossupAnswer.charAt(0).toLowerCase() === mcChoice.customId) { if (tossupAnswer.charAt(0).toLowerCase() === mcChoice.customId) {
updateScore(true, score, authorId).then((msgToReply) => updateScore(true, score, authorId).then((msgToReply) =>
mcChoice.reply(msgToReply), mcChoice.reply(msgToReply),
); );
} }
else { else {
const incorrectEmbed = new MessageEmbed() const incorrectEmbed = new MessageEmbed()
.setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() }) .setAuthor({ name: interaction.user.tag, iconURL: interaction.user.displayAvatarURL() })
.addField('Correct answer', `\`${tossupAnswer}\``) .addField('Correct answer', `\`${tossupAnswer}\``)
.setDescription(`It seems your answer ${mcChoice.customId.toUpperCase()} was incorrect.`) .setDescription(`It seems your answer ${mcChoice.customId.toUpperCase()} was incorrect.`)
.setColor('#ffffff') .setColor('#ffffff')
.setTimestamp(); .setTimestamp();
mcChoice.reply({ embeds: [incorrectEmbed] }); mcChoice.reply({ embeds: [incorrectEmbed] });
} }
interaction.editReply({ components: [sourceButton] }); interaction.editReply({ components: [sourceButton] });
}); });
break; break;
} }
} }
}).catch(err => log({ logger: 'train', content: `${err}`, level: 'error' })); }).catch(err => log({ logger: 'train', content: `${err}`, level: 'error' }));
}).catch(err => log({ logger: 'train', content: `${err}`, level: 'error' })); }).catch(err => log({ logger: 'train', content: `${err}`, level: 'error' }));
} }

94
src/helpers/db.ts

@ -1,47 +1,47 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import log from '../helpers/log'; import log from '../helpers/log';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
export async function updateScore(isCorrect : boolean, score : number, authorId : string) { export async function updateScore(isCorrect : boolean, score : number, authorId : string) {
if (!isCorrect) { if (!isCorrect) {
return `Nice try! Your score is still ${score}.`; return `Nice try! Your score is still ${score}.`;
} }
else { else {
// TODO: Error handling // TODO: Error handling
const doc = await userScore.findOne({ const doc = await userScore.findOne({
authorID: authorId, authorID: authorId,
}); });
if (!doc) { if (!doc) {
const newUserScore = new userScore({ const newUserScore = new userScore({
authorID: authorId, authorID: authorId,
score: score + 4, score: score + 4,
}); });
newUserScore.save(err => { newUserScore.save(err => {
if (err) { if (err) {
log({ logger: 'db', content: `Error creating new user ${authorId} for scoring`, level: 'error' }); log({ logger: 'db', content: `Error creating new user ${authorId} for scoring`, level: 'error' });
} }
else { else {
log({ logger: 'db', content: `Successfully created user ${authorId} for scoring`, level: 'debug' }); log({ logger: 'db', content: `Successfully created user ${authorId} for scoring`, level: 'debug' });
} }
}); });
} }
else { else {
doc.score = doc.score + 4; doc.score = doc.score + 4;
doc.save(); doc.save();
} }
return `Great job! Your score is now ${score + 4}.`; return `Great job! Your score is now ${score + 4}.`;
} }
} }
export async function connect(mongoUri) { export async function connect(mongoUri) {
mongoose mongoose
.connect(mongoUri, { .connect(mongoUri, {
useUnifiedTopology: true, useUnifiedTopology: true,
useNewUrlParser: true, useNewUrlParser: true,
}) })
.then(() => log({ logger: 'db', content: `Connected to the database at ${mongoUri}!`, level: 'info' })) .then(() => log({ logger: 'db', content: `Connected to the database at ${mongoUri}!`, level: 'info' }))
.catch(err => log({ logger: 'db', content: `Failed to connect to the database at ${mongoUri}: ${err}`, level: 'fatal' })); .catch(err => log({ logger: 'db', content: `Failed to connect to the database at ${mongoUri}: ${err}`, level: 'fatal' }));
} }

3852
yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save