@ -1,7 +1,9 @@
#!/usr/bin/env node
#!/usr/bin/env node
const Discord = require ( "discord.js" ) ;
const Discord = require ( "discord.js" ) ;
const Intents = Discord . Intents ;
const client = new Discord . Client ( {
const client = new Discord . Client ( {
intents : [ Intents . FLAGS . GUILDS , Intents . FLAGS . GUILD_MESSAGES ] ,
partials : [ "MESSAGE" , "CHANNEL" , "REACTION" ] ,
partials : [ "MESSAGE" , "CHANNEL" , "REACTION" ] ,
} ) ;
} ) ;
const fetch = require ( "node-fetch" ) ;
const fetch = require ( "node-fetch" ) ;
@ -15,6 +17,30 @@ const helpMessage =
"`do be helping`: display this help message\n`do be roundgen`: send a pdf round to the channel\n`do be scoring`: start a scoring session\n > `do be scoring (a/b)(4/10)`: add points to Team A or Team B\n > `do be scoring stop`: end scoring session and post final points\n > `do be servers`: send the number of servers this bot is a part of\n > `do be iss`: show the current location of the International Space Station\n`do be training`: send a quick practice problem (you **must** react to your answer, or the bot will yell at you)\n > subject options: astro, phys, chem, math, bio, ess, energy\n`do be top`: list cross-server top 10 players\n `do be about`: List people who contributed to this bot\n Source Code: https://github.com/ADawesomeguy/AwesomeSciBo (don't forget to star!)" ;
"`do be helping`: display this help message\n`do be roundgen`: send a pdf round to the channel\n`do be scoring`: start a scoring session\n > `do be scoring (a/b)(4/10)`: add points to Team A or Team B\n > `do be scoring stop`: end scoring session and post final points\n > `do be servers`: send the number of servers this bot is a part of\n > `do be iss`: show the current location of the International Space Station\n`do be training`: send a quick practice problem (you **must** react to your answer, or the bot will yell at you)\n > subject options: astro, phys, chem, math, bio, ess, energy\n`do be top`: list cross-server top 10 players\n `do be about`: List people who contributed to this bot\n Source Code: https://github.com/ADawesomeguy/AwesomeSciBo (don't forget to star!)" ;
client . once ( "ready" , ( ) => {
client . once ( "ready" , ( ) => {
// Register slash command
const commandData = [
{
"name" : "train" ,
"description" : "sends a single training question to be answered" ,
"options" : [
{
"type" : 3 ,
"name" : "subject" ,
"description" : "optional subject to be used as a filter" ,
"default" : false ,
"required" : false
}
]
} ,
{
"name" : "help" ,
"description" : "replies with a help message explaining what the bot can do"
}
]
commandData . forEach ( commandData => {
client . application . commands . create ( commandData ) ;
} )
// Connect to MongoDB using mongoose
// Connect to MongoDB using mongoose
if ( ! process . env . CI ) {
if ( ! process . env . CI ) {
mongoose
mongoose
@ -47,7 +73,7 @@ client.on("guildCreate", (guild) => {
async function updateScore ( isCorrect , score , authorId ) {
async function updateScore ( isCorrect , score , authorId ) {
if ( ! isCorrect ) {
if ( ! isCorrect ) {
return ` n ice try! Your score is still ${ score } . ` ;
return ` N ice try! Your score is still ${ score } . ` ;
} else {
} else {
score += 4 ;
score += 4 ;
if ( score == 4 ) {
if ( score == 4 ) {
@ -68,167 +94,143 @@ async function updateScore(isCorrect, score, authorId) {
doc . save ( ) ;
doc . save ( ) ;
}
}
return ` g reat job! Your score is now ${ score } . ` ;
return ` G reat job! Your score is now ${ score } . ` ;
}
}
}
}
async function otherCommands ( message ) {
function training ( subject , interaction ) {
if (
const authorId = interaction . member . user . id ;
message . content . toLowerCase ( ) . startsWith ( "do be announcing" ) &&
let score ;
( message . author . id === process . env . ABHEEK_USER_ID ||
userScore
message . author . id === process . env . TEJAS_USER_ID )
. findOne ( { authorID : authorId } )
) {
. lean ( )
const announcement = message . content . substring ( 17 ) ;
. then ( ( obj , err ) => {
client . guilds . cache . forEach ( ( guild ) => {
if ( ! obj ) {
const channel = guild . channels . cache . find (
score = 0 ;
( channelGeneral ) =>
} else if ( obj ) {
channelGeneral . name === process . env . ANNOUNCING_CHANNEL
score = obj . score ;
) ;
} else {
if ( channel ) {
console . log ( err ) ;
if ( channel . type === "text" ) {
channel . send ( announcement ) . catch ( console . error ) ;
}
}
}
} ) ;
} ) ;
} else if ( message . content . toLowerCase ( ) . startsWith ( "do be training" ) ) {
const authorId = message . author . id ;
let score ;
userScore
. findOne ( { authorID : authorId } )
. lean ( )
. then ( ( obj , err ) => {
if ( ! obj ) {
score = 0 ;
} else if ( obj ) {
score = obj . score ;
} else {
console . log ( err ) ;
}
} ) ;
const subject = message . content . substring ( 15 ) ;
let categoryArray = [ ] ;
switch ( subject ) {
case "" :
categoryArray = [ "BIOLOGY" , "PHYSICS" , "CHEMISTRY" , "EARTH AND SPACE" , "ASTRONOMY" , "MATH" ] ;
break ;
case "astro" :
case "astronomy" :
categoryArray = [ "ASTRONOMY" ]
break ;
case "bio" :
case "biology" :
categoryArray = [ "BIOLOGY" ] ;
break ;
case "ess" :
case "earth science" :
case "es" :
categoryArray = [ "EARTH SCIENCE" ] ;
break ;
case "chem" :
case "chemistry" :
categoryArray = [ "CHEMISTRY" ] ;
break ;
case "phys" :
case "physics" :
categoryArray = [ "PHYSICS" ] ;
break ;
case "math" :
categoryArray = [ "MATH" ] ;
break ;
case "energy" :
categoryArray = [ "ENERGY" ] ;
break ;
default :
message . channel . send ( "Not a valid subject!" ) ;
return ;
}
axios
let categoryArray = [ ] ;
. post ( "https://scibowldb.com/api/questions/random" , { categories : categoryArray } )
. then ( ( res ) => {
switch ( subject ) {
data = res . data . question ;
case null :
const messageFilter = ( m ) => m . author . id === authorId ;
categoryArray = [ "BIOLOGY" , "PHYSICS" , "CHEMISTRY" , "EARTH AND SPACE" , "ASTRONOMY" , "MATH" ] ;
message . reply ( data . tossup_question ) . then ( ( ) => {
break ;
message . channel
case "astro" :
. awaitMessages ( messageFilter , {
case "astronomy" :
max : 1 ,
categoryArray = [ "ASTRONOMY" ]
time : 30000 ,
break ;
errors : [ "time" ] ,
case "bio" :
} )
case "biology" :
. then ( ( answerMsg ) => {
categoryArray = [ "BIOLOGY" ] ;
answerMsg = answerMsg . first ( ) ;
break ;
case "ess" :
let predicted = null ;
case "earth science" :
if ( data . tossup_format === "Multiple Choice" ) {
case "es" :
if (
categoryArray = [ "EARTH SCIENCE" ] ;
answerMsg . content . charAt ( 0 ) . toLowerCase ( ) ===
break ;
data . tossup_answer . charAt ( 0 ) . toLowerCase ( )
case "chem" :
) {
case "chemistry" :
predicted = "correct" ;
categoryArray = [ "CHEMISTRY" ] ;
} else {
break ;
predicted = "incorrect" ;
case "phys" :
}
case "physics" :
categoryArray = [ "PHYSICS" ] ;
break ;
case "math" :
categoryArray = [ "MATH" ] ;
break ;
case "energy" :
categoryArray = [ "ENERGY" ] ;
break ;
default :
interaction . reply ( "Not a valid subject!" ) ;
return ;
}
axios
. post ( "https://scibowldb.com/api/questions/random" , { categories : categoryArray } )
. then ( ( res ) => {
data = res . data . question ;
const messageFilter = ( m ) => m . author . id === authorId ;
interaction . reply ( data . tossup_question ) . then ( ( ) => {
interaction . channel
. awaitMessages ( messageFilter , {
max : 1 ,
time : 30000 ,
errors : [ "time" ] ,
} )
. then ( ( answerMsg ) => {
answerMsg = answerMsg . first ( ) ;
let predicted = null ;
if ( data . tossup_format === "Multiple Choice" ) {
if (
answerMsg . content . charAt ( 0 ) . toLowerCase ( ) ===
data . tossup_answer . charAt ( 0 ) . toLowerCase ( )
) {
predicted = "correct" ;
} else {
} else {
if (
predicted = "incorrect" ;
answerMsg . content . toLowerCase ( ) ===
data . tossup_answer . toLowerCase ( )
) {
predicted = "correct" ;
} else {
predicted = "incorrect" ;
}
}
}
} else {
if ( predicted === "correct" ) {
if (
updateScore ( true , score , authorId ) . then ( ( msgToReply ) =>
answerMsg . content . toLowerCase ( ) ===
answerMsg . reply ( msgToReply )
data . tossup_answer . toLowerCase ( )
) ;
) {
predicted = "correct" ;
} else {
} else {
const overrideEmbed = new Discord . MessageEmbed ( )
predicted = "incorrect" ;
. setAuthor ( answerMsg . author . tag , answerMsg . author . displayAvatarURL ( ) )
. addField ( "Correct answer" , ` \` ${ data . tossup_answer } \` ` )
. setDescription ( ` It seems your answer was incorrect. Please react with <:override:842778128966615060> to override your answer if you think you got it right. ` )
. setTimestamp ( ) ;
const overrideMsg = answerMsg . channel . send (
overrideEmbed
)
. then ( overrideMsg => {
overrideMsg . react ( "<:override:842778128966615060>" ) ;
const filter = ( reaction , user ) => {
return (
[ "override" ] . includes ( reaction . emoji . name ) &&
user . id === answerMsg . author . id
) ;
} ;
overrideMsg
. awaitReactions ( filter , {
max : 1 ,
time : 600000 ,
errors : [ "time" ] ,
} )
. then ( ( userReaction ) => {
updateScore ( true , score , authorId ) . then ( ( msgToReply ) =>
answerMsg . reply ( msgToReply )
) ;
} ) ;
} )
}
}
} )
}
. catch ( console . error ) ;
} ) ;
if ( predicted === "correct" ) {
} )
updateScore ( true , score , authorId ) . then ( ( msgToReply ) =>
. catch ( console . error ) ;
answerMsg . reply ( msgToReply )
} else {
) ;
// Not any of the commands supported
} else {
message . channel . send (
const overrideEmbed = new Discord . MessageEmbed ( )
"That didn't quite make sense! Please use `do be helping` to see the available commands."
. setAuthor ( answerMsg . author . tag , answerMsg . author . displayAvatarURL ( ) )
) ;
. addField ( "Correct answer" , ` \` ${ data . tossup_answer } \` ` )
}
. setDescription ( ` It seems your answer was incorrect. Please react with <:override:842778128966615060> to override your answer if you think you got it right. ` )
}
. setTimestamp ( ) ;
const overrideMsg = answerMsg . channel . send (
overrideEmbed
)
. then ( overrideMsg => {
overrideMsg . react ( "<:override:842778128966615060>" ) ;
const filter = ( reaction , user ) => {
return (
[ "override" ] . includes ( reaction . emoji . name ) &&
user . id === answerMsg . author . id
) ;
} ;
overrideMsg
. awaitReactions ( filter , {
max : 1 ,
time : 600000 ,
errors : [ "time" ] ,
} )
. then ( ( userReaction ) => {
updateScore ( true , score , authorId ) . then ( ( msgToReply ) =>
answerMsg . reply ( msgToReply )
) ;
} ) ;
} )
}
} )
. catch ( console . error ) ;
} ) ;
} )
. catch ( console . error ) ;
}
function sendHelpMessage ( message ) {
function sendHelpMessage ( interaction ) {
message . channel . send (
interaction . reply (
new Discord . MessageEmbed ( ) . setTitle ( "Help" ) . setDescription ( helpMessage )
new Discord . MessageEmbed ( ) . setTitle ( "Help" ) . setDescription ( helpMessage )
) ;
) ;
}
}
@ -402,7 +404,7 @@ async function userRounds(message) {
let rounds = await generatedRound . find ( { requestedBy : message . author . id } ) . sort ( { timestamp : - 1 } ) ;
let rounds = await generatedRound . find ( { requestedBy : message . author . id } ) . sort ( { timestamp : - 1 } ) ;
let finalMessage = "" ;
let finalMessage = "" ;
if ( ! rounds ) {
if ( ! rounds ) {
message . reply ( "y ou haven't requested any rounds!" ) ;
message . reply ( "Y ou haven't requested any rounds!" ) ;
return ;
return ;
}
}
@ -457,17 +459,10 @@ client.on("message", async (message) => {
if ( message . author . bot ) {
if ( message . author . bot ) {
return ;
return ;
}
}
// Temporary logging purposes
const formattedMessage = message . content . toLowerCase ( ) . replace ( / /g , "" ) ;
const formattedMessage = message . content . toLowerCase ( ) . replace ( / /g , "" ) ;
if ( formattedMessage . startsWith ( "dobe" ) ) {
if ( formattedMessage . startsWith ( "dobe" ) ) {
console . log ( ` ${ message . author . tag } > ${ message . content } ` ) ;
// Bot prefix is "do be"
// Bot prefix is "do be"
switch ( formattedMessage ) {
switch ( formattedMessage ) {
case "dobehelping" : // Display help message
sendHelpMessage ( message ) ;
break ;
case "doberoundgen" : // Generate round publicly
case "doberoundgen" : // Generate round publicly
generateRound ( message ) ;
generateRound ( message ) ;
break ;
break ;
@ -505,6 +500,20 @@ client.on("message", async (message) => {
}
}
} ) ;
} ) ;
client . on ( "interaction" , interaction => {
// If the interaction isn't a slash command, return
if ( ! interaction . isCommand ( ) ) return ;
switch ( interaction . commandName ) {
case "help" :
sendHelpMessage ( interaction ) ;
break ;
case "train" :
training ( interaction . options [ 0 ] ? interaction . options [ 0 ] . value : null , interaction ) ;
break ;
}
} )
client
client
. login ( process . env . TOKEN )
. login ( process . env . TOKEN )
. then ( ( ) => console . log ( "Running!" ) )
. then ( ( ) => console . log ( "Running!" ) )