From 16223bd92c9419e23506dc81b675a18c774ef0df Mon Sep 17 00:00:00 2001 From: Woomy Date: Fri, 29 Apr 2022 19:14:40 +0200 Subject: [PATCH] Interserver: Implement message edit --- src/events/messageUpdate.ts | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/events/messageUpdate.ts diff --git a/src/events/messageUpdate.ts b/src/events/messageUpdate.ts new file mode 100644 index 0000000..7bc52e5 --- /dev/null +++ b/src/events/messageUpdate.ts @@ -0,0 +1,52 @@ +import { Message, TextChannel } from "discord.js"; +import { InterServerClient } from "../classes/Client"; +import { EventData, MessageCloneData } from "../typings"; +import { INTERSERVER_WH_NAME } from "../utils/constants.js"; + +const data: EventData = { + name: "messageUpdate", + type: "messageUpdate", + callback: async ( + oldmessage: Message, + newmessage: Message + ): Promise => { + if (newmessage.partial) await newmessage.fetch(); + if (newmessage.author.bot) return; + const client = newmessage.client as InterServerClient; + const dbMessage = await client.prisma.message.findUnique({ + where: { id: newmessage.id } + }); + if (!dbMessage) return; + for (const clone of dbMessage.clones as unknown as Array) { + const channel = client.channels.cache.get( + clone.channelId + ) as TextChannel; + if (!channel) continue; + const webHook = (await channel.fetchWebhooks()).find( + (hook) => hook.name === INTERSERVER_WH_NAME + ); + if (webHook) { + try { + webHook.editMessage(clone.id, { + content: + newmessage.cleanContent.length == 0 + ? "​" // Invisible char + : newmessage.cleanContent, + embeds: newmessage.embeds, + allowedMentions: { parse: ["users"] }, + files: newmessage.attachments.map( + (attachement) => attachement.url + ) + }); + } catch (e) { + client.logger.error( + `Can't edit message webhook message ${clone.id} in ${clone.channelId} (original message ${newmessage.id})`, + "messageEditHandler" + ); + } + } + } + } +}; + +export default data;