From 6ae88b1061396c3ece35799dea981d6e34994ae5 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 21:59:01 -0500 Subject: [PATCH 01/26] Add score migrate script to prepare for merge to master --- .gitignore | 3 ++- bot/awesomescibo.mjs | 7 ++++-- bot/migrateScores.js | 38 ++++++++++++++++++++++++++++++++ bot/package-lock.json | 6 ++--- bot/userScore/745063586422063214 | 1 - 5 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 bot/migrateScores.js delete mode 100644 bot/userScore/745063586422063214 diff --git a/.gitignore b/.gitignore index c607f42..6ec2bb9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ round.pdf bot/README.md .eslintrc .json -.env \ No newline at end of file +.env +userScore/* diff --git a/bot/awesomescibo.mjs b/bot/awesomescibo.mjs index b58996c..6da91f5 100755 --- a/bot/awesomescibo.mjs +++ b/bot/awesomescibo.mjs @@ -16,13 +16,15 @@ const helpMessage = "`do be helping`: display this help message\n`do be roundgen`: send a pdf round to the channel\n`do be roundgen dm`: dm a pdf round to you\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", () => { + // Connect to MongoDB using mongoose mongoose .connect(process.env.MONGO_URI, { useUnifiedTopology: true, useNewUrlParser: true, }) .then(() => { - console.log(client.user.username); + // Log client tag and set status + console.log(`Logged in as: ${client.user.username}!`); client.user.setActivity( 'for "do be helping" | Add me to your own server: adat.link/awscibo', { type: "WATCHING" } @@ -35,6 +37,7 @@ client.on("guildCreate", (guild) => { guild.channels.cache .find( (channel) => + // Find channel by name channel.name === process.env.WELCOME_CHANNEL && channel.type === "text" ) .send("'Sup, I'm the AwesomeSciBo bot!") @@ -496,7 +499,7 @@ function showLeaderboard(message) { function aboutMessage(message) { message.channel.send( new Discord.MessageEmbed().setTitle("Contributors: ").setDescription(` - <@745063586422063214> + <@745063586422063214> <@650525101048987649> `) // Add more contributors here, first one is Abheek, second one is Tejas ); diff --git a/bot/migrateScores.js b/bot/migrateScores.js new file mode 100644 index 0000000..2e208bb --- /dev/null +++ b/bot/migrateScores.js @@ -0,0 +1,38 @@ +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)); diff --git a/bot/package-lock.json b/bot/package-lock.json index 8207f1a..df633ab 100644 --- a/bot/package-lock.json +++ b/bot/package-lock.json @@ -391,7 +391,6 @@ "node": ">=0.10" } }, - "node_modules/discord.js": { "version": "12.5.1", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz", @@ -944,8 +943,7 @@ "bson": "^1.1.4", "denque": "^1.4.1", "optional-require": "^1.0.2", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" + "safe-buffer": "^5.1.2" }, "engines": { "node": ">=4" @@ -2578,7 +2576,7 @@ "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" } } - }, + }, "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", diff --git a/bot/userScore/745063586422063214 b/bot/userScore/745063586422063214 deleted file mode 100644 index 301160a..0000000 --- a/bot/userScore/745063586422063214 +++ /dev/null @@ -1 +0,0 @@ -8 \ No newline at end of file From ed580e6eddb72a82a9bbb079dc744d1cdf8dabb5 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 22:06:20 -0500 Subject: [PATCH 02/26] Attempt to fix hang on CI on GitHub --- bot/awesomescibo.mjs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/bot/awesomescibo.mjs b/bot/awesomescibo.mjs index 6da91f5..5ec615d 100755 --- a/bot/awesomescibo.mjs +++ b/bot/awesomescibo.mjs @@ -17,20 +17,22 @@ const helpMessage = client.once("ready", () => { // Connect to MongoDB using mongoose - mongoose - .connect(process.env.MONGO_URI, { - useUnifiedTopology: true, - useNewUrlParser: true, - }) - .then(() => { - // Log client tag and set status - console.log(`Logged in as: ${client.user.username}!`); - client.user.setActivity( - 'for "do be helping" | Add me to your own server: adat.link/awscibo', - { type: "WATCHING" } - ); - }) - .catch((err) => console.log(err)); + if (!process.env.CI) { + mongoose + .connect(process.env.MONGO_URI, { + useUnifiedTopology: true, + useNewUrlParser: true, + }) + .then(() => { + // Log client tag and set status + console.log(`Logged in as: ${client.user.username}!`); + client.user.setActivity( + 'for "do be helping" | Add me to your own server: adat.link/awscibo', + { type: "WATCHING" } + ); + }) + .catch((err) => console.log(err)); + } }); client.on("guildCreate", (guild) => { From 1688303c8fff71069fccf53d28a6113e123e7aeb Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 22:18:13 -0500 Subject: [PATCH 03/26] Use config file for URLs --- .gitignore | 1 + bot/awesomescibo.mjs | 5 +++-- bot/config.default.json | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 bot/config.default.json diff --git a/.gitignore b/.gitignore index 6ec2bb9..7c3dd2f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ bot/README.md .json .env userScore/* +config.json diff --git a/bot/awesomescibo.mjs b/bot/awesomescibo.mjs index 5ec615d..8b310f1 100755 --- a/bot/awesomescibo.mjs +++ b/bot/awesomescibo.mjs @@ -11,6 +11,7 @@ import axios from "axios"; import userScore from "./mongooseModels/mongooseUserScoreModel.js"; import {} from "dotenv/config.js"; import mongoose from "mongoose"; +import config from ('./config.json') const helpMessage = "`do be helping`: display this help message\n`do be roundgen`: send a pdf round to the channel\n`do be roundgen dm`: dm a pdf round to you\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!)"; @@ -47,7 +48,7 @@ client.on("guildCreate", (guild) => { }); function getSubjectUrl(subject) { - return `https://moose.lcsrc.org/subjects/${subject}.json`; + return `${config.subjectURL}${subject}.json`; } async function updateScore(isCorrect, score, authorId) { @@ -365,7 +366,7 @@ async function generateRound(message, isDM) { generatingMsg.delete({ timeout: 100 }).catch(console.error); } execSync( - "curl --request POST --url https://localhost:3136/convert/html --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf", + `curl --request POST --url ${config.gotenbergURL} --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf`, { encoding: "utf-8" } ); if (isDM) { diff --git a/bot/config.default.json b/bot/config.default.json new file mode 100644 index 0000000..7bfe57b --- /dev/null +++ b/bot/config.default.json @@ -0,0 +1,4 @@ +{ + "subjectURL": "https://moose.lcsrc.org/subjects/", + "gotenbergURL": "https://localhost:3136/convert/html" +} From a3c801fd1af15d5372ebe9efad422fdcecc9efa3 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 04:07:24 +0000 Subject: [PATCH 04/26] Convert to CommonJS to easier reference JSON files --- .gitignore | 2 +- bot/{awesomescibo.mjs => awesomescibo.js} | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) rename bot/{awesomescibo.mjs => awesomescibo.js} (98%) diff --git a/.gitignore b/.gitignore index 7c3dd2f..fb11a82 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,5 @@ bot/README.md .eslintrc .json .env -userScore/* +userScore config.json diff --git a/bot/awesomescibo.mjs b/bot/awesomescibo.js similarity index 98% rename from bot/awesomescibo.mjs rename to bot/awesomescibo.js index 8b310f1..32d16d0 100755 --- a/bot/awesomescibo.mjs +++ b/bot/awesomescibo.js @@ -1,17 +1,16 @@ #!/usr/bin/env node -import * as Discord from "discord.js"; -import { execSync } from "child_process"; +const Discord = require("discord.js"); +const execSync = require("child_process").execSync; const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"], }); -import fetch from "node-fetch"; -import * as fs from "fs"; -import axios from "axios"; -import userScore from "./mongooseModels/mongooseUserScoreModel.js"; -import {} from "dotenv/config.js"; -import mongoose from "mongoose"; -import config from ('./config.json') +const fetch = require("node-fetch"); +const fs = require("fs"); +const axios = require("axios"); +const userScore = require("./mongooseModels/mongooseUserScoreModel.js"); +const mongoose = require("mongoose"); +const config = require("./config.json"); const helpMessage = "`do be helping`: display this help message\n`do be roundgen`: send a pdf round to the channel\n`do be roundgen dm`: dm a pdf round to you\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!)"; @@ -513,7 +512,7 @@ client.on("message", async (message) => { return; } - const formattedMessage = message.content.toLowerCase().replace(" ", ""); + const formattedMessage = message.content.toLowerCase().replace(/ /g, ""); if (formattedMessage.startsWith("dobe")) { // Bot prefix is "do be" switch (formattedMessage) { From 9854fb2f3774e954c5448267db1a207b939455de Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:09:22 -0500 Subject: [PATCH 05/26] Update package.json to reflect new file names --- bot/mongooseModels/mongooseGeneratedRoundModel.js | 14 ++++++++++++++ bot/package.json | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 bot/mongooseModels/mongooseGeneratedRoundModel.js diff --git a/bot/mongooseModels/mongooseGeneratedRoundModel.js b/bot/mongooseModels/mongooseGeneratedRoundModel.js new file mode 100644 index 0000000..b0e9515 --- /dev/null +++ b/bot/mongooseModels/mongooseGeneratedRoundModel.js @@ -0,0 +1,14 @@ +const mongoose = require("mongoose"); + +const generatedRoundSchema = new mongoose.Schema({ + htmlContent: { + type: String, + required: true, + }, + requestedBy: { + type: String, + required: true, + }, +}); + +module.exports = mongoose.model("GeneratedRounds", generatedRoundSchema); diff --git a/bot/package.json b/bot/package.json index ac6dc2d..c1b06cb 100644 --- a/bot/package.json +++ b/bot/package.json @@ -10,13 +10,13 @@ }, "name": "awscibo", "version": "0.4.1", - "main": "awesomescibo.mjs", + "main": "awesomescibo.js", "bin": { - "awscibo": "./awesomescibo.mjs" + "awscibo": "./awesomescibo.js" }, "scripts": { - "test": "node awesomescibo.mjs randomtoken", - "start": "nodemon awesomescibo.mjs" + "test": "node awesomescibo.js randomtoken", + "start": "nodemon awesomescibo.js" }, "keywords": [ "discord", From ae5edba52329d648b45c49889cea4e462c617e71 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:17:12 -0500 Subject: [PATCH 06/26] Test saving HTML content to database --- bot/awesomescibo.js | 47 ++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 32d16d0..495c2c3 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -9,11 +9,13 @@ const fetch = require("node-fetch"); const fs = require("fs"); const axios = require("axios"); const userScore = require("./mongooseModels/mongooseUserScoreModel.js"); +const generatedRound = require("./mongooseModels/mongooseGeneratedRoundModel.js"); const mongoose = require("mongoose"); -const config = require("./config.json"); +let config = {}; +process.env.CI ? config = require("./config.default.json") : config = require("./config.json") const helpMessage = - "`do be helping`: display this help message\n`do be roundgen`: send a pdf round to the channel\n`do be roundgen dm`: dm a pdf round to you\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", () => { // Connect to MongoDB using mongoose @@ -304,7 +306,7 @@ function sendHelpMessage(message) { ); } -async function generateRound(message, isDM) { +async function generateRound(message) { fs.writeFile("index.html", "

Here's your round!

", (error) => { if (error) { console.log(error); @@ -354,40 +356,25 @@ async function generateRound(message, isDM) { bonus_answer + "

"; htmlContent = htmlContent.replace(/\n/g, "
"); - fs.appendFile("index.html", htmlContent, (error) => { - if (error) { - console.log(error); - } + newGeneratedRound = new generatedRound({ + htmlContent: htmlContent, + requestedBy: message.author.tag, }); - }); + newGeneratedRound.save((err) => + err + ? console.log("Error creating saving generated round content") + : console.log("Sucessfully created new entry for the generated round") + ); } if (generatingMsg) { generatingMsg.delete({ timeout: 100 }).catch(console.error); } execSync( - `curl --request POST --url ${config.gotenbergURL} --header 'Content-Type: multipart/form-data' --form files=@index.html -o round.pdf`, - { encoding: "utf-8" } + message.channel.send( + new Discord.MessageEmbed() + .setTitle("Here's your round!") + .attachFiles("round.pdf") ); - if (isDM) { - client.users.cache - .get(message.author.id) - .send( - new Discord.MessageEmbed() - .setTitle("Here's your round!") - .attachFiles("round.pdf") - ) - .catch(() => - message.reply( - "Unable to DM you! Make sure DMs from server members are allowed." - ) - ); - } else { - message.channel.send( - new Discord.MessageEmbed() - .setTitle("Here's your round!") - .attachFiles("round.pdf") - ); - } } async function startScoring(message) { From 3244ba2eafd3bd3288321183286481c7bccbb1c0 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:18:01 -0500 Subject: [PATCH 07/26] Fix syntax error Missing a ) after } for a .then() block --- bot/awesomescibo.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 495c2c3..2f487d1 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -365,15 +365,13 @@ async function generateRound(message) { ? console.log("Error creating saving generated round content") : console.log("Sucessfully created new entry for the generated round") ); - } + }); if (generatingMsg) { generatingMsg.delete({ timeout: 100 }).catch(console.error); } - execSync( message.channel.send( new Discord.MessageEmbed() - .setTitle("Here's your round!") - .attachFiles("round.pdf") + .setTitle("Done?"); ); } From 53d265c36ad5ec57542a7111c534d60e2705201f Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:19:56 -0500 Subject: [PATCH 08/26] Set placeholder for message --- bot/awesomescibo.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 2f487d1..792a554 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -369,10 +369,7 @@ async function generateRound(message) { if (generatingMsg) { generatingMsg.delete({ timeout: 100 }).catch(console.error); } - message.channel.send( - new Discord.MessageEmbed() - .setTitle("Done?"); - ); + message.channel.send("Check database"); } async function startScoring(message) { From 985b94789daabbcad373f36241626d69fc784f00 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:22:42 -0500 Subject: [PATCH 09/26] Fix formatting --- bot/awesomescibo.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 792a554..e6fecba 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -313,7 +313,6 @@ async function generateRound(message) { } }); let i; - let generatingMsg = await message.channel.send("Generating..."); for (i = 1; i < 26; i++) { let tossup_question; let question_category; @@ -356,19 +355,17 @@ async function generateRound(message) { bonus_answer + "

"; htmlContent = htmlContent.replace(/\n/g, "
"); - newGeneratedRound = new generatedRound({ - htmlContent: htmlContent, - requestedBy: message.author.tag, - }); - newGeneratedRound.save((err) => - err - ? console.log("Error creating saving generated round content") - : console.log("Sucessfully created new entry for the generated round") - ); - }); - if (generatingMsg) { - generatingMsg.delete({ timeout: 100 }).catch(console.error); + }); } + newGeneratedRound = new generatedRound({ + htmlContent: htmlContent, + requestedBy: message.author.tag, + }); + newGeneratedRound.save((err) => + err + ? console.log("Error creating saving generated round content") + : console.log("Sucessfully created new entry for the generated round") + ); message.channel.send("Check database"); } From 26ede2a8ffb40337d3219851def00582626621e3 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:24:51 -0500 Subject: [PATCH 10/26] Attempt to fix scope issue --- bot/awesomescibo.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index e6fecba..77c5fcb 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -355,17 +355,19 @@ async function generateRound(message) { bonus_answer + "

"; htmlContent = htmlContent.replace(/\n/g, "
"); + if (i === 25) { + newGeneratedRound = new generatedRound({ + htmlContent: htmlContent, + requestedBy: message.author.tag, + }); + newGeneratedRound.save((err) => + err + ? console.log("Error creating saving generated round content") + : console.log("Sucessfully created new entry for the generated round") + ); + } }); } - newGeneratedRound = new generatedRound({ - htmlContent: htmlContent, - requestedBy: message.author.tag, - }); - newGeneratedRound.save((err) => - err - ? console.log("Error creating saving generated round content") - : console.log("Sucessfully created new entry for the generated round") - ); message.channel.send("Check database"); } From 7eaee053193125876bd70326003e191025533efa Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:27:20 -0500 Subject: [PATCH 11/26] Add HTML content for each question to separate variable for full round --- bot/awesomescibo.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 77c5fcb..9d30e8d 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -321,6 +321,7 @@ async function generateRound(message) { let bonus_question; let bonus_format; let bonus_answer; + let finalizedHTML = ""; let htmlContent = ""; await fetch("https://scibowldb.com/api/questions/random") .then((response) => response.json()) @@ -355,10 +356,11 @@ async function generateRound(message) { bonus_answer + "

"; htmlContent = htmlContent.replace(/\n/g, "
"); + finalizedHTML += htmlContent; if (i === 25) { newGeneratedRound = new generatedRound({ - htmlContent: htmlContent, - requestedBy: message.author.tag, + htmlContent: finalizedHTML, + requestedBy: message.author.id, }); newGeneratedRound.save((err) => err From 61568b8c5c3cb75fa001a658459201dfd8e2c752 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sat, 1 May 2021 23:28:38 -0500 Subject: [PATCH 12/26] Fix variable declaration location --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 9d30e8d..f02522a 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -313,6 +313,7 @@ async function generateRound(message) { } }); let i; + let finalizedHTML = ""; for (i = 1; i < 26; i++) { let tossup_question; let question_category; @@ -321,7 +322,6 @@ async function generateRound(message) { let bonus_question; let bonus_format; let bonus_answer; - let finalizedHTML = ""; let htmlContent = ""; await fetch("https://scibowldb.com/api/questions/random") .then((response) => response.json()) From 395caf5ed93fe9f9bea27ac498e2dca010f223b1 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:09:21 -0500 Subject: [PATCH 13/26] Improve formatting of HTML rounds --- bot/awesomescibo.js | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index f02522a..7fbeaaa 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -313,7 +313,7 @@ async function generateRound(message) { } }); let i; - let finalizedHTML = ""; + let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; for (i = 1; i < 26; i++) { let tossup_question; let question_category; @@ -333,28 +333,8 @@ async function generateRound(message) { bonus_question = data.question.bonus_question; bonus_answer = data.question.bonus_answer; bonus_format = data.question.bonus_format; - htmlContent = - `

${i}. Tossup\n

` + - `${question_category}` + - " " + - `${tossup_format}` + - " " + - tossup_question + - "

" + - "ANSWER: " + - tossup_answer + - "

"; - htmlContent += - "

Bonus\n

" + - `${question_category}` + - " " + - `${bonus_format}` + - " " + - bonus_question + - "

" + - "ANSWER: " + - bonus_answer + - "

"; + htmlContent = `

TOSS-UP

\n
` + `${i}) ${question_category}` + " " + `${tossup_format}` + " " + tossup_question + "

" + "ANSWER: " + tossup_answer + "
"; + htmlContent += `

BONUS

\n
` + `${i}) ${question_category}` + " " + `${bonus_format}` + " " + bonus_question + "

" + "ANSWER: " + bonus_answer + "



"; htmlContent = htmlContent.replace(/\n/g, "
"); finalizedHTML += htmlContent; if (i === 25) { From 2b26e5598aef58dcf35a2e1fed95c6230c5d2d98 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:15:07 -0500 Subject: [PATCH 14/26] Change HTML width --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 7fbeaaa..48f0cdf 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -313,7 +313,7 @@ async function generateRound(message) { } }); let i; - let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; + let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; for (i = 1; i < 26; i++) { let tossup_question; let question_category; From 8c427866a19f974ceb0444277868e6e649e6b57f Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:16:31 -0500 Subject: [PATCH 15/26] Attempt to fix margins --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 48f0cdf..f7b7970 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -313,7 +313,7 @@ async function generateRound(message) { } }); let i; - let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; + let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; for (i = 1; i < 26; i++) { let tossup_question; let question_category; From 3b7c0a4d1237bc53b859eaac7ad9474bb01e351f Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:19:20 -0500 Subject: [PATCH 16/26] Use h3 tags instead of p tags for question type declaration --- bot/awesomescibo.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index f7b7970..305fc9a 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -333,8 +333,8 @@ async function generateRound(message) { bonus_question = data.question.bonus_question; bonus_answer = data.question.bonus_answer; bonus_format = data.question.bonus_format; - htmlContent = `

TOSS-UP

\n
` + `${i}) ${question_category}` + " " + `${tossup_format}` + " " + tossup_question + "

" + "ANSWER: " + tossup_answer + "
"; - htmlContent += `

BONUS

\n
` + `${i}) ${question_category}` + " " + `${bonus_format}` + " " + bonus_question + "

" + "ANSWER: " + bonus_answer + "



"; + htmlContent = `

TOSS-UP

\n
` + `${i}) ${question_category}` + " " + `${tossup_format}` + " " + tossup_question + "

" + "ANSWER: " + tossup_answer + "
"; + htmlContent += `

BONUS

\n
` + `${i}) ${question_category}` + " " + `${bonus_format}` + " " + bonus_question + "

" + "ANSWER: " + bonus_answer + "



"; htmlContent = htmlContent.replace(/\n/g, "
"); finalizedHTML += htmlContent; if (i === 25) { From 76ce2ad24902fa6ee8fad008527b02c35318a459 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:20:58 -0500 Subject: [PATCH 17/26] Use h2 tags for title --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 305fc9a..d0f1937 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -313,7 +313,7 @@ async function generateRound(message) { } }); let i; - let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; + let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; for (i = 1; i < 26; i++) { let tossup_question; let question_category; From e656a8fff947fc078b71a003b713cc12e831a5dd Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:27:37 -0500 Subject: [PATCH 18/26] Return round URL on generation --- bot/awesomescibo.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index d0f1937..b6def1a 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -307,11 +307,7 @@ function sendHelpMessage(message) { } async function generateRound(message) { - fs.writeFile("index.html", "

Here's your round!

", (error) => { - if (error) { - console.log(error); - } - }); + const generatingMessage = message.channel.send("Generating..."); let i; let finalizedHTML = '

ROUND GENERATED BY AWESOMESCIBO USING THE SCIBOWLDB API

'; for (i = 1; i < 26; i++) { @@ -342,15 +338,16 @@ async function generateRound(message) { htmlContent: finalizedHTML, requestedBy: message.author.id, }); - newGeneratedRound.save((err) => + newGeneratedRound.save((err, round) => err ? console.log("Error creating saving generated round content") : console.log("Sucessfully created new entry for the generated round") + + message.channel.fetch(generatingMessage.id).edit(`https://api.adawesome.tech/round/${round._id.toString()}`); ); } }); } - message.channel.send("Check database"); } async function startScoring(message) { From 2b6145863ff3e12abe7b91992aed8594446da4fd Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:29:05 -0500 Subject: [PATCH 19/26] Improve error logging --- bot/awesomescibo.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index b6def1a..ba60b67 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -339,10 +339,10 @@ async function generateRound(message) { requestedBy: message.author.id, }); newGeneratedRound.save((err, round) => - err - ? console.log("Error creating saving generated round content") - : console.log("Sucessfully created new entry for the generated round") - + if (err) { + console.log(err); + return; + } message.channel.fetch(generatingMessage.id).edit(`https://api.adawesome.tech/round/${round._id.toString()}`); ); } From 8c74943cf2bfe47e8c72c332903c94301e54c9dc Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:31:31 -0500 Subject: [PATCH 20/26] Attempt to fix formatting --- bot/awesomescibo.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index ba60b67..5a54576 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -338,15 +338,15 @@ async function generateRound(message) { htmlContent: finalizedHTML, requestedBy: message.author.id, }); - newGeneratedRound.save((err, round) => + newGeneratedRound.save((err, round) => { if (err) { console.log(err); return; } message.channel.fetch(generatingMessage.id).edit(`https://api.adawesome.tech/round/${round._id.toString()}`); - ); - } }); + } + }); } } From 37471d5068fedf4193d39bfe3b4d28a3480c74fa Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:33:13 -0500 Subject: [PATCH 21/26] Fix message fetching and editing --- bot/awesomescibo.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 5a54576..fb4a44e 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -343,7 +343,10 @@ async function generateRound(message) { console.log(err); return; } - message.channel.fetch(generatingMessage.id).edit(`https://api.adawesome.tech/round/${round._id.toString()}`); + message.channel.messages.fetch(generatingMessage.id) + .then(message => { + message.edit(`https://api.adawesome.tech/round/${round._id.toString()}`); + }); }); } }); From 55218cf545adcef9fdd144ed143b320fca8173be Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:34:08 -0500 Subject: [PATCH 22/26] Fix message fetching and editing --- bot/awesomescibo.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index fb4a44e..3f9a4e4 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -345,7 +345,8 @@ async function generateRound(message) { } message.channel.messages.fetch(generatingMessage.id) .then(message => { - message.edit(`https://api.adawesome.tech/round/${round._id.toString()}`); + const msg = msg.first(); + msg.edit(`https://api.adawesome.tech/round/${round._id.toString()}`); }); }); } From b8af6d299a2601ca9f623031e74125cd663f8ce1 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:34:51 -0500 Subject: [PATCH 23/26] Fix typo between message and msg --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 3f9a4e4..d3cf07e 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -345,7 +345,7 @@ async function generateRound(message) { } message.channel.messages.fetch(generatingMessage.id) .then(message => { - const msg = msg.first(); + const msg = message.first(); msg.edit(`https://api.adawesome.tech/round/${round._id.toString()}`); }); }); From fedd619b4d58d9cd42b0f4a5130f3e6fb5244c06 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:36:16 -0500 Subject: [PATCH 24/26] Mention user on round generation completion --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index d3cf07e..18a4016 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -346,7 +346,7 @@ async function generateRound(message) { message.channel.messages.fetch(generatingMessage.id) .then(message => { const msg = message.first(); - msg.edit(`https://api.adawesome.tech/round/${round._id.toString()}`); + msg.edit(`${generatingMessage.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`); }); }); } From 1b3903447cf0bbed75c53b9dd910bbae5bc00a1f Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:37:12 -0500 Subject: [PATCH 25/26] Fix message author object --- bot/awesomescibo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 18a4016..4323cac 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -346,7 +346,7 @@ async function generateRound(message) { message.channel.messages.fetch(generatingMessage.id) .then(message => { const msg = message.first(); - msg.edit(`${generatingMessage.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`); + msg.edit(`${msg.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`); }); }); } From 4290d4536f53a4cf426873d888f7dc5f6168b24c Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Sun, 2 May 2021 00:38:10 -0500 Subject: [PATCH 26/26] Fix message author object --- bot/awesomescibo.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/awesomescibo.js b/bot/awesomescibo.js index 4323cac..5f30412 100755 --- a/bot/awesomescibo.js +++ b/bot/awesomescibo.js @@ -344,9 +344,9 @@ async function generateRound(message) { return; } message.channel.messages.fetch(generatingMessage.id) - .then(message => { - const msg = message.first(); - msg.edit(`${msg.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`); + .then(generatingMessage => { + const msg = generatingMessage.first(); + msg.edit(`${message.author}, here's your round: https://api.adawesome.tech/round/${round._id.toString()}`); }); }); }