@ -1,7 +1,9 @@
#!/usr/bin/env node
const Discord = require ( "discord.js" ) ;
const Intents = Discord . Intents ;
const client = new Discord . Client ( {
intents : [ Intents . FLAGS . GUILDS , Intents . FLAGS . GUILD_MESSAGES ] ,
partials : [ "MESSAGE" , "CHANNEL" , "REACTION" ] ,
} ) ;
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!)" ;
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
if ( ! process . env . CI ) {
mongoose
@ -47,7 +73,7 @@ client.on("guildCreate", (guild) => {
async function updateScore ( isCorrect , score , authorId ) {
if ( ! isCorrect ) {
return ` n ice try! Your score is still ${ score } . ` ;
return ` N ice try! Your score is still ${ score } . ` ;
} else {
score += 4 ;
if ( score == 4 ) {
@ -68,30 +94,12 @@ async function updateScore(isCorrect, score, authorId) {
doc . save ( ) ;
}
return ` g reat job! Your score is now ${ score } . ` ;
return ` G reat job! Your score is now ${ score } . ` ;
}
}
async function otherCommands ( message ) {
if (
message . content . toLowerCase ( ) . startsWith ( "do be announcing" ) &&
( message . author . id === process . env . ABHEEK_USER_ID ||
message . author . id === process . env . TEJAS_USER_ID )
) {
const announcement = message . content . substring ( 17 ) ;
client . guilds . cache . forEach ( ( guild ) => {
const channel = guild . channels . cache . find (
( channelGeneral ) =>
channelGeneral . name === process . env . ANNOUNCING_CHANNEL
) ;
if ( channel ) {
if ( channel . type === "text" ) {
channel . send ( announcement ) . catch ( console . error ) ;
}
}
} ) ;
} else if ( message . content . toLowerCase ( ) . startsWith ( "do be training" ) ) {
const authorId = message . author . id ;
function training ( subject , interaction ) {
const authorId = interaction . member . user . id ;
let score ;
userScore
. findOne ( { authorID : authorId } )
@ -105,11 +113,11 @@ async function otherCommands(message) {
console . log ( err ) ;
}
} ) ;
const subject = message . content . substring ( 15 ) ;
let categoryArray = [ ] ;
switch ( subject ) {
case "" :
case null :
categoryArray = [ "BIOLOGY" , "PHYSICS" , "CHEMISTRY" , "EARTH AND SPACE" , "ASTRONOMY" , "MATH" ] ;
break ;
case "astro" :
@ -140,7 +148,7 @@ async function otherCommands(message) {
categoryArray = [ "ENERGY" ] ;
break ;
default :
message . channel . send ( "Not a valid subject!" ) ;
interaction . reply ( "Not a valid subject!" ) ;
return ;
}
@ -149,8 +157,8 @@ async function otherCommands(message) {
. then ( ( res ) => {
data = res . data . question ;
const messageFilter = ( m ) => m . author . id === authorId ;
message . reply ( data . tossup_question ) . then ( ( ) => {
message . channel
interaction . reply ( data . tossup_question ) . then ( ( ) => {
interaction . channel
. awaitMessages ( messageFilter , {
max : 1 ,
time : 30000 ,
@ -219,16 +227,10 @@ async function otherCommands(message) {
} ) ;
} )
. catch ( console . error ) ;
} else {
// Not any of the commands supported
message . channel . send (
"That didn't quite make sense! Please use `do be helping` to see the available commands."
) ;
}
}
function sendHelpMessage ( message ) {
message . channel . send (
function sendHelpMessage ( interaction ) {
interaction . reply (
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 finalMessage = "" ;
if ( ! rounds ) {
message . reply ( "y ou haven't requested any rounds!" ) ;
message . reply ( "Y ou haven't requested any rounds!" ) ;
return ;
}
@ -457,17 +459,10 @@ client.on("message", async (message) => {
if ( message . author . bot ) {
return ;
}
// Temporary logging purposes
const formattedMessage = message . content . toLowerCase ( ) . replace ( / /g , "" ) ;
if ( formattedMessage . startsWith ( "dobe" ) ) {
console . log ( ` ${ message . author . tag } > ${ message . content } ` ) ;
// Bot prefix is "do be"
switch ( formattedMessage ) {
case "dobehelping" : // Display help message
sendHelpMessage ( message ) ;
break ;
case "doberoundgen" : // Generate round publicly
generateRound ( message ) ;
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
. login ( process . env . TOKEN )
. then ( ( ) => console . log ( "Running!" ) )