cleanup and attachment logs for bulk deletes

This commit is contained in:
Erik 2021-06-16 21:39:47 +03:00
parent 747c718570
commit 67f8d32040
No known key found for this signature in database
GPG Key ID: 7E862371D3409F16

View File

@ -106,10 +106,6 @@ class GuildLogger extends Observer {
const { reference, channel, author, content, id } = message;
const embed = {
// author: {
// name: message.format('MSGLOG_DELETE_TITLE', { channel: message.channel.name, author: Util.escapeMarkdown(message.author.tag) }), //`${message.author.tag} (${message.author.id})`,
// icon_url: message.author.displayAvatarURL({ size: 32 }) //eslint-disable-line camelcase
// },
title: message.format('MSGLOG_DELETE_TITLE', { channel: channel.name, author: Util.escapeMarkdown(author.tag) }),
description: Util.escapeMarkdown(content)?.replace(/\\n/gu, ' ') || message.format('MSGLOG_NOCONTENT'),
color: CONSTANTS.COLORS.RED,
@ -202,7 +198,7 @@ class GuildLogger extends Observer {
const currentMb = currentFiles.length > 0 ? currentFiles.map((f) => f.size).reduce((p, v) => p + v) : 0;
if (currentMb + file.size > 8 * CONSTANTS.IMAGES.MB_DIVIDER) {
await upload(currentFiles);
currentFiles = [];
currentFiles = [file];
} else {
currentFiles.push(file);
}
@ -223,24 +219,6 @@ class GuildLogger extends Observer {
this.client.logger.error('Error in message delete:\n' + err.stack);
});
/*
if(message.attachments.size > 0) {
return this.logAttachment({ msgID: message.id, guildID: guild.id, channelID: channel.id, logChannel });
}
const embed = {
title: message.format('MSGLOG_DELETE_TITLE', { author: Util.escapeMarkdown(author.tag), channel: channel.name }),
description: message.content,
footer: {
text: message.format('MSGLOG_DELETE_FOOTER', { msgID: message.id, userID: author.id })
},
color: CONSTANTS.COLORS.RED,
timestamp: message.createdAt
};
await logChannel.send({ embed });
*/
}
async messageDeleteBulk(messages) {
@ -253,6 +231,8 @@ class GuildLogger extends Observer {
const settings = await guild.settings();
const chatlogs = settings.messageLog;
if (!chatlogs.channel) return;
const logChannel = await guild.resolveChannel(chatlogs.channel);
if (!logChannel) return undefined;
const { ignore, bypass } = chatlogs;
if (ignore.includes(channel.id)) return;
@ -284,6 +264,60 @@ class GuildLogger extends Observer {
}
}
const uploaded = [];
if (message.attachments.size > 0 && chatlogs.attachments && logChannel.nsfw) {
const data = await this.client.storageManager.mongodb.messages.findOne({
id: message.id
});
if (data) {
const attachments = await this.client.storageManager.mongodb.attachments.find({
_id: { $in: data.attachments.filter((a) => a.index).map((a) => a.index) }
});
const sortedAttachments = data.attachments.sort((a, b) => b.size - a.size);
const files = [];
for (const attachment of sortedAttachments) {
const attachmentData = attachments.find((a) => a.attachmentId === attachment.id);
if (!attachmentData) continue;
// TODO: Implement a webhook on the logging server to upload the images instead
if (attachment.size > 8 * CONSTANTS.IMAGES.MB_DIVIDER) continue; //(attachment.size > CONSTANTS.IMAGES.UPLOAD_LIMIT[message.guild.premiumTier] * CONSTANTS.IMAGES.MB_DIVIDER)
attachmentData.buffer = attachmentData.buffer.buffer;
const messageAttachment = new MessageAttachment(attachmentData.buffer, attachment.name, { size: attachment.size });
files.push(messageAttachment);
}
let currentFiles = [];
const upload = async (files) => {
const attachmentMessage = await this.attachmentWebhook.send(null, files).catch((error) => this.client.logger.error(error));
attachmentMessage.attachments.map((a) => uploaded.push(`[${a.filename} (${(a.size / CONSTANTS.IMAGES.MB_DIVIDER).toFixed(2)}mb)](${a.url})`));
};
for (const file of files) {
const currentMb = currentFiles.length > 0 ? currentFiles.map((f) => f.size).reduce((p, v) => p + v) : 0;
if (currentMb + file.size > 8 * CONSTANTS.IMAGES.MB_DIVIDER) {
await upload(currentFiles);
currentFiles = [file];
} else {
currentFiles.push(file);
}
}
if (currentFiles.length > 0) {
await upload(currentFiles);
}
}
}
//const attStr = message.attachments.map((att) => `${att.name} (${att.id})`).join(', ');
const value = stripIndents`${content ? content.substring(0, cutOff) : message.format('BULK_DELETE_NO_CONTENT')}`;
//${message.attachments.size > 0 ? `__(Attachments: ${attStr})__` : '' }
@ -297,6 +331,12 @@ class GuildLogger extends Observer {
value: content.substring(cutOff)
});
if (uploaded.length) fields.push({
name: message.format('BULK_DELETE_ATTACHMENTS'),
value: uploaded.join('\n'),
_attachment: true
});
}
//Compile embeds
@ -328,7 +368,7 @@ class GuildLogger extends Observer {
for (let i = 0; i < embeds.length; i++) {
const embed = embeds[i];
//Amount of messages showing
const x = embed.fields.filter((field) => field.name !== '\u200b').length;
const x = embed.fields.filter((field) => field.name !== '\u200b' && !field._attachment).length;
embed.title = guild.format('BULK_DELETE_TITLE', { embedNr: i + 1, channel: channel.name });
embed.footer = { text: guild.format('BULK_DELETE_FOOTER', { rangeStart: showed + 1, rangeEnd: showed += x, total: messages.size }) };
const result = await hook.send({ embeds: [embed] }).catch((err) => {