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
*/
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})`,
},
]),
];

View File

@ -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,
});

View File

@ -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 },