generated from federation-interservices-d-informatique/fiibot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |