From b8c760121b395363c27ef1e966390e77c438331c Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Tue, 23 Jan 2024 08:57:07 +1100 Subject: [PATCH] Finetune the raw gateway events --- src/events/messageDelete.ts | 8 +------- src/events/messageUpdate.ts | 15 +++++---------- src/index.ts | 36 +++++++++++++++++++++++++++++++++++- src/interfaces.d.ts | 8 ++------ 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/events/messageDelete.ts b/src/events/messageDelete.ts index 8b604af..11f457f 100644 --- a/src/events/messageDelete.ts +++ b/src/events/messageDelete.ts @@ -1,17 +1,11 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import Logger from '../helpers/Logger.js'; -import {RawGatewayPacket, RawMessageDelete} from 'src/interfaces'; +import {disabledChannels} from '../index.js'; 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 aad64c0..5b38800 100644 --- a/src/events/messageUpdate.ts +++ b/src/events/messageUpdate.ts @@ -1,20 +1,15 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import MessageTool from '../helpers/MessageTool.js'; -import {RawGatewayPacket, RawMessageUpdate} from 'src/interfaces'; +import {disabledChannels, rawSwitches} from '../index.js'; export default class MessageUpdate { static async run(client:TClient, oldMsg:Discord.Message, newMsg:Discord.Message){ if (!client.config.botSwitches.logs) 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'))]}); + if (!rawSwitches.MESSAGE_UPDATE || (rawSwitches.MESSAGE_UPDATE && newMsg.content !== oldMsg.content)) { + rawSwitches.MESSAGE_UPDATE = true; + (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/index.ts b/src/index.ts index 43284cf..8b13228 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,8 +6,9 @@ import Logger from './helpers/Logger.js'; import YTModule from './modules/YTModule.js'; import MPModule, {refreshTimerSecs} from './modules/MPModule.js'; import UsernameHelper from './helpers/UsernameHelper.js'; -import {Punishment} from 'src/interfaces'; +import {Punishment, RawGatewayPacket, RawMessageDelete, RawMessageUpdate} from 'src/interfaces'; import {readFileSync} from 'node:fs'; +export const disabledChannels = ['548032776830582794', '541677709487505408', '949380187668242483']; // Error handler function _(error:Error, type:string) { @@ -69,3 +70,36 @@ if (client.config.botSwitches.dailyMsgsBackup) { client.userLevels.dataSweeper(); } // Cronjob tasks + +// Raw gateway event receivers +export let rawSwitches = { + MESSAGE_UPDATE: false, + MESSAGE_DELETE: false +}; +client.on('raw', async (packet:RawGatewayPacket)=>{ + if (rawSwitches[packet.t]) return; + if (packet.t !== 'MESSAGE_UPDATE') return; + if (packet.d.guild_id != client.config.dcServer.id || disabledChannels.includes(packet.d.channel_id)) return; + if (typeof packet.d.content === 'undefined') return; + + const channel = client.channels.cache.get(packet.d.channel_id) as Discord.TextBasedChannel; + const new_message = await channel.messages.fetch(packet.d.id); + client.emit('messageUpdate', new_message, new_message); +}); + +client.on('raw', async (packet:RawGatewayPacket)=>{ + if (rawSwitches[packet.t]) return; + if (packet.t !== 'MESSAGE_DELETE' || packet.d.guild_id != client.config.dcServer.id || disabledChannels.includes(packet.d.channel_id)) return; + (client.channels.resolve(client.config.dcServer.channels.logs) as Discord.TextChannel).send({ + embeds: [new client.embed() + .setColor(client.config.embedColorRed) + .setTitle('Message deleted') + .setDescription('Unknown author') + .addFields( + {name: 'Received over raw API gateway', value: '\u200b'}, + {name: 'Channel', value: `<#${packet.d.channel_id}>`}, + ).setTimestamp() + ] + }); + rawSwitches[packet.t] = true; +}); diff --git a/src/interfaces.d.ts b/src/interfaces.d.ts index 7478562..67e4e10 100644 --- a/src/interfaces.d.ts +++ b/src/interfaces.d.ts @@ -1,4 +1,4 @@ -import {ColorResolvable, PresenceData} from 'discord.js'; +import {ColorResolvable, PresenceData, APIUser} from 'discord.js'; export interface Punishment { case_id: number; @@ -182,10 +182,6 @@ export interface RawMessageUpdate { embeds: any[], components: any[], attachments: any[], - author: { - username: string, - id: string, - global_name: string - }, + author: APIUser member: { roles: any[] } }