From 40956e5c9efa445a853dd03d74b7e152e1d66da9 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sat, 30 Dec 2023 17:32:44 -0500 Subject: [PATCH] Adding message modify, delete, and attachment tracking (untested) --- breadbot.js | 48 ++++++++++++++++++++++++++++++- utilities/sqlutil.js | 68 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/breadbot.js b/breadbot.js index 8280469..c28d68a 100644 --- a/breadbot.js +++ b/breadbot.js @@ -248,9 +248,25 @@ client.on(Events.MessageCreate, async message => { console.log(`User OK? ${user_ok}`) if (channel_ok && user_ok) { - sqlutil.registerMessage(message.id, message.channelId, message.author.id, message.content, message.createdAt).then(message_add => { + await sqlutil.registerMessage(message.id, message.channelId, message.author.id, message.content, message.createdAt).then(async message_add => { if(message_add) { console.log("Message Added") + + if (message.attachments.size != 0) { + const all_attachments = message.attachments.map(attachment => sqlutil.registerAttachmentIfMissing( + attachment.id, + message.id, + attachment.name, + attachment.description, + message.createdAt, + attachment.contentType, + attachment.url + )) + + await Promise.all(all_attachments).catch((error) => { + console.log(error) + }) + } } else { console.log("Failed to log message") } @@ -258,6 +274,36 @@ client.on(Events.MessageCreate, async message => { } }) +client.on(Events.MessageUpdate, async (oldMessage, newMessage) => { + console.log("Message Update Fired") + console.log(`Old Message Snowflake: ${oldMessage.id}`) + console.log(`New Message Snowflake: ${newMessage.id}`) + + await sqlutil.updateMessageContentIfPresent(newMessage.id, newMessage.content, newMessage.editedAt).then(async (updated) => { + if (updated) { + if (newMessage.attachments.size != 0) { + const all_attachments = newMessage.attachments.map(attachment => sqlutil.registerAttachmentIfMissing( + attachment.id, + newMessage.id, + attachment.name, + attachment.description, + newMessage.editedAt, + attachment.contentType, + attachment.url + )) + + await Promise.all(all_attachments).catch((error) => { + console.log(error) + }) + } + } + }) +}) + +client.on(Events.MessageDelete, async deletedMessage => { + await sqlutil.markMessageDeletedIfPresent(deletedMessage.id) +}) + client.once(Events.ClientReady, c => { console.log(`Ready! Logged in as ${c.user.tag}`); }); diff --git a/utilities/sqlutil.js b/utilities/sqlutil.js index d906225..8cb7316 100644 --- a/utilities/sqlutil.js +++ b/utilities/sqlutil.js @@ -65,6 +65,69 @@ async function registerChannelIfMissing(channel_snowflake, server_snowflake, cha }) } +async function updateMessageContentIfPresent(message_snowflake, message_content, message_timestamp) { + return connection_pool.query("SELECT message_snowflake FROM messages WHERE message_snowflake = ?", [message_snowflake]).then(async ([rows, fields]) => { + if (rows.length == 0) { + console.log("Message specified doesn't exist, probably created before breadbot was here") + return false + } else { + return await connection_pool.query( + "INSERT INTO message_content_changes (message_snowflake, message_change_old_timestamp, message_change_old_content) " + + "SELECT message_snowflake, message_timestamp, message_content) FROM messages WHERE message_snowflake = ?;" + + "UPDATE messages SET message_timestamp = ?, message_content = ? WHERE message_snowflake = ?;", + [message_snowflake, message_timestamp, message_content, message_snowflake] + ).then(([rows, fields]) => { + return true + }) + } + }).catch((error) => { + console.log(error) + + return false + }) +} + +async function markMessageDeletedIfPresent(message_snowflake) { + return connection_pool.query("SELECT message_snowflake FROM messages WHERE message_snowflake = ?", [message_snowflake]).then(async ([rows, fields]) => { + if (rows.length == 0) { + console.log("Message specified doesn't exists, probably created before breadbot was here") + + return false + } else { + return await connection_pool.query( + "UPDATE messages SET message_deleted = 1 WHERE message_snowflake = ?", [message_snowflake] + ).then(([rows, fields]) => { + return true + }) + } + }).catch((error) => { + console.log(error) + + return false + }) +} + +async function registerAttachmentIfMissing(attachment_snowflake, message_snowflake, attachment_name, attachment_description, attachment_timestamp, attachment_mime_type, attachment_url) { + return connection_pool.query("SELECT attachment_snowflake FROM message_attachments WHERE attachment_snowflake = ?", [attachment_snowflake]).then(async ([rows, fields]) => { + if (rows.length != 0) { + console.log("Attachment already exists") + + return true + } else { + return await connection_pool.query( + "INSERT INTO (attachment_snowflake, message_snowflake, attachment_name, attachment_description, attachment_timestamp, attachment_mime_type, attachment_url) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)", + [attachment_snowflake, message_snowflake, attachment_name, attachment_description, attachment_timestamp, attachment_mime_type, attachment_url] + ).then(([rows, fields]) => { + return true + }) + } + }).catch((error) => { + console.log(error) + + return false + }) +} async function registerUserIfMissing(user_snowflake, user_name, user_displayname) { return connection_pool.query("SELECT * FROM users WHERE user_snowflake = ?;", [user_snowflake]).then(async ([rows, fields]) => { @@ -194,5 +257,8 @@ module.exports = { getVoiceActiveUsers, registerNewCall, updateCallEndTime, - inCall + inCall, + updateMessageContentIfPresent, + markMessageDeletedIfPresent, + registerAttachmentIfMissing } \ No newline at end of file