Skip to content

Commit

Permalink
Interserver: Implement message edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Woomymy committed Apr 29, 2022
1 parent 7df593f commit 16223bd
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/events/messageUpdate.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<MessageCloneData>) {
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;

0 comments on commit 16223bd

Please # to comment.