Browse Source

Switch training and helping to slash commands

pull/10/head
Abheek Dhawan 4 years ago
parent
commit
e379af7db7
  1. 95
      bot/awesomescibo.js
  2. 1519
      bot/package-lock.json
  3. 2
      bot/package.json

95
bot/awesomescibo.js

@ -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 `nice try! Your score is still ${score}.`; return `Nice try! Your score is still ${score}.`;
} else { } else {
score += 4; score += 4;
if (score == 4) { if (score == 4) {
@ -68,30 +94,12 @@ async function updateScore(isCorrect, score, authorId) {
doc.save(); doc.save();
} }
return `great job! Your score is now ${score}.`; return `Great 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") &&
(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;
let score; let score;
userScore userScore
.findOne({ authorID: authorId }) .findOne({ authorID: authorId })
@ -105,11 +113,11 @@ async function otherCommands(message) {
console.log(err); console.log(err);
} }
}); });
const subject = message.content.substring(15);
let categoryArray = []; let categoryArray = [];
switch (subject) { switch (subject) {
case "": 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":
@ -140,7 +148,7 @@ async function otherCommands(message) {
categoryArray = ["ENERGY"]; categoryArray = ["ENERGY"];
break; break;
default: default:
message.channel.send("Not a valid subject!"); interaction.reply("Not a valid subject!");
return; return;
} }
@ -149,8 +157,8 @@ async function otherCommands(message) {
.then((res) => { .then((res) => {
data = res.data.question; data = res.data.question;
const messageFilter = (m) => m.author.id === authorId; const messageFilter = (m) => m.author.id === authorId;
message.reply(data.tossup_question).then(() => { interaction.reply(data.tossup_question).then(() => {
message.channel interaction.channel
.awaitMessages(messageFilter, { .awaitMessages(messageFilter, {
max: 1, max: 1,
time: 30000, time: 30000,
@ -219,16 +227,10 @@ async function otherCommands(message) {
}); });
}) })
.catch(console.error); .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) { 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("you haven't requested any rounds!"); message.reply("You 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!"))

1519
bot/package-lock.json

File diff suppressed because it is too large

2
bot/package.json

@ -1,7 +1,7 @@
{ {
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"discord.js": "^12.5.1", "discord.js": "github:discordjs/discord.js",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"eslint": "^7.21.0", "eslint": "^7.21.0",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",

Loading…
Cancel
Save