@ -1,37 +1,37 @@
import { SlashCommandBuilder } from "@discordjs/builders" ;
import { SlashCommandBuilder } from '@discordjs/builders' ;
import {
MessageEmbed ,
MessageActionRow ,
MessageButton ,
CommandInteraction ,
Message ,
} from "discord.js" ;
} from 'discord.js' ;
import { decode } from "html-entities" ;
import axios from "axios" ;
import { decode } from 'html-entities' ;
import axios from 'axios' ;
import userScore from "../models/userScore" ;
import userConfig from "../models/userConfig" ;
import userScore from '../models/userScore' ;
import userConfig from '../models/userConfig' ;
import log from "../helpers/log.js" ;
import { updateScore } from "../helpers/db.js" ;
import log from '../helpers/log.js' ;
import { updateScore } from '../helpers/db.js' ;
export const data = new SlashCommandBuilder ( )
. setName ( "train" )
. setDescription ( "Sends a training question to be answered" )
. setName ( 'train' )
. setDescription ( 'Sends a training question to be answered' )
. addStringOption ( ( option ) = > {
option
. setName ( "subject" )
. setDescription ( "Optional subject to be used as a filter" )
. setName ( 'subject' )
. setDescription ( 'Optional subject to be used as a filter' )
. setRequired ( false )
. addChoices (
{ name : "astro" , value : "astro" } ,
{ name : "bio" , value : "bio" } ,
{ name : "chem" , value : "chem" } ,
{ name : "ess" , value : "ess" } ,
{ name : "phys" , value : "phys" } ,
{ name : "math" , value : "math" } ,
{ name : "energy" , value : "energy" }
{ name : 'astro' , value : 'astro' } ,
{ name : 'bio' , value : 'bio' } ,
{ name : 'chem' , value : 'chem' } ,
{ name : 'ess' , value : 'ess' } ,
{ name : 'phys' , value : 'phys' } ,
{ name : 'math' , value : 'math' } ,
{ name : 'energy' , value : 'energy' }
)
. setRequired ( false ) ;
return option ;
@ -40,8 +40,8 @@ export const data = new SlashCommandBuilder()
export async function execute ( interaction : CommandInteraction ) {
await interaction . deferReply ( ) ;
const subject = interaction . options . get ( "subject" )
? interaction . options . get ( "subject" ) ? . value
const subject = interaction . options . get ( 'subject' )
? interaction . options . get ( 'subject' ) ? . value
: null ;
const authorId = interaction . user . id ;
let score : number ;
@ -55,43 +55,43 @@ export async function execute(interaction: CommandInteraction) {
. setAuthor ( {
name : interaction.client.user?.tag
? interaction . client . user ? . tag
: "" ,
: '' ,
iconURL : interaction.client.user?.displayAvatarURL ( ) ,
} )
. setDescription (
"Hey! It seems like it's your first time using AwesomeSciBo. Here's some information regarding the bot if you need it (for issues, contributions, etc.):"
)
. addField ( "Creator" , "<@745063586422063214> [@abheekd#3602]" )
. addField ( 'Creator' , '<@745063586422063214> [@abheekd#3602]' )
. addField (
"GitHub" ,
'GitHub' ,
"[Link](https://github.com/ADawesomeguy/AwesomeSciBo) (a star couldn't hurt...)"
)
. setColor ( "#ffffff" )
. setColor ( '#ffffff' )
. setTimestamp ( ) ;
interaction . user
. send ( { embeds : [ firstTimeEmbed ] } )
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
} else if ( obj ) {
score = obj . score ;
} else {
log ( {
logger : "train" ,
logger : 'train' ,
content : ` Getting user score failed: ${ err } ` ,
level : "error" ,
level : 'error' ,
} ) ;
}
} ) ;
let categoryArray : string [ ] = [ ] ;
const allCategories = [
"BIOLOGY" ,
"PHYSICS" ,
"CHEMISTRY" ,
"EARTH AND SPACE" ,
"ASTRONOMY" ,
"MATH" ,
'BIOLOGY' ,
'PHYSICS' ,
'CHEMISTRY' ,
'EARTH AND SPACE' ,
'ASTRONOMY' ,
'MATH' ,
] ;
const configCategories = await userConfig . findById ( interaction . user . id ) ;
@ -101,46 +101,46 @@ export async function execute(interaction: CommandInteraction) {
? configCategories . subjects || allCategories
: allCategories ;
break ;
case "astro" :
case "astronomy" :
categoryArray = [ "ASTRONOMY" ] ;
case 'astro' :
case 'astronomy' :
categoryArray = [ 'ASTRONOMY' ] ;
break ;
case "bio" :
case "biology" :
categoryArray = [ "BIOLOGY" ] ;
case 'bio' :
case 'biology' :
categoryArray = [ 'BIOLOGY' ] ;
break ;
case "ess" :
case "earth science" :
case "es" :
categoryArray = [ "EARTH SCIENCE" ] ;
case 'ess' :
case 'earth science' :
case 'es' :
categoryArray = [ 'EARTH SCIENCE' ] ;
break ;
case "chem" :
case "chemistry" :
categoryArray = [ "CHEMISTRY" ] ;
case 'chem' :
case 'chemistry' :
categoryArray = [ 'CHEMISTRY' ] ;
break ;
case "phys" :
case "physics" :
categoryArray = [ "PHYSICS" ] ;
case 'phys' :
case 'physics' :
categoryArray = [ 'PHYSICS' ] ;
break ;
case "math" :
categoryArray = [ "MATH" ] ;
case 'math' :
categoryArray = [ 'MATH' ] ;
break ;
case "energy" :
categoryArray = [ "ENERGY" ] ;
case 'energy' :
categoryArray = [ 'ENERGY' ] ;
break ;
default :
interaction . followUp ( {
embeds : [
new MessageEmbed ( )
. setDescription ( "<:red_x:816791117671825409> Not a valid subject!" )
. setColor ( "#ffffff" ) ,
. setDescription ( '<:red_x:816791117671825409> Not a valid subject!' )
. setColor ( '#ffffff' ) ,
] ,
} ) ;
return ;
}
axios
. post ( "https://scibowldb.com/api/questions/random" , {
. post ( 'https://scibowldb.com/api/questions/random' , {
categories : categoryArray ,
} )
. then ( ( res ) = > {
@ -148,10 +148,10 @@ export async function execute(interaction: CommandInteraction) {
const tossupQuestion = questionData . tossup_question ;
const tossupAnswer = questionData . tossup_answer ;
const tossupFormat = questionData . tossup_format ;
let answers = tossupAnswer . split ( " (ACCEPT: " ) ;
let answers = tossupAnswer . split ( ' (ACCEPT: ' ) ;
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 = [ 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 } )
@ -160,11 +160,11 @@ export async function execute(interaction: CommandInteraction) {
const sourceButton = new MessageActionRow ( ) . addComponents (
new MessageButton ( )
. setURL ( questionData . uri )
. setLabel ( "Source" )
. setStyle ( "LINK" )
. setLabel ( 'Source' )
. setStyle ( 'LINK' )
) ;
switch ( tossupFormat ) {
case "Short Answer" : {
case 'Short Answer' : {
// eslint-disable-next-line no-case-declarations
const messageFilter = ( m ) = >
m . author . id === interaction . user . id ||
@ -180,38 +180,38 @@ export async function execute(interaction: CommandInteraction) {
if ( answerMsg ? . author . id === interaction . client . user ? . id )
return ;
let predicted = "" ;
let predicted = '' ;
if (
answerMsg ? . content . toLowerCase ( ) ===
tossupAnswer . toLowerCase ( ) ||
answers . includes ( answerMsg ? . content . toUpperCase ( ) )
) {
predicted = "correct" ;
predicted = 'correct' ;
} else {
predicted = "incorrect" ;
predicted = 'incorrect' ;
}
if ( predicted === "correct" ) {
if ( predicted === 'correct' ) {
updateScore ( true , score , authorId ) . then ( ( msgToReply ) = >
answerMsg ? . reply ( msgToReply )
) ;
} else {
const overrideEmbed = new MessageEmbed ( )
. setAuthor ( {
name : answerMsg?.author.tag ? answerMsg . author . tag : "" ,
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."
' 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 ( ) ;
const overrideButton = new MessageActionRow ( ) . addComponents (
new MessageButton ( )
. setCustomId ( "override" )
. setEmoji ( "<:override:955265585086857236>" )
. setStyle ( "SECONDARY" )
. setCustomId ( 'override' )
. setEmoji ( '<:override:955265585086857236>' )
. setStyle ( 'SECONDARY' )
) ;
answerMsg ? . channel
. send ( {
@ -221,7 +221,7 @@ export async function execute(interaction: CommandInteraction) {
. then ( ( overrideMsg ) = > {
const overrideFilter = ( i ) = > {
return (
[ "override" ] . includes ( i . customId ) &&
[ 'override' ] . includes ( i . customId ) &&
i . user . id === answerMsg . author . id
) ;
} ;
@ -239,49 +239,49 @@ export async function execute(interaction: CommandInteraction) {
} )
. catch ( ( err ) = >
log ( {
logger : "train" ,
logger : 'train' ,
content : ` Failed to override score: ${ err } ` ,
level : "error" ,
level : 'error' ,
} )
) ;
} )
. catch ( ( err ) = >
log ( {
logger : "train" ,
logger : 'train' ,
content : ` Failed to send override message: ${ err } ` ,
level : "error" ,
level : 'error' ,
} )
) ;
}
interaction . editReply ( { components : [ sourceButton ] } ) ;
} )
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
break ;
}
case "Multiple Choice" : {
case 'Multiple Choice' : {
const choices = new MessageActionRow ( ) . addComponents (
new MessageButton ( )
. setCustomId ( "w" )
. setLabel ( "W" )
. setStyle ( "SECONDARY" ) ,
. setCustomId ( 'w' )
. setLabel ( 'W' )
. setStyle ( 'SECONDARY' ) ,
new MessageButton ( )
. setCustomId ( "x" )
. setLabel ( "X" )
. setStyle ( "SECONDARY" ) ,
. setCustomId ( 'x' )
. setLabel ( 'X' )
. setStyle ( 'SECONDARY' ) ,
new MessageButton ( )
. setCustomId ( "y" )
. setLabel ( "Y" )
. setStyle ( "SECONDARY" ) ,
. setCustomId ( 'y' )
. setLabel ( 'Y' )
. setStyle ( 'SECONDARY' ) ,
new MessageButton ( )
. setCustomId ( "z" )
. setLabel ( "Z" )
. setStyle ( "SECONDARY" )
. setCustomId ( 'z' )
. setLabel ( 'Z' )
. setStyle ( 'SECONDARY' )
) ;
interaction . editReply ( { components : [ choices ] } ) ;
const mcFilter = ( i ) = >
[ "w" , "x" , "y" , "z" ] . includes ( i . customId ) &&
[ 'w' , 'x' , 'y' , 'z' ] . includes ( i . customId ) &&
i . user . id === interaction . user . id ;
questionMessage
. awaitMessageComponent ( { filter : mcFilter } )
@ -298,11 +298,11 @@ export async function execute(interaction: CommandInteraction) {
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. `
)
. setColor ( "#ffffff" )
. setColor ( '#ffffff' )
. setTimestamp ( ) ;
mcChoice . reply ( { embeds : [ incorrectEmbed ] } ) ;
}
@ -313,10 +313,10 @@ export async function execute(interaction: CommandInteraction) {
}
} )
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
} )
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
}