Browse Source

Clean up code

pull/22/head
Abheek Dhawan 3 years ago
parent
commit
fb4e0afc4e
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 153
      index.js
  2. 38
      migrateScores.js
  3. 0
      models/generateRound.js
  4. 0
      models/userScore.js

153
index.js

@ -3,20 +3,20 @@
require('dotenv').config(); require('dotenv').config();
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: ["GUILDS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", "DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS"/*, "GUILD_MEMBERS", "GUILD_PRESENCES"*/], intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", "DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS"/*, "GUILD_MEMBERS", "GUILD_PRESENCES"*/],
partials: ["MESSAGE", "CHANNEL", "REACTION"], partials: ["MESSAGE", "CHANNEL", "REACTION"],
}); });
const fetch = require("node-fetch");
const axios = require("axios"); const axios = require("axios");
const userScore = require("./mongooseModels/mongooseUserScoreModel.js");
const generatedRound = require("./mongooseModels/mongooseGeneratedRoundModel.js");
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const gitlog = require("gitlog").default; const gitlog = require("gitlog").default;
const config = require("./config.json");
const decode = require('html-entities').decode; const decode = require('html-entities').decode;
const userScore = require("./models/userScore");
const generatedRound = require("./models/generateRound");
const config = require("./config.json");
const helpMessage = "AwesomeSciBo has migrated to using slash commands! You can take a look at the different commands by typing `/` and clicking on the AwesomeSciBo icon." const helpMessage = "AwesomeSciBo has migrated to using slash commands! You can take a look at the different commands by typing `/` and clicking on the AwesomeSciBo icon."
const slashCommands = require('./slashCommands.json') const slashCommands = require('./slashCommands.json')
@ -62,7 +62,7 @@ client.on("guildCreate", (guild) => {
} }
}); });
client.on("guildDelete", guild => { client.on("guildDelete", () => {
const topggAuthHeader = { const topggAuthHeader = {
headers: { headers: {
'Authorization': config.topggauth 'Authorization': config.topggauth
@ -204,7 +204,7 @@ async function training(subject, interaction) {
.setDescription(`It seems your answer was incorrect. Please react with <:override:842778128966615060> to override your answer if you think you got it right.`) .setDescription(`It seems your answer was incorrect. Please react with <:override:842778128966615060> to override your answer if you think you got it right.`)
.setColor("#ffffff") .setColor("#ffffff")
.setTimestamp(); .setTimestamp();
const overrideMsg = answerMsg.channel.send({ answerMsg.channel.send({
embeds: [overrideEmbed] embeds: [overrideEmbed]
}) })
.then(overrideMsg => { .then(overrideMsg => {
@ -220,7 +220,7 @@ async function training(subject, interaction) {
filter, filter,
max: 1 max: 1
}) })
.then((userReaction) => { .then(() => {
updateScore(true, score, authorId).then((msgToReply) => updateScore(true, score, authorId).then((msgToReply) =>
answerMsg.reply(msgToReply) answerMsg.reply(msgToReply)
); );
@ -237,71 +237,6 @@ function sendHelpMessage(interaction) {
interaction.reply({ embeds: [helpEmbed] }); interaction.reply({ embeds: [helpEmbed] });
} }
async function startScoring(message) {
let scoreA = 0;
let scoreB = 0;
await message.channel
.send(`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`)
.then((scoreboard) => {
const filter = (m) => m.content.includes("do be");
const collector = message.channel.createMessageCollector(filter, {
time: 1500000,
});
collector.on("collect", (m) => {
if (m.content.toLowerCase() === "/scoring a 4") {
// A team gets toss-up
m.delete({ timeout: 1000 }).catch(console.error);
scoreA += 4;
scoreboard.channel.send(
`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`
);
} else if (m.content.toLowerCase() === "/scoring a 10") {
// A team gets bonus
m.delete({ timeout: 1000 }).catch(console.error);
scoreA += 10;
scoreboard.channel.send(
`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`
);
} else if (m.content.toLowerCase() === "/scoring b 4") {
// B team gets toss up
m.delete({ timeout: 1000 }).catch(console.error);
scoreB += 4;
scoreboard.channel.send(
`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`
);
} else if (m.content.toLowerCase() === "/scoring b 10") {
// B team gets bonus
m.delete({ timeout: 1000 }).catch(console.error);
scoreB += 10;
scoreboard.channel.send(
`Here's the score:\nTeam A: ${scoreA}\nTeam B: ${scoreB}`
);
} else if (m.content === "/scoring stop") {
m.delete({ timeout: 1000 }).catch(console.error);
scoreboard.delete({ timeout: 1000 });
m.channel.send(
`**FINAL SCORE:**\nTeam A: ${scoreA}\nTeam B: ${scoreB}`
);
collector.stop();
}
});
});
}
function dontWorryBeHappy(message) {
message.channel.send(
new Discord.MessageEmbed()
.setTitle(`Don't Worry Be Happy!`)
.setImage("https://media.giphy.com/media/7OKC8ZpTT0PVm/giphy.gif")
.setURL("https://youtu.be/d-diB65scQU")
.setColor("#ffffff")
);
}
function showServerNumber(message) {
message.channel.send(client.guilds.cache.size);
}
function showLeaderboard(interaction) { function showLeaderboard(interaction) {
let messageContent = ""; let messageContent = "";
userScore userScore
@ -379,44 +314,44 @@ async function rounds(action, interaction) {
if (action === "generate") { if (action === "generate") {
let i; let i;
let finalizedHTML = '<html><head><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> </head><body style="width: 70%; margin-left: auto; margin-right: auto;"><h2 style="text-align: center; text-decoration: underline overline; padding: 7px;">ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API</h2>'; let finalizedHTML = '<html><head><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> </head><body style="width: 70%; margin-left: auto; margin-right: auto;"><h2 style="text-align: center; text-decoration: underline overline; padding: 7px;">ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API</h2>';
let tossup_question; let tossup_question;
let question_category; let question_category;
let tossup_format; let tossup_format;
let tossup_answer; let tossup_answer;
let bonus_question; let bonus_question;
let bonus_format; let bonus_format;
let bonus_answer; let bonus_answer;
let htmlContent = ""; let htmlContent = "";
await axios.post("https://scibowldb.com/api/questions", { categories: ["BIOLOGY", "PHYSICS", "CHEMISTRY", "EARTH AND SPACE", "ASTRONOMY", "MATH"] }) await axios.post("https://scibowldb.com/api/questions", { categories: ["BIOLOGY", "PHYSICS", "CHEMISTRY", "EARTH AND SPACE", "ASTRONOMY", "MATH"] })
.then((response) => { .then((response) => {
for (i = 1; i < 26; i++) { for (i = 1; i < 26; i++) {
data = response.data.questions[Math.floor(Math.random() * response.data.questions.length)]; data = response.data.questions[Math.floor(Math.random() * response.data.questions.length)];
tossup_question = data.tossup_question; tossup_question = data.tossup_question;
tossup_answer = data.tossup_answer; tossup_answer = data.tossup_answer;
question_category = data.category; question_category = data.category;
tossup_format = data.tossup_format; tossup_format = data.tossup_format;
bonus_question = data.bonus_question; bonus_question = data.bonus_question;
bonus_answer = data.bonus_answer; bonus_answer = data.bonus_answer;
bonus_format = data.bonus_format; bonus_format = data.bonus_format;
htmlContent = `<br><br><h3 style="text-align: center;"><strong>TOSS-UP</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br>"; htmlContent = `<br><br><h3 style="text-align: center;"><strong>TOSS-UP</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${tossup_format}</em>` + " " + tossup_question + "<br><br>" + "<strong>ANSWER:</strong> " + tossup_answer + "<br>";
htmlContent += `<br><br><h3 style="text-align: center;"><strong>BONUS</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br><hr><br>"; htmlContent += `<br><br><h3 style="text-align: center;"><strong>BONUS</strong></h3>\n<br>` + `${i}) <strong>${question_category}</strong>` + " " + `<em>${bonus_format}</em>` + " " + bonus_question + "<br><br>" + "<strong>ANSWER:</strong> " + bonus_answer + "<br><br><hr><br>";
htmlContent = htmlContent.replace(/\n/g, "<br>"); htmlContent = htmlContent.replace(/\n/g, "<br>");
finalizedHTML += htmlContent; finalizedHTML += htmlContent;
}
newGeneratedRound = new generatedRound({
htmlContent: finalizedHTML,
requestedBy: interaction.user.id,
authorTag: interaction.user.tag,
timestamp: new Date().toISOString(),
});
newGeneratedRound.save((err, round) => {
if (err) {
console.log(err);
return;
} }
newGeneratedRound = new generatedRound({ interaction.reply(`Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, { ephemeral: true });
htmlContent: finalizedHTML,
requestedBy: interaction.user.id,
authorTag: interaction.user.tag,
timestamp: new Date().toISOString(),
});
newGeneratedRound.save((err, round) => {
if (err) {
console.log(err);
return;
}
interaction.reply(`Here's your round: https://api.adawesome.tech/round/${round._id.toString()}`, { ephemeral: true });
});
}); });
});
} else if (action === "list"){ } else if (action === "list"){
let rounds = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 }); let rounds = await generatedRound.find({ requestedBy: interaction.user.id }).sort({ timestamp: -1 });
let finalMessage = ""; let finalMessage = "";

38
migrateScores.js

@ -1,38 +0,0 @@
const fs = require("fs");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const dirName = "userScore";
const fileNames = fs.readdirSync(dirName);
const userScoreSchema = new Schema({
authorID: {
type: String,
required: true,
},
score: {
type: Number,
required: true,
},
});
const userScore = mongoose.model("UserScore", userScoreSchema);
mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => {
fileNames.forEach(file => {
fs.readFile(dirName + "/" + file, 'utf-8', (err, content) => {
const migrateScore = new userScore({
authorID: file,
score: content,
});
migrateScore.save();
});
});
})
.catch((err) => console.log(err));

0
mongooseModels/mongooseGeneratedRoundModel.js → models/generateRound.js

0
mongooseModels/mongooseUserScoreModel.js → models/userScore.js

Loading…
Cancel
Save