From 345f640d71679f1bf5556473d55f6ecad4b19f2b Mon Sep 17 00:00:00 2001 From: Philippe Dellaert Date: Tue, 18 Jul 2023 21:19:27 -0700 Subject: [PATCH 1/5] Adding support for embeds and referenced messages in delete log --- src/handlers/messagedelete.ts | 94 ++++++++++++++++------------------- 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/src/handlers/messagedelete.ts b/src/handlers/messagedelete.ts index ecb42a1a..54061da3 100644 --- a/src/handlers/messagedelete.ts +++ b/src/handlers/messagedelete.ts @@ -1,13 +1,13 @@ -import { Colors, AuditLogEvent, TextChannel } from 'discord.js'; +import { Colors, AuditLogEvent, TextChannel, Message } from 'discord.js'; import moment from 'moment'; import { Channels, UserLogExclude } from '../constants'; import { makeEmbed } from '../lib/embed'; -const FEATURE_NOT_AVAIL = '(can\'t show embeds or images)'; +const CONTENT_NOT_AVAIL = 'Unable to find content or embeds.'; module.exports = { event: 'messageDelete', - executor: async (message) => { + executor: async (message: Message) => { if (message.guild === null) { // DMs return; @@ -22,55 +22,51 @@ module.exports = { limit: 1, type: AuditLogEvent.MessageDelete, }); - const deletionLog = fetchedLogs.entries.first(); - const currentDate = new Date(); const formattedDate: string = moment(currentDate).utcOffset(0).format('DD, MM, YYYY, HH:mm:ss'); - const userLogsChannel = message.guild.channels.resolve(Channels.USER_LOGS) as TextChannel | null; - - const messageDeleteEmbedNoLog = (formattedDate) => makeEmbed({ + const messageEmbeds = message.embeds.length > 0 ? message.embeds : []; + const messageComponents = []; + if (message.content) { + messageComponents.push(message.content); + } + if (message.attachments) { + message.attachments.forEach((attachment) => { + if (attachment.url || attachment.proxyURL) { + messageComponents.push(attachment.url ? attachment.url : attachment.proxyURL); + } + }); + } + for (let i = 0; i < messageEmbeds.length; i++) { + const messageEmbed = messageEmbeds[i]; + const { image, fields } = messageEmbed; + if (image) { + messageComponents.push(`<${image.url}>`); + } + for (let j = 0; j < fields.length; j++) { + const field = fields[i]; + const { name, value } = field; + if (name && value) { + messageComponents.push(`${name}: ${value}`); + } + } + } + const messageContent = messageComponents.join('\n'); + const messageReference = message.reference ? await message.fetchReference() : null; + const messageDeleteEmbed = makeEmbed({ color: Colors.Red, thumbnail: { url: 'https://cdn.discordapp.com/attachments/770835189419999262/779946282373873694/150-1509174_deleted-message-icon-sign-hd-png-download.png' }, author: { name: message.author.tag, - iconURL: message.author.displayAvatarURL({ dynamic: true }), + iconURL: message.author.displayAvatarURL(), }, fields: [ { - name: 'Author', - value: `${message.author}`, - inline: true, - }, - { - name: 'Channel', - value: `${message.channel}`, - inline: true, - }, - { - name: 'Deleted by', - value: 'No audit log was found, message was either deleted by author, or a bot', - inline: false, - }, - { - name: 'Deleted Message', - value: message.content ? `${message.content}` : FEATURE_NOT_AVAIL, + name: 'Date', + value: formattedDate, inline: false, }, - { name: 'Date', value: formattedDate, inline: false }, - ], - footer: { text: `User ID: ${message.author.id}` }, - }); - - const messageDeleteEmbed = (executor, formattedDate) => makeEmbed({ - color: Colors.Red, - thumbnail: { url: 'https://cdn.discordapp.com/attachments/770835189419999262/779946282373873694/150-1509174_deleted-message-icon-sign-hd-png-download.png' }, - author: { - name: message.author.tag, - iconURL: message.author.displayAvatarURL({ dynamic: true }), - }, - fields: [ { name: 'Author', value: `${message.author}`, @@ -81,31 +77,27 @@ module.exports = { value: `${message.channel}`, inline: true, }, + { + name: 'Reply to', + value: messageReference ? `${messageReference.url}` : 'None', + inline: false, + }, { name: 'Deleted by', - value: `${executor}`, + value: (deletionLog && deletionLog.target.id === message.author.id) ? `${deletionLog.executor}` : 'No audit log was found, message was either deleted by author, or a bot', inline: false, }, { name: 'Deleted Message', - value: message.content ? `${message.content}` : FEATURE_NOT_AVAIL, + value: messageContent ? `${messageContent}` : CONTENT_NOT_AVAIL, inline: false, }, - { name: 'Date', value: formattedDate, inline: false }, ], footer: { text: `User ID: ${message.author.id}` }, }); if (userLogsChannel && !UserLogExclude.some((e) => e === message.author.id)) { - if (!deletionLog) await userLogsChannel.send({ embeds: [messageDeleteEmbedNoLog(formattedDate)] }); - - const { executor, target } = deletionLog; - - if (target.id === message.author.id) { - await userLogsChannel.send({ embeds: [messageDeleteEmbed(executor, formattedDate)] }); - } else { - await userLogsChannel.send({ embeds: [messageDeleteEmbedNoLog(formattedDate)] }); - } + await userLogsChannel.send({ embeds: [messageDeleteEmbed] }); } }, }; From 998f6e36dbe339c7b154b5f82cc964e37e9fb8e5 Mon Sep 17 00:00:00 2001 From: Philippe Dellaert Date: Tue, 18 Jul 2023 21:23:11 -0700 Subject: [PATCH 2/5] CHANGELOG for delete handler enhancements --- .github/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index d3555c88..5e3d6824 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -6,6 +6,7 @@ Update _ August 2023 Update _ July 2023 +- feat: enhancing user delete log with reference if a reply is deleted, and with embed and attachment data (27/07/2023) - fix: change the checklist image in the checklist command to the correct one (27/07/2023) - fix: Removing auto-delete of timeout command and response, and fixing bug with untimeout (27/07/2023) - feat: optimize simbriefdata for input error and check for FBW airframe (11/07/2023) From 5662aa6a858ca788c078b7d7ca3917b1caa92bab Mon Sep 17 00:00:00 2001 From: Philippe Dellaert Date: Tue, 18 Jul 2023 21:32:15 -0700 Subject: [PATCH 3/5] Condensing data a little --- src/handlers/messagedelete.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/handlers/messagedelete.ts b/src/handlers/messagedelete.ts index 54061da3..996f46f3 100644 --- a/src/handlers/messagedelete.ts +++ b/src/handlers/messagedelete.ts @@ -65,7 +65,7 @@ module.exports = { { name: 'Date', value: formattedDate, - inline: false, + inline: true, }, { name: 'Author', @@ -80,7 +80,7 @@ module.exports = { { name: 'Reply to', value: messageReference ? `${messageReference.url}` : 'None', - inline: false, + inline: true, }, { name: 'Deleted by', From f8e4fc8bdb50170c413985c32215f1de8bb943f6 Mon Sep 17 00:00:00 2001 From: Philippe Dellaert Date: Thu, 27 Jul 2023 15:49:51 +0200 Subject: [PATCH 4/5] Improving loops based on review comments --- src/handlers/messagedelete.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/handlers/messagedelete.ts b/src/handlers/messagedelete.ts index 996f46f3..e4cf292a 100644 --- a/src/handlers/messagedelete.ts +++ b/src/handlers/messagedelete.ts @@ -38,14 +38,12 @@ module.exports = { } }); } - for (let i = 0; i < messageEmbeds.length; i++) { - const messageEmbed = messageEmbeds[i]; + for (const messageEmbed of messageEmbeds) { const { image, fields } = messageEmbed; if (image) { messageComponents.push(`<${image.url}>`); } - for (let j = 0; j < fields.length; j++) { - const field = fields[i]; + for (const field of fields) { const { name, value } = field; if (name && value) { messageComponents.push(`${name}: ${value}`); From 64c9fd25c81840d480d7d3f7d6ea1f33c6eb7005 Mon Sep 17 00:00:00 2001 From: Philippe Dellaert Date: Sun, 27 Aug 2023 11:39:15 -0700 Subject: [PATCH 5/5] Updating CHANGELOG for deletion enhancer --- .github/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 5e3d6824..4054ec8a 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -2,11 +2,11 @@ Update _ August 2023 -- feat: added .simbridgedebug command which explains debugging steps, if SimBridge crashes right after starting (07/08/2023) +- feat: enhancing user delete log with reference if a reply is deleted, and with embed and attachment data (27/08/2023) +- feat: added .simbridgedebug command which explains debugging steps, if SimBridge crashes right after starting (27/08/2023) Update _ July 2023 -- feat: enhancing user delete log with reference if a reply is deleted, and with embed and attachment data (27/07/2023) - fix: change the checklist image in the checklist command to the correct one (27/07/2023) - fix: Removing auto-delete of timeout command and response, and fixing bug with untimeout (27/07/2023) - feat: optimize simbriefdata for input error and check for FBW airframe (11/07/2023)