|
| 1 | +import * as Sentry from "@sentry/bun"; |
| 2 | +import type { Message, Snowflake } from "discord.js"; |
| 3 | +import ExpiryMap from "expiry-map"; |
| 4 | +import { config } from "../../Config.js"; |
| 5 | +import { logger } from "../../logging.js"; |
| 6 | +import type { EventListener } from "../module.js"; |
| 7 | +import { |
| 8 | + type CachedMessage, |
| 9 | + logBulkDeletedMessages, |
| 10 | + logDeletedMessage, |
| 11 | +} from "./logs.js"; |
| 12 | + |
| 13 | +// Configurable TTL - default 24 hours |
| 14 | +const CACHE_TTL_MS = |
| 15 | + config.deletedMessageLog?.cacheTtlMs ?? 1000 * 60 * 60 * 24; |
| 16 | + |
| 17 | +const messageCache = new ExpiryMap<Snowflake, CachedMessage>(CACHE_TTL_MS); |
| 18 | + |
| 19 | +// Auto-excluded mod channels |
| 20 | +const modChannels = new Set([ |
| 21 | + config.channels.modLog, |
| 22 | + config.channels.auditLog, |
| 23 | + config.modmail.channel, |
| 24 | +]); |
| 25 | + |
| 26 | +function isExcludedChannel(channelId: Snowflake): boolean { |
| 27 | + const additionalExcluded = config.deletedMessageLog?.excludedChannels ?? []; |
| 28 | + return modChannels.has(channelId) || additionalExcluded.includes(channelId); |
| 29 | +} |
| 30 | + |
| 31 | +function cacheMessage(message: Message): void { |
| 32 | + if ( |
| 33 | + message.author.bot || |
| 34 | + !message.inGuild() || |
| 35 | + isExcludedChannel(message.channelId) |
| 36 | + ) |
| 37 | + return; |
| 38 | + |
| 39 | + messageCache.set(message.id, { |
| 40 | + id: message.id, |
| 41 | + content: message.content, |
| 42 | + authorId: message.author.id, |
| 43 | + authorTag: message.author.tag, |
| 44 | + channelId: message.channelId, |
| 45 | + createdTimestamp: message.createdTimestamp, |
| 46 | + attachmentUrls: message.attachments.map((a) => a.url), |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +export const DeletedMessagesListener: EventListener = { |
| 51 | + messageCreate(_, message) { |
| 52 | + if (!message.inGuild()) return; |
| 53 | + cacheMessage(message); |
| 54 | + }, |
| 55 | + |
| 56 | + messageUpdate(_, _oldMessage, newMessage) { |
| 57 | + if (!newMessage.inGuild()) return; |
| 58 | + if (newMessage.partial) return; |
| 59 | + cacheMessage(newMessage); |
| 60 | + }, |
| 61 | + |
| 62 | + async messageDelete(client, message) { |
| 63 | + try { |
| 64 | + if (isExcludedChannel(message.channelId)) return; |
| 65 | + |
| 66 | + const cached = messageCache.get(message.id); |
| 67 | + if (!cached) { |
| 68 | + logger.debug(`Deleted message ${message.id} not in cache`); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + await logDeletedMessage(client, cached); |
| 73 | + messageCache.delete(message.id); |
| 74 | + } catch (error) { |
| 75 | + logger.error("Failed to log deleted message:", error); |
| 76 | + Sentry.captureException(error); |
| 77 | + } |
| 78 | + }, |
| 79 | + |
| 80 | + async messageDeleteBulk(client, messages, channel) { |
| 81 | + try { |
| 82 | + if (isExcludedChannel(channel.id)) return; |
| 83 | + |
| 84 | + const cachedMessages: CachedMessage[] = []; |
| 85 | + for (const [id] of messages) { |
| 86 | + const cached = messageCache.get(id); |
| 87 | + if (cached) { |
| 88 | + cachedMessages.push(cached); |
| 89 | + messageCache.delete(id); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (cachedMessages.length === 0) { |
| 94 | + logger.debug(`Bulk deletion in ${channel.id} - no messages in cache`); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + await logBulkDeletedMessages(client, cachedMessages, channel.id); |
| 99 | + } catch (error) { |
| 100 | + logger.error("Failed to log bulk deleted messages:", error); |
| 101 | + Sentry.captureException(error); |
| 102 | + } |
| 103 | + }, |
| 104 | +}; |
0 commit comments