|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 InterChat |
| 3 | + * |
| 4 | + * InterChat is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as published |
| 6 | + * by the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * InterChat is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with InterChat. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +import BaseEventListener from '#src/core/BaseEventListener.js'; |
| 19 | +import { getConnectionHubId } from '#src/utils/ConnectedListUtils.js'; |
| 20 | +import { logMsgDelete } from '#src/utils/hub/logger/ModLogs.js'; |
| 21 | +import { fetchHub } from '#src/utils/hub/utils.js'; |
| 22 | +import { deleteMessageFromHub } from '#src/utils/moderation/deleteMessage.js'; |
| 23 | +import { getOriginalMessage } from '#src/utils/network/messageUtils.js'; |
| 24 | +import { stripIndents } from 'common-tags'; |
| 25 | +import { AuditLogEvent, Message, OmitPartialGroupDMChannel, PartialMessage } from 'discord.js'; |
| 26 | + |
| 27 | +export default class MessageDelete extends BaseEventListener<'messageDelete'> { |
| 28 | + readonly name = 'messageDelete'; |
| 29 | + |
| 30 | + public async execute(message: OmitPartialGroupDMChannel<Message<boolean> | PartialMessage>) { |
| 31 | + if (!message.inGuild()) return; |
| 32 | + |
| 33 | + const fetchedLogs = await message.guild.fetchAuditLogs({ |
| 34 | + type: AuditLogEvent.MessageDelete, |
| 35 | + limit: 10, |
| 36 | + }); |
| 37 | + |
| 38 | + const deletedMessageLog = fetchedLogs.entries.find( |
| 39 | + (entry) => |
| 40 | + entry.targetId === message.author.id && |
| 41 | + entry.extra.channel.id === message.channelId && |
| 42 | + entry.createdTimestamp > Date.now() - 5000, |
| 43 | + ); |
| 44 | + |
| 45 | + const deletedBy = deletedMessageLog?.executor ?? message.author; |
| 46 | + |
| 47 | + const connectionHubId = await getConnectionHubId(message.channelId); |
| 48 | + if (!connectionHubId) return; |
| 49 | + |
| 50 | + const hub = await fetchHub({ id: connectionHubId }); |
| 51 | + if (!hub) return; |
| 52 | + |
| 53 | + // strictly check if the message is the original, non-webhook message |
| 54 | + // because with broadcasts (webhook messages), there is no possible way of knowing who deleted the message through audit logs |
| 55 | + const originalMsg = await getOriginalMessage(message.id); |
| 56 | + if (!originalMsg) return; |
| 57 | + |
| 58 | + message.channel |
| 59 | + .send({ |
| 60 | + content: stripIndents` |
| 61 | + ${this.getEmoji('info_icon')} ${deletedBy}, you deleted a message from this channel. As this channel is linked to a hub, it will be deleted from other linked servers as well. |
| 62 | + -# Hub moderators can still see the message in the mod logs. Whatever you say in a hub is your responsibility. |
| 63 | + `, |
| 64 | + allowedMentions: { users: [deletedBy.id] }, |
| 65 | + }) |
| 66 | + .then((msg) => setTimeout(() => msg.delete(), 10_000)) |
| 67 | + .catch(() => null); |
| 68 | + |
| 69 | + await deleteMessageFromHub(hub.id, originalMsg.messageId); |
| 70 | + |
| 71 | + await logMsgDelete(message.client, originalMsg, await hub.fetchLogConfig(), { |
| 72 | + hubName: hub.data.name, |
| 73 | + modName: deletedBy.username, |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments