From 9b0ae0fd9068e3e6763c1d1e258ef4c7580a48d5 Mon Sep 17 00:00:00 2001 From: jamesbt365 Date: Tue, 2 Sep 2025 03:20:56 +0100 Subject: [PATCH] Translate: support automod & forwarded messages (#3367) Co-authored-by: V --- src/plugins/translate/index.tsx | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/plugins/translate/index.tsx b/src/plugins/translate/index.tsx index 363897d1..1bcc8b3f 100644 --- a/src/plugins/translate/index.tsx +++ b/src/plugins/translate/index.tsx @@ -21,6 +21,7 @@ import "./styles.css"; import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; +import { Message } from "@vencord/discord-types"; import { ChannelStore, Menu } from "@webpack/common"; import { settings } from "./settings"; @@ -28,8 +29,9 @@ import { setShouldShowTranslateEnabledTooltip, TranslateChatBarIcon, TranslateIc import { handleTranslate, TranslationAccessory } from "./TranslationAccessory"; import { translate } from "./utils"; -const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) => { - if (!message.content) return; +const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }: { message: Message; }) => { + const content = getMessageContent(message); + if (!content) return; const group = findGroupChildrenByChildId("copy-text", children); if (!group) return; @@ -40,13 +42,23 @@ const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) => label="Translate" icon={TranslateIcon} action={async () => { - const trans = await translate("received", message.content); + const trans = await translate("received", content); handleTranslate(message.id, trans); }} /> )); }; + +function getMessageContent(message: Message) { + // Message snapshots is an array, which allows for nested snapshots, which Discord does not do yet. + // no point collecting content or rewriting this to render in a certain way that makes sense + // for something currently impossible. + return message.content + || message.messageSnapshots?.[0]?.message.content + || message.embeds?.find(embed => embed.type === "auto_moderation_message")?.rawDescription || ""; +} + let tooltipTimeout: any; export default definePlugin({ @@ -64,8 +76,9 @@ export default definePlugin({ renderChatBarButton: TranslateChatBarIcon, - renderMessagePopoverButton(message) { - if (!message.content) return null; + renderMessagePopoverButton(message: Message) { + const content = getMessageContent(message); + if (!content) return null; return { label: "Translate", @@ -73,7 +86,7 @@ export default definePlugin({ message, channel: ChannelStore.getChannel(message.channel_id), onClick: async () => { - const trans = await translate("received", message.content); + const trans = await translate("received", content); handleTranslate(message.id, trans); } };