Skip to content

Commit

Permalink
Merge pull request #547 from aternosorg/message-deletes
Browse files Browse the repository at this point in the history
add message ids to bulk delete and normal message log
  • Loading branch information
JulianVennen authored Dec 14, 2022
2 parents d14fb7e + 55fb3df commit 3d48aea
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/discord/GuildWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default class GuildWrapper {

/**
* send this message to the guild's log channel
* @param {import('discord.js').MessagePayload|import('discord.js').MessageOptions} options
* @param {import('discord.js').MessagePayload|import('discord.js').MessageCreateOptions} options
* @return {Promise<?Message>} Discord message (if it was sent)
*/
async log(options) {
Expand All @@ -195,7 +195,7 @@ export default class GuildWrapper {

/**
* send this message to the guild's message log channel
* @param {import('discord.js').MessagePayload|import('discord.js').MessageOptions} options
* @param {import('discord.js').MessagePayload|import('discord.js').MessageCreateOptions} options
* @return {Promise<?Message>} Discord message (if it was sent)
*/
async logMessage(options) {
Expand All @@ -205,7 +205,7 @@ export default class GuildWrapper {

/**
* send this message to the guild's join log channel
* @param {import('discord.js').MessagePayload|import('discord.js').MessageOptions} options
* @param {import('discord.js').MessagePayload|import('discord.js').MessageCreateOptions} options
* @return {Promise<?Message>} Discord message (if it was sent)
*/
async logJoin(options) {
Expand Down
6 changes: 5 additions & 1 deletion src/embeds/MessageDeleteEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export default class MessageDeleteEmbed extends EmbedWrapper {
this.setAuthor({
name: `Message by ${escapeMarkdown(message.author.tag)} was deleted in #${message.channel.name}`,
iconURL: message.author.avatarURL()
}).setFooter({text: message.author.id});
}).setFooter({text:
`Message ID: ${message.id}\n` +
`Channel ID: ${message.channel.id}\n` +
`User ID: ${message.author.id}`
});

if (message.content.length) {
this.setDescription(message.content.substring(0, EMBED_DESCRIPTION_LIMIT));
Expand Down
27 changes: 23 additions & 4 deletions src/events/discord/MessageDeleteBulkEventListener.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import EventListener from '../EventListener.js';
import {EmbedBuilder, escapeMarkdown} from 'discord.js';
import {AttachmentBuilder, EmbedBuilder, userMention} from 'discord.js';
import colors from '../../util/colors.js';
import GuildWrapper from '../../discord/GuildWrapper.js';
import {EMBED_DESCRIPTION_LIMIT} from '../../util/apiLimits.js';
import {EMBED_DESCRIPTION_LIMIT, MESSAGE_FILE_LIMIT} from '../../util/apiLimits.js';

export default class MessageDeleteBulkEventListener extends EventListener {
get name() {
Expand All @@ -18,11 +18,29 @@ export default class MessageDeleteBulkEventListener extends EventListener {
const embed = new EmbedBuilder()
.setTitle(`${messages.size} messages were deleted in ${channel.name}`)
.setColor(colors.RED);
const attachments = [];
for (const message of messages.sort((m1, m2) => m1.createdTimestamp - m2.createdTimestamp).values()) {
if (!message.content) {
for (const attachment of message.attachments.values()) {
attachments.push(new AttachmentBuilder(attachment.attachment)
.setDescription(attachment.description)
.setName(attachment.name)
.setSpoiler(true));
}

let content = message.content.replaceAll('\n', ' ').trim();
let attachmentCount = message.attachments.size;

if (!content && attachmentCount === 0) {
continue;
}
const data = `${escapeMarkdown(message.author.tag)} (${message.author.id}): ${message.content.replaceAll('\n', ' ').trim()}\n`;

let data = `${userMention(message.author.id)} (${message.id})`;
if (attachmentCount) {
data += `${attachmentCount} file${attachmentCount === 1 ? '' : 's'}`;
}
if (content) {
data += `: ${content}\n`;
}

const description = embed.data.description ?? '';
embed.setDescription((description + data).substring(0, EMBED_DESCRIPTION_LIMIT));
Expand All @@ -39,6 +57,7 @@ export default class MessageDeleteBulkEventListener extends EventListener {
const guild = new GuildWrapper(channel.guild);
await guild.logMessage({
embeds: [embed],
files: attachments.slice(0, MESSAGE_FILE_LIMIT)
});
}
}
4 changes: 3 additions & 1 deletion src/util/apiLimits.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ export const BULK_DELETE_MAX_AGE = 14 * 24 * 60 * 60 * 1000;
* maximum number of bans that can be fetched per page
* @type {number}
*/
export const FETCH_BAN_PAGE_SIZE = 1000;
export const FETCH_BAN_PAGE_SIZE = 1000;

export const MESSAGE_FILE_LIMIT = 10;

0 comments on commit 3d48aea

Please sign in to comment.