From 79a3ff8fd922ba009c995db899fda6a45bba9199 Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Tue, 23 Jan 2024 07:13:59 +1100 Subject: [PATCH] Add raw gateway packets for messageUpdate and messageDelete --- src/events/messageDelete.ts | 6 ++++++ src/events/messageUpdate.ts | 10 +++++++++- src/interfaces.d.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/events/messageDelete.ts b/src/events/messageDelete.ts index 5ff14ad..8b604af 100644 --- a/src/events/messageDelete.ts +++ b/src/events/messageDelete.ts @@ -1,11 +1,17 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import Logger from '../helpers/Logger.js'; +import {RawGatewayPacket, RawMessageDelete} from 'src/interfaces'; export default class MessageDelete { static run(client:TClient, msg:Discord.Message){ if (!client.config.botSwitches.logs) return; const disabledChannels = ['548032776830582794', '541677709487505408', '949380187668242483'] if (msg.guild?.id != client.config.dcServer.id || msg.partial || msg.author.bot || disabledChannels.includes(msg.channelId)) return; + + client.on('raw', async (packet:RawGatewayPacket)=>{ + if (packet.t !== 'MESSAGE_DELETE' || packet.d.guild_id != client.config.dcServer.id || disabledChannels.includes(packet.d.channel_id)) return; + }); + if (Discord.DiscordAPIError.name === '10008') return Logger.console('log', 'MsgDelete', 'Caught an unexpected error returned by Discord API. (Unknown Message)'); const embed = new client.embed().setColor(client.config.embedColorRed).setTimestamp().setAuthor({name: `Author: ${msg.author.username} (${msg.author.id})`, iconURL: `${msg.author.displayAvatarURL()}`}).setTitle('Message deleted').setDescription(`<@${msg.author.id}>\n\`${msg.author.id}\``); if (msg.content.length != 0) embed.addFields({name: 'Content', value: `\`\`\`\n${Discord.escapeCodeBlock(msg.content.slice(0,1000))}\n\`\`\``}); diff --git a/src/events/messageUpdate.ts b/src/events/messageUpdate.ts index d57dc52..aad64c0 100644 --- a/src/events/messageUpdate.ts +++ b/src/events/messageUpdate.ts @@ -1,12 +1,20 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import MessageTool from '../helpers/MessageTool.js'; +import {RawGatewayPacket, RawMessageUpdate} from 'src/interfaces'; export default class MessageUpdate { static async run(client:TClient, oldMsg:Discord.Message, newMsg:Discord.Message){ if (!client.config.botSwitches.logs) return; - if (oldMsg.guild?.id != client.config.dcServer.id || oldMsg.author === null || oldMsg?.author.bot || oldMsg.partial || newMsg.partial || !newMsg.member || ['548032776830582794', '541677709487505408', '949380187668242483'].includes(newMsg.channelId)) return; + const disabledChannels = ['548032776830582794', '541677709487505408', '949380187668242483'] + if (oldMsg.guild?.id != client.config.dcServer.id || oldMsg.author === null || oldMsg?.author.bot || oldMsg.partial || newMsg.partial || !newMsg.member || disabledChannels.includes(newMsg.channelId)) return; if (await client.prohibitedWords.findWord(newMsg.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]|[]|ing\b/g, '').split(' ').join('')) && (!MessageTool.isStaff(newMsg.member))) newMsg.delete(); if (newMsg.content === oldMsg.content) return; + + client.on('raw', async (packet:RawGatewayPacket)=>{ + if (packet.t !== 'MESSAGE_UPDATE' || packet.d.guild_id != client.config.dcServer.id || disabledChannels.includes(packet.d.channel_id)) return; + if (typeof packet.d.content === 'undefined' || packet.d.content === oldMsg.content) return; + }); + (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({embeds: [new client.embed().setColor(client.config.embedColor).setTimestamp().setAuthor({name: `Author: ${oldMsg.author.username} (${oldMsg.author.id})`, iconURL: oldMsg.author.displayAvatarURL()}).setTitle('Message edited').setDescription(`<@${oldMsg.author.id}>\n\`${oldMsg.author.id}\``).addFields({name: 'Old content', value: `\`\`\`${oldMsg.content.length < 1 ? '(Attachment)' : Discord.escapeCodeBlock(oldMsg.content.slice(0,2048))}\`\`\``}, {name: 'New content', value: `\`\`\`${Discord.escapeCodeBlock(newMsg.content.slice(0,2048))}\`\`\``}, {name: 'Channel', value: `<#${oldMsg.channelId}>`})], components: [new Discord.ActionRowBuilder().addComponents(new Discord.ButtonBuilder().setStyle(5).setURL(oldMsg.url).setLabel('Jump to message'))]}); } } diff --git a/src/interfaces.d.ts b/src/interfaces.d.ts index c46f3e9..7478562 100644 --- a/src/interfaces.d.ts +++ b/src/interfaces.d.ts @@ -163,3 +163,29 @@ export interface Config { } } } +// Credits to FlyingSixtySix/neurobot for inspiration. +// https://github.com/FlyingSixtySix/neurobot/blob/0dee4ea4f72872e2df240700eb56e1d38da1f8bb/src/interactions/jp.ts#L37-L85 +export interface RawGatewayPacket { + t: 'MESSAGE_DELETE'|'MESSAGE_UPDATE'; + d: T; +} +export interface RawMessageDelete { + id: string, + channel_id: string, + guild_id: string +} +export interface RawMessageUpdate { + id: string, + channel_id: string, + guild_id: string, + content: string, + embeds: any[], + components: any[], + attachments: any[], + author: { + username: string, + id: string, + global_name: string + }, + member: { roles: any[] } +}