@ -1,37 +1,37 @@
import { SlashCommandBuilder } from "@discordjs/builders" ;
import { SlashCommandBuilder } from '@discordjs/builders' ;
import {
import {
MessageEmbed ,
MessageEmbed ,
MessageActionRow ,
MessageActionRow ,
MessageButton ,
MessageButton ,
CommandInteraction ,
CommandInteraction ,
Message ,
Message ,
} from "discord.js" ;
} 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 userConfig from "../models/userConfig" ;
import userConfig from '../models/userConfig' ;
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 )
. addChoices (
. addChoices (
{ name : "astro" , value : "astro" } ,
{ name : 'astro' , value : 'astro' } ,
{ name : "bio" , value : "bio" } ,
{ name : 'bio' , value : 'bio' } ,
{ name : "chem" , value : "chem" } ,
{ name : 'chem' , value : 'chem' } ,
{ name : "ess" , value : "ess" } ,
{ name : 'ess' , value : 'ess' } ,
{ name : "phys" , value : "phys" } ,
{ name : 'phys' , value : 'phys' } ,
{ name : "math" , value : "math" } ,
{ name : 'math' , value : 'math' } ,
{ name : "energy" , value : "energy" }
{ name : 'energy' , value : 'energy' }
)
)
. setRequired ( false ) ;
. setRequired ( false ) ;
return option ;
return option ;
@ -40,8 +40,8 @@ export const data = new SlashCommandBuilder()
export async function execute ( interaction : CommandInteraction ) {
export async function execute ( interaction : CommandInteraction ) {
await interaction . deferReply ( ) ;
await interaction . deferReply ( ) ;
const subject = interaction . options . get ( "subject" )
const subject = interaction . options . get ( 'subject' )
? interaction . options . get ( "subject" ) ? . value
? interaction . options . get ( 'subject' ) ? . value
: null ;
: null ;
const authorId = interaction . user . id ;
const authorId = interaction . user . id ;
let score : number ;
let score : number ;
@ -55,43 +55,43 @@ export async function execute(interaction: CommandInteraction) {
. setAuthor ( {
. setAuthor ( {
name : interaction.client.user?.tag
name : interaction.client.user?.tag
? interaction . client . user ? . tag
? interaction . client . user ? . tag
: "" ,
: '' ,
iconURL : interaction.client.user?.displayAvatarURL ( ) ,
iconURL : interaction.client.user?.displayAvatarURL ( ) ,
} )
} )
. setDescription (
. 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.):"
"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 (
. addField (
"GitHub" ,
'GitHub' ,
"[Link](https://github.com/ADawesomeguy/AwesomeSciBo) (a star couldn't hurt...)"
"[Link](https://github.com/ADawesomeguy/AwesomeSciBo) (a star couldn't hurt...)"
)
)
. setColor ( "#ffffff" )
. setColor ( '#ffffff' )
. setTimestamp ( ) ;
. setTimestamp ( ) ;
interaction . user
interaction . user
. send ( { embeds : [ firstTimeEmbed ] } )
. send ( { embeds : [ firstTimeEmbed ] } )
. catch ( ( err ) = >
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
) ;
} else if ( obj ) {
} else if ( obj ) {
score = obj . score ;
score = obj . score ;
} else {
} else {
log ( {
log ( {
logger : "train" ,
logger : 'train' ,
content : ` Getting user score failed: ${ err } ` ,
content : ` Getting user score failed: ${ err } ` ,
level : "error" ,
level : 'error' ,
} ) ;
} ) ;
}
}
} ) ;
} ) ;
let categoryArray : string [ ] = [ ] ;
let categoryArray : string [ ] = [ ] ;
const allCategories = [
const allCategories = [
"BIOLOGY" ,
'BIOLOGY' ,
"PHYSICS" ,
'PHYSICS' ,
"CHEMISTRY" ,
'CHEMISTRY' ,
"EARTH AND SPACE" ,
'EARTH AND SPACE' ,
"ASTRONOMY" ,
'ASTRONOMY' ,
"MATH" ,
'MATH' ,
] ;
] ;
const configCategories = await userConfig . findById ( interaction . user . id ) ;
const configCategories = await userConfig . findById ( interaction . user . id ) ;
@ -101,46 +101,46 @@ export async function execute(interaction: CommandInteraction) {
? configCategories . subjects || allCategories
? configCategories . subjects || allCategories
: allCategories ;
: allCategories ;
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 : [
embeds : [
new MessageEmbed ( )
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" , {
. post ( 'https://scibowldb.com/api/questions/random' , {
categories : categoryArray ,
categories : categoryArray ,
} )
} )
. then ( ( res ) = > {
. then ( ( res ) = > {
@ -148,10 +148,10 @@ export async function execute(interaction: CommandInteraction) {
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
interaction
. followUp ( { content : decode ( tossupQuestion ) , fetchReply : true } )
. followUp ( { content : decode ( tossupQuestion ) , fetchReply : true } )
@ -160,11 +160,11 @@ export async function execute(interaction: CommandInteraction) {
const sourceButton = new MessageActionRow ( ) . addComponents (
const sourceButton = new MessageActionRow ( ) . 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 ) = >
const messageFilter = ( m ) = >
m . author . id === interaction . user . id ||
m . author . id === interaction . user . id ||
@ -180,38 +180,38 @@ export async function execute(interaction: CommandInteraction) {
if ( answerMsg ? . author . id === interaction . client . user ? . id )
if ( answerMsg ? . author . id === interaction . client . user ? . id )
return ;
return ;
let predicted = "" ;
let predicted = '' ;
if (
if (
answerMsg ? . content . toLowerCase ( ) ===
answerMsg ? . content . toLowerCase ( ) ===
tossupAnswer . toLowerCase ( ) ||
tossupAnswer . toLowerCase ( ) ||
answers . includes ( answerMsg ? . content . toUpperCase ( ) )
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 ( {
. setAuthor ( {
name : answerMsg?.author.tag ? answerMsg . author . tag : "" ,
name : answerMsg?.author.tag ? answerMsg . author . tag : '' ,
iconURL : answerMsg?.author.displayAvatarURL ( ) ,
iconURL : answerMsg?.author.displayAvatarURL ( ) ,
} )
} )
. addField ( "Correct answer" , ` \` ${ tossupAnswer } \` ` )
. addField ( 'Correct answer' , ` \` ${ tossupAnswer } \` ` )
. setDescription (
. 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 ( ) ;
. setTimestamp ( ) ;
const overrideButton = new MessageActionRow ( ) . addComponents (
const overrideButton = new MessageActionRow ( ) . addComponents (
new MessageButton ( )
new MessageButton ( )
. setCustomId ( "override" )
. setCustomId ( 'override' )
. setEmoji ( "<:override:955265585086857236>" )
. setEmoji ( '<:override:955265585086857236>' )
. setStyle ( "SECONDARY" )
. setStyle ( 'SECONDARY' )
) ;
) ;
answerMsg ? . channel
answerMsg ? . channel
. send ( {
. send ( {
@ -221,7 +221,7 @@ export async function execute(interaction: CommandInteraction) {
. 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
) ;
) ;
} ;
} ;
@ -239,49 +239,49 @@ export async function execute(interaction: CommandInteraction) {
} )
} )
. catch ( ( err ) = >
. catch ( ( err ) = >
log ( {
log ( {
logger : "train" ,
logger : 'train' ,
content : ` Failed to override score: ${ err } ` ,
content : ` Failed to override score: ${ err } ` ,
level : "error" ,
level : 'error' ,
} )
} )
) ;
) ;
} )
} )
. catch ( ( err ) = >
. catch ( ( err ) = >
log ( {
log ( {
logger : "train" ,
logger : 'train' ,
content : ` Failed to send override message: ${ err } ` ,
content : ` Failed to send override message: ${ err } ` ,
level : "error" ,
level : 'error' ,
} )
} )
) ;
) ;
}
}
interaction . editReply ( { components : [ sourceButton ] } ) ;
interaction . editReply ( { components : [ sourceButton ] } ) ;
} )
} )
. catch ( ( err ) = >
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
) ;
break ;
break ;
}
}
case "Multiple Choice" : {
case 'Multiple Choice' : {
const choices = new MessageActionRow ( ) . addComponents (
const choices = new MessageActionRow ( ) . 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 ) = >
const mcFilter = ( i ) = >
[ "w" , "x" , "y" , "z" ] . includes ( i . customId ) &&
[ 'w' , 'x' , 'y' , 'z' ] . includes ( i . customId ) &&
i . user . id === interaction . user . id ;
i . user . id === interaction . user . id ;
questionMessage
questionMessage
. awaitMessageComponent ( { filter : mcFilter } )
. awaitMessageComponent ( { filter : mcFilter } )
@ -298,11 +298,11 @@ export async function execute(interaction: CommandInteraction) {
name : interaction.user.tag ,
name : interaction.user.tag ,
iconURL : interaction.user.displayAvatarURL ( ) ,
iconURL : interaction.user.displayAvatarURL ( ) ,
} )
} )
. addField ( "Correct answer" , ` \` ${ tossupAnswer } \` ` )
. addField ( 'Correct answer' , ` \` ${ tossupAnswer } \` ` )
. setDescription (
. setDescription (
` It seems your answer ${ mcChoice . customId . toUpperCase ( ) } was incorrect. `
` It seems your answer ${ mcChoice . customId . toUpperCase ( ) } was incorrect. `
)
)
. setColor ( "#ffffff" )
. setColor ( '#ffffff' )
. setTimestamp ( ) ;
. setTimestamp ( ) ;
mcChoice . reply ( { embeds : [ incorrectEmbed ] } ) ;
mcChoice . reply ( { embeds : [ incorrectEmbed ] } ) ;
}
}
@ -313,10 +313,10 @@ export async function execute(interaction: CommandInteraction) {
}
}
} )
} )
. catch ( ( err ) = >
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
) ;
} )
} )
. catch ( ( err ) = >
. catch ( ( err ) = >
log ( { logger : "train" , content : ` ${ err } ` , level : "error" } )
log ( { logger : 'train' , content : ` ${ err } ` , level : 'error' } )
) ;
) ;
}
}