fix: message logging

This commit is contained in:
Isaac 2023-01-30 12:49:15 +00:00
parent 2a8c1603f2
commit a60c998605
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
3 changed files with 48 additions and 9 deletions

View File

@ -191,17 +191,18 @@ async function logTicketEvent(client, {
* @param {import("@prisma/client").Ticket & {guild: import("@prisma/client").Guild}} details.ticket * @param {import("@prisma/client").Ticket & {guild: import("@prisma/client").Guild}} details.ticket
*/ */
async function logMessageEvent(client, { async function logMessageEvent(client, {
action, target, ticket, diff, action, executor, target, ticket, diff,
}) { }) {
if (!ticket) return; 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; if (!ticket.guild.logChannel) return;
const colour = action === 'update' const colour = action === 'update'
? 'Purple' : action === 'delete' ? 'Purple' : action === 'delete'
? 'DarkPurple' : 'Default'; ? 'DarkPurple' : 'Default';
const getMessage = client.i18n.getLocale(ticket.guild.locale); const getMessage = client.i18n.getLocale(ticket.guild.locale);
const i18nOptions = { const i18nOptions = {
user: `<@${target.member.user.id}>`, user: `<@${executor?.user.id}>`,
verb: getMessage(`log.message.verb.${action}`), verb: getMessage(`log.message.verb.${action}`),
}; };
const channel = client.channels.cache.get(ticket.guild.logChannel); const channel = client.channels.cache.get(ticket.guild.logChannel);
@ -210,15 +211,15 @@ async function logMessageEvent(client, {
new EmbedBuilder() new EmbedBuilder()
.setColor(colour) .setColor(colour)
.setAuthor({ .setAuthor({
iconURL: target.member.displayAvatarURL(), iconURL: target.member?.displayAvatarURL() || 'https://discord.com/assets/1f0bfc0865d324c2587920a7d80c609b.png',
name: target.member.displayName, name: target.member?.displayName || 'Unknown',
}) })
.setTitle(getMessage('log.message.title', i18nOptions)) .setTitle(getMessage('log.message.title', i18nOptions))
.setDescription(getMessage('log.message.description', i18nOptions)) .setDescription(getMessage('log.message.description', i18nOptions))
.addFields([ .addFields([
{ {
name: getMessage('log.message.message'), name: getMessage('log.message.message'),
value: `[${target.id}](${target.url})`, value: `[\`${target.id}\`](${target.url})`,
}, },
]), ]),
]; ];

View File

@ -1,5 +1,8 @@
const { Listener } = require('@eartharoid/dbf'); const { Listener } = require('@eartharoid/dbf');
const { AuditLogEvent } = require('discord.js');
const { logMessageEvent } = require('../../lib/logging'); const { logMessageEvent } = require('../../lib/logging');
const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class extends Listener { module.exports = class extends Listener {
constructor(client, options) { constructor(client, options) {
@ -14,16 +17,26 @@ module.exports = class extends Listener {
/** @type {import("client")} */ /** @type {import("client")} */
const client = this.client; const client = this.client;
if (!message.guild) return;
const ticket = await client.prisma.ticket.findUnique({ const ticket = await client.prisma.ticket.findUnique({
include: { guild: true }, include: { guild: true },
where: { id: message.channel.id }, where: { id: message.channel.id },
}); });
if (!ticket) return; if (!ticket) return;
let content = message.cleanContent;
const logEvent = (await message.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MessageDelete,
})).entries.first();
if (ticket.guild.archive) { if (ticket.guild.archive) {
try { try {
const archived = await client.prisma.archivedMessage.findUnique({ where: { id: message.id } }); const archived = await client.prisma.archivedMessage.findUnique({ where: { id: message.id } });
if (archived) { if (archived) {
if (!content) content = JSON.parse(decrypt(archived.content)).content; // won't be cleaned
await client.prisma.archivedMessage.update({ await client.prisma.archivedMessage.update({
data: { deleted: true }, data: { deleted: true },
where: { id: message.id }, 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, { await logMessageEvent(this.client, {
action: 'delete', action: 'delete',
diff: { diff: {
original: { content: message.cleanContent }, original: { content },
updated: { content: '' }, updated: { content: '' },
}, },
executor,
target: message, target: message,
ticket, ticket,
}); });

View File

@ -20,9 +20,17 @@ module.exports = class extends Listener {
/** @type {import("client")} */ /** @type {import("client")} */
const client = this.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.flags.has(MessageFlagsBitField.Flags.Ephemeral)) return;
if (!newMessage.edited) return; if (!newMessage.editedAt) return;
const ticket = await client.prisma.ticket.findUnique({ const ticket = await client.prisma.ticket.findUnique({
include: { guild: true }, include: { guild: true },