From 63142ba3c1332b5c14abf08cacaad61af11f06e5 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 21:48:06 -0400 Subject: [PATCH 01/12] Add dotenv (will remove after I'm done) --- bot.js | 1 + 1 file changed, 1 insertion(+) diff --git a/bot.js b/bot.js index 4e0a6a8..5d06a90 100644 --- a/bot.js +++ b/bot.js @@ -19,6 +19,7 @@ ytdl = require('ytdl-core'); const botPrefix = "!"; const version = "2.3.0"; // Version PROD_GUILD = "256222023993393152"; +require('dotenv').config() numRequests = 0; schedule.scheduleJob('*/30 * * * * *', () => numRequests = 0); From 22edebd5349e3b3b9412f2056dca68a8ecd485fd Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:29:34 -0400 Subject: [PATCH 02/12] Add Redis module + config --- package.json | 1 + redis.conf | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 redis.conf diff --git a/package.json b/package.json index 7458d63..ae455aa 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "homepage": "https://github.com/TheScootz/unity-machine#readme", "dependencies": { + "@redis/client": "^1.5.6", "bluebird": "^3.7.2", "discord.js": "^13.12.0", "dotenv": "^10.0.0", diff --git a/redis.conf b/redis.conf new file mode 100644 index 0000000..12481eb --- /dev/null +++ b/redis.conf @@ -0,0 +1,6 @@ +# Shamelessly stolen from the redis.io website +timeout 0 +tcp-keepalive 300 +# No persistence +save "" +appendonly no \ No newline at end of file From 5900c7951ae2bc54c67ed9affe9dbfccce224cd8 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:06:24 -0400 Subject: [PATCH 03/12] Implement fetching ooc for Redis in bot.js --- bot.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bot.js b/bot.js index 5d06a90..3d6dd18 100644 --- a/bot.js +++ b/bot.js @@ -1,6 +1,7 @@ Promise = require('bluebird'); childProcess = require('child_process'); +redis = require('@redis/client') Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); @@ -50,6 +51,13 @@ MongoClient.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true} //(async () => nonWAElectoralCitizens = (await dbo.collection("nonWAElectoralCitizens").findOne({"id": "nonWAElectoralCitizens"})).nonWAElectoralCitizens)(); // Array of all non-WA Electoral Citizens of TLA }); +// Initialise Redis +async () => { + const redisClient = redis.createClient(); + await redisClient.connect(); + redisClient.on('error', err => console.error(`Could not connect to Redis: ${err}`)) +} + // Initialise Google API youtube = google.youtube({ version: 'v3', @@ -470,10 +478,19 @@ client.once('ready', async () => { // Delete text-only messages in #out-of-context const oocChannel = TLAServer.channels.cache.find(channel => channel.name === "out-of-context"); // Channel for OOC posts let oocMessages = await getMessages(oocChannel); // Get all messages in #out-of-context + let oocMessages2 = oocMessages; // Filter out all mesages with one image oocMessages = oocMessages.filter(message => !(message.attachments.size === 1 && isImage(message.attachments.first().attachment))); oocMessages = oocMessages.filter(message => ! message.pinned); // Filter out all pinned messages oocMessages.forEach(message => message.delete()); // Delete all messages + + // Add all OOC images to cache + oocMessages2 = oocMessages2.filter(message => (message.attachments.size === 1 && isImage(message.attachments.first().attachment))); + oocMessages2.forEach(async (message) => { + // For each image in ooc, add its ID to a set and store the image as a hash + await redisClient.sAdd("messageIds", message.id) + await redisClient.hSet(`${message.id}`, "imageUrl", message.attachments[0].url) + }) console.log("Ready to take commands!"); }); From 9d7220720c7cdba30f87d8e43651d9015f3b703b Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:17:36 -0400 Subject: [PATCH 04/12] Implement cache on msgCreate + added the missing ; --- bot.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index 3d6dd18..5f6b913 100644 --- a/bot.js +++ b/bot.js @@ -6,6 +6,8 @@ Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); const {google} = require('googleapis'); +const { redis } = require('googleapis/build/src/apis/redis'); +const isImage = require('is-image'); he = require('he'); isImage = require('is-image'); moment = require('moment'); @@ -260,6 +262,12 @@ client.on('messageCreate', async msg => { return; } + // Adds ooc image to cache + if (msg.channelId === IDS.channels.ooc && msg.attachments.size === 1 && isImage(msg.attachments[0].attachment)) { + await redisClient.sAdd("messageIds", msg.id); + await redisClient.hSet(msg.id, "ImageUrl", msg.attachments[0].attachment.url); + } + // Received message starts with bot prefix if (msg.content.startsWith(botPrefix)) { const fullCommand = msg.content.substr(botPrefix.length); // Remove the leading bot prefix @@ -488,9 +496,9 @@ client.once('ready', async () => { oocMessages2 = oocMessages2.filter(message => (message.attachments.size === 1 && isImage(message.attachments.first().attachment))); oocMessages2.forEach(async (message) => { // For each image in ooc, add its ID to a set and store the image as a hash - await redisClient.sAdd("messageIds", message.id) - await redisClient.hSet(`${message.id}`, "imageUrl", message.attachments[0].url) - }) + await redisClient.sAdd("messageIds", message.id); + await redisClient.hSet(`${message.id}`, "imageUrl", message.attachments[0].attachment.url); + }); console.log("Ready to take commands!"); }); From 23548ac18628d9d8b45a083b861824b19b5e19de Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:35:09 -0400 Subject: [PATCH 05/12] Implement deleting from cache if msg gets deleted --- bot.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bot.js b/bot.js index 5f6b913..518fe36 100644 --- a/bot.js +++ b/bot.js @@ -295,6 +295,15 @@ client.on('messageCreate', async msg => { } }); +// Message has been deleted +client.on('messageDelete', async (msg) => { + // Checks if it is in cache + if (await redisClient.sIsMember("messageIds", msg.id) === 1) { + await redisClient.sRem("messageIds", msg.id) + await redisClient.hDel(msg.id, "imageUrl") + } +}) + // Client is connected to Discord client.once('ready', async () => { console.log("Connected as " + client.user.tag); // Confirm connection From 917485e8a9e55b4bccf9cd4cf6a2cfcf60aed9b0 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:40:37 -0400 Subject: [PATCH 06/12] Implement command + dumb bug fix --- bot.js | 2 +- commands/ooc.js | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bot.js b/bot.js index 518fe36..0bd158d 100644 --- a/bot.js +++ b/bot.js @@ -1,7 +1,7 @@ Promise = require('bluebird'); childProcess = require('child_process'); -redis = require('@redis/client') +const redis = require('@redis/client') Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); diff --git a/commands/ooc.js b/commands/ooc.js index 3c980a3..71f2133 100644 --- a/commands/ooc.js +++ b/commands/ooc.js @@ -8,11 +8,8 @@ module.exports = { \`!ooc\``, async execute(msg, args) { - const oocChannel = await TLAServer.channels.fetch(IDS.channels.ooc); // Channel for OOC posts - let oocMessages = await getMessages(oocChannel); // Get all messages in #out-of-context - oocMessages = oocMessages.filter(message => message.attachments.size === 1); // Only include oocMessages with one attachment - oocMessages = oocMessages.map(message => message.attachments.first().attachment); // Only include the url of a message's attachment - oocMessages = oocMessages.filter(messageAttachmentURL => isImage(messageAttachmentURL)); // Only include images - msg.channel.send({files: [getRandomObject(oocMessages)]}); // Send random image from message url array + messageId = await redisClient.sRandMember("messageIds") // Get random image from cache + imageUrl = await redisClient.hGet(messageId, "imageUrl") // Get that image's URL + msg.channel.send({files: [imageUrl]}); // Send that image } } \ No newline at end of file From f923f48641f9a90077eff0ec4244d9cfff4137dd Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:42:05 -0400 Subject: [PATCH 07/12] dumb bug fix 2: electric boogaloo --- bot.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/bot.js b/bot.js index 0bd158d..f944d38 100644 --- a/bot.js +++ b/bot.js @@ -6,10 +6,8 @@ Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); const {google} = require('googleapis'); -const { redis } = require('googleapis/build/src/apis/redis'); const isImage = require('is-image'); he = require('he'); -isImage = require('is-image'); moment = require('moment'); mongo = Promise.promisifyAll(require('mongodb')); path = require('path'); From 0e669c91b48e82195611c2b522b62bc500124dd0 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Sat, 25 Mar 2023 00:37:30 -0400 Subject: [PATCH 08/12] shit ton of bugs, squashed --- bot.js | 21 +++++++++++---------- commands/ooc.js | 4 ++++ package.json | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bot.js b/bot.js index f944d38..f9eb41e 100644 --- a/bot.js +++ b/bot.js @@ -1,7 +1,7 @@ Promise = require('bluebird'); childProcess = require('child_process'); -const redis = require('@redis/client') +const redis = require('redis') Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); @@ -52,11 +52,7 @@ MongoClient.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true} }); // Initialise Redis -async () => { - const redisClient = redis.createClient(); - await redisClient.connect(); - redisClient.on('error', err => console.error(`Could not connect to Redis: ${err}`)) -} +const redisClient = redis.createClient(); // Initialise Google API youtube = google.youtube({ @@ -261,9 +257,10 @@ client.on('messageCreate', async msg => { } // Adds ooc image to cache - if (msg.channelId === IDS.channels.ooc && msg.attachments.size === 1 && isImage(msg.attachments[0].attachment)) { + if (msg.channelId === IDS.channels.ooc && msg.attachments.size === 1 && isImage(msg.attachments.first().attachment)) { + console.log(msg.attachments.first().attachment) await redisClient.sAdd("messageIds", msg.id); - await redisClient.hSet(msg.id, "ImageUrl", msg.attachments[0].attachment.url); + await redisClient.hSet(msg.id, "ImageUrl", msg.attachments.first().attachment); } // Received message starts with bot prefix @@ -304,6 +301,9 @@ client.on('messageDelete', async (msg) => { // Client is connected to Discord client.once('ready', async () => { + // Connect Redis client + await redisClient.connect(); + redisClient.on('error', err => console.error(`Could not connect to Redis: ${err}`)) console.log("Connected as " + client.user.tag); // Confirm connection client.user.setPresence({activity: {name: 'Type "!help" to get all commands'}}); @@ -503,8 +503,9 @@ client.once('ready', async () => { oocMessages2 = oocMessages2.filter(message => (message.attachments.size === 1 && isImage(message.attachments.first().attachment))); oocMessages2.forEach(async (message) => { // For each image in ooc, add its ID to a set and store the image as a hash - await redisClient.sAdd("messageIds", message.id); - await redisClient.hSet(`${message.id}`, "imageUrl", message.attachments[0].attachment.url); + console.log(message.attachments.first().attachment.toString()) + await redisClient.sAdd("messageIds", `${message.id}`); + await redisClient.hSet(`${message.id}`, "imageUrl", message.attachments.first().attachment.toString()); }); console.log("Ready to take commands!"); diff --git a/commands/ooc.js b/commands/ooc.js index 71f2133..602676d 100644 --- a/commands/ooc.js +++ b/commands/ooc.js @@ -1,3 +1,5 @@ +const { assuredworkloads } = require("googleapis/build/src/apis/assuredworkloads") + module.exports = { name: "ooc", help: `\`!ooc\` @@ -8,6 +10,8 @@ module.exports = { \`!ooc\``, async execute(msg, args) { + const redisClient = require('redis').createClient() + await redisClient.connect() messageId = await redisClient.sRandMember("messageIds") // Get random image from cache imageUrl = await redisClient.hGet(messageId, "imageUrl") // Get that image's URL msg.channel.send({files: [imageUrl]}); // Send that image diff --git a/package.json b/package.json index ae455aa..7a986f9 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ }, "homepage": "https://github.com/TheScootz/unity-machine#readme", "dependencies": { - "@redis/client": "^1.5.6", "bluebird": "^3.7.2", "discord.js": "^13.12.0", "dotenv": "^10.0.0", @@ -40,6 +39,7 @@ "node-schedule": "^2.0.0", "npm-check-updates": "^16.0.6", "papaparse": "^5.3.1", + "redis": "^4.6.5", "striptags": "^3.2.0", "xml2js": "^0.4.23", "ytdl-core": "^4.9.1" From 6d2811fb1778889ca42a1288116542031a1cbdc1 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Sat, 25 Mar 2023 00:42:06 -0400 Subject: [PATCH 09/12] Remove dotenv bcuz I forgor --- bot.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bot.js b/bot.js index f9eb41e..b6d3435 100644 --- a/bot.js +++ b/bot.js @@ -20,7 +20,6 @@ ytdl = require('ytdl-core'); const botPrefix = "!"; const version = "2.3.0"; // Version PROD_GUILD = "256222023993393152"; -require('dotenv').config() numRequests = 0; schedule.scheduleJob('*/30 * * * * *', () => numRequests = 0); From e965705f0ffdb2361f4dc44d0787999d37b3c945 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Sat, 25 Mar 2023 10:57:33 -0400 Subject: [PATCH 10/12] Implicit globals suck I almost broke your code lol --- bot.js | 6 +++--- commands/ooc.js | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/bot.js b/bot.js index b6d3435..7dcf149 100644 --- a/bot.js +++ b/bot.js @@ -1,12 +1,12 @@ Promise = require('bluebird'); childProcess = require('child_process'); -const redis = require('redis') +redis = require('redis') Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); const {google} = require('googleapis'); -const isImage = require('is-image'); +isImage = require('is-image'); he = require('he'); moment = require('moment'); mongo = Promise.promisifyAll(require('mongodb')); @@ -51,7 +51,7 @@ MongoClient.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true} }); // Initialise Redis -const redisClient = redis.createClient(); +redisClient = redis.createClient(); // Initialise Google API youtube = google.youtube({ diff --git a/commands/ooc.js b/commands/ooc.js index 602676d..510b3fa 100644 --- a/commands/ooc.js +++ b/commands/ooc.js @@ -10,8 +10,6 @@ module.exports = { \`!ooc\``, async execute(msg, args) { - const redisClient = require('redis').createClient() - await redisClient.connect() messageId = await redisClient.sRandMember("messageIds") // Get random image from cache imageUrl = await redisClient.hGet(messageId, "imageUrl") // Get that image's URL msg.channel.send({files: [imageUrl]}); // Send that image From 45c38762938082527a6e5967899dea3347310d3b Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Sat, 25 Mar 2023 11:02:40 -0400 Subject: [PATCH 11/12] =?UTF-8?q?The=20alphabet=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot.js b/bot.js index 7dcf149..37fe29f 100644 --- a/bot.js +++ b/bot.js @@ -1,17 +1,17 @@ Promise = require('bluebird'); childProcess = require('child_process'); -redis = require('redis') Discord = require('discord.js'); fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); fs = Promise.promisifyAll(require('fs')); const {google} = require('googleapis'); -isImage = require('is-image'); he = require('he'); +isImage = require('is-image'); moment = require('moment'); mongo = Promise.promisifyAll(require('mongodb')); path = require('path'); Papa = require('papaparse'); +redis = require('redis') schedule = require('node-schedule'); const striptags = require('striptags'); const xml2js = require('xml2js'); From e3d4fd8f16895f2cab61f8c1f00dd40665dc7a58 Mon Sep 17 00:00:00 2001 From: Phoenix <71989768+iCEY-007@users.noreply.github.com> Date: Sat, 25 Mar 2023 11:24:10 -0400 Subject: [PATCH 12/12] Who tf put that there --- commands/ooc.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/commands/ooc.js b/commands/ooc.js index 510b3fa..71f2133 100644 --- a/commands/ooc.js +++ b/commands/ooc.js @@ -1,5 +1,3 @@ -const { assuredworkloads } = require("googleapis/build/src/apis/assuredworkloads") - module.exports = { name: "ooc", help: `\`!ooc\`