Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'}});

Expand Down Expand Up @@ -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!");
});
Expand Down
9 changes: 3 additions & 6 deletions commands/ooc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Shamelessly stolen from the redis.io website
timeout 0
tcp-keepalive 300
# No persistence
save ""
appendonly no