diff --git a/src/lib/logging.js b/src/lib/logging.js index 4bb5b2b..1b4829c 100644 --- a/src/lib/logging.js +++ b/src/lib/logging.js @@ -191,17 +191,18 @@ async function logTicketEvent(client, { * @param {import("@prisma/client").Ticket & {guild: import("@prisma/client").Guild}} details.ticket */ async function logMessageEvent(client, { - action, target, ticket, diff, + action, executor, target, ticket, diff, }) { if (!ticket) return; - client.log.info.tickets(`${target.member.user.tag} ${client.i18n.getMessage('en-GB', `log.message.verb.${action}`)} message ${target.id}`); + if (executor === undefined) executor = target.member; + client.log.info.tickets(`${executor?.user.tag || 'Unknown'} ${client.i18n.getMessage('en-GB', `log.message.verb.${action}`)} message ${target.id}`); if (!ticket.guild.logChannel) return; const colour = action === 'update' ? 'Purple' : action === 'delete' ? 'DarkPurple' : 'Default'; const getMessage = client.i18n.getLocale(ticket.guild.locale); const i18nOptions = { - user: `<@${target.member.user.id}>`, + user: `<@${executor?.user.id}>`, verb: getMessage(`log.message.verb.${action}`), }; const channel = client.channels.cache.get(ticket.guild.logChannel); @@ -210,15 +211,15 @@ async function logMessageEvent(client, { new EmbedBuilder() .setColor(colour) .setAuthor({ - iconURL: target.member.displayAvatarURL(), - name: target.member.displayName, + iconURL: target.member?.displayAvatarURL() || 'https://discord.com/assets/1f0bfc0865d324c2587920a7d80c609b.png', + name: target.member?.displayName || 'Unknown', }) .setTitle(getMessage('log.message.title', i18nOptions)) .setDescription(getMessage('log.message.description', i18nOptions)) .addFields([ { name: getMessage('log.message.message'), - value: `[${target.id}](${target.url})`, + value: `[\`${target.id}\`](${target.url})`, }, ]), ]; diff --git a/src/listeners/client/messageDelete.js b/src/listeners/client/messageDelete.js index 96f779e..68c7953 100644 --- a/src/listeners/client/messageDelete.js +++ b/src/listeners/client/messageDelete.js @@ -1,5 +1,8 @@ const { Listener } = require('@eartharoid/dbf'); +const { AuditLogEvent } = require('discord.js'); const { logMessageEvent } = require('../../lib/logging'); +const Cryptr = require('cryptr'); +const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY); module.exports = class extends Listener { constructor(client, options) { @@ -14,16 +17,26 @@ module.exports = class extends Listener { /** @type {import("client")} */ const client = this.client; + if (!message.guild) return; + const ticket = await client.prisma.ticket.findUnique({ include: { guild: true }, where: { id: message.channel.id }, }); if (!ticket) return; + let content = message.cleanContent; + + const logEvent = (await message.guild.fetchAuditLogs({ + limit: 1, + type: AuditLogEvent.MessageDelete, + })).entries.first(); + if (ticket.guild.archive) { try { const archived = await client.prisma.archivedMessage.findUnique({ where: { id: message.id } }); if (archived) { + if (!content) content = JSON.parse(decrypt(archived.content)).content; // won't be cleaned await client.prisma.archivedMessage.update({ data: { deleted: true }, where: { id: message.id }, @@ -35,12 +48,29 @@ module.exports = class extends Listener { } } + let { + executor, + target, + } = logEvent ?? {}; + + executor ||= undefined; + if (target?.id !== message.author?.id) executor = undefined; + + if (executor) { + try { + executor = await message.guild.members.fetch(executor.id); + } catch (error) { + client.log.error(error); + } + } + await logMessageEvent(this.client, { action: 'delete', diff: { - original: { content: message.cleanContent }, + original: { content }, updated: { content: '' }, }, + executor, target: message, ticket, }); diff --git a/src/listeners/client/messageUpdate.js b/src/listeners/client/messageUpdate.js index 1ff4cd9..db3b78d 100644 --- a/src/listeners/client/messageUpdate.js +++ b/src/listeners/client/messageUpdate.js @@ -20,9 +20,17 @@ module.exports = class extends Listener { /** @type {import("client")} */ const client = this.client; - if (newMessage.partial) newMessage.fetch().then(m => (newMessage = m)).catch(client.log.error); + if (newMessage.partial) { + try { + newMessage = await newMessage.fetch(); + } catch (error) { + client.log.error(error); + } + } + + if (!newMessage.guild) return; if (newMessage.flags.has(MessageFlagsBitField.Flags.Ephemeral)) return; - if (!newMessage.edited) return; + if (!newMessage.editedAt) return; const ticket = await client.prisma.ticket.findUnique({ include: { guild: true },