diff --git a/bot.js b/bot.js index 4e0a6a8..37fe29f 100644 --- a/bot.js +++ b/bot.js @@ -11,6 +11,7 @@ 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'); @@ -49,6 +50,9 @@ 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 +redisClient = redis.createClient(); + // Initialise Google API youtube = google.youtube({ version: 'v3', @@ -251,6 +255,13 @@ client.on('messageCreate', async msg => { return; } + // Adds ooc image to cache + 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.first().attachment); + } + // Received message starts with bot prefix if (msg.content.startsWith(botPrefix)) { const fullCommand = msg.content.substr(botPrefix.length); // Remove the leading bot prefix @@ -278,8 +289,20 @@ 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 () => { + // 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'}}); @@ -469,10 +492,20 @@ 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 + 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 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 diff --git a/package.json b/package.json index 7458d63..7a986f9 100644 --- a/package.json +++ b/package.json @@ -39,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" 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