From d26db7d3a553c3a85e60fde15cb5fc83211d8c32 Mon Sep 17 00:00:00 2001 From: Philippe Dellaert Date: Sun, 27 Aug 2023 11:56:03 -0700 Subject: [PATCH] feat: Adding support for embeds and referenced messages in delete log (#488) --- .github/CHANGELOG.md | 3 +- src/handlers/messagedelete.ts | 92 ++++++++++++++++------------------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index d3555c88..4054ec8a 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -2,7 +2,8 @@ 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 diff --git a/src/handlers/messagedelete.ts b/src/handlers/messagedelete.ts index ecb42a1a..e4cf292a 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,49 @@ 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 (const messageEmbed of messageEmbeds) { + const { image, fields } = messageEmbed; + if (image) { + messageComponents.push(`<${image.url}>`); + } + for (const field of fields) { + 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}`, + name: 'Date', + value: formattedDate, 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, - 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 +75,27 @@ module.exports = { value: `${message.channel}`, inline: true, }, + { + name: 'Reply to', + value: messageReference ? `${messageReference.url}` : 'None', + inline: true, + }, { 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] }); } }, };