Translate: support automod & forwarded messages (#3367)
Co-authored-by: V <vendicated@riseup.net>
This commit is contained in:
parent
f0f75aa918
commit
9b0ae0fd90
1 changed files with 19 additions and 6 deletions
|
|
@ -21,6 +21,7 @@ import "./styles.css";
|
||||||
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
|
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin from "@utils/types";
|
||||||
|
import { Message } from "@vencord/discord-types";
|
||||||
import { ChannelStore, Menu } from "@webpack/common";
|
import { ChannelStore, Menu } from "@webpack/common";
|
||||||
|
|
||||||
import { settings } from "./settings";
|
import { settings } from "./settings";
|
||||||
|
|
@ -28,8 +29,9 @@ import { setShouldShowTranslateEnabledTooltip, TranslateChatBarIcon, TranslateIc
|
||||||
import { handleTranslate, TranslationAccessory } from "./TranslationAccessory";
|
import { handleTranslate, TranslationAccessory } from "./TranslationAccessory";
|
||||||
import { translate } from "./utils";
|
import { translate } from "./utils";
|
||||||
|
|
||||||
const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) => {
|
const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }: { message: Message; }) => {
|
||||||
if (!message.content) return;
|
const content = getMessageContent(message);
|
||||||
|
if (!content) return;
|
||||||
|
|
||||||
const group = findGroupChildrenByChildId("copy-text", children);
|
const group = findGroupChildrenByChildId("copy-text", children);
|
||||||
if (!group) return;
|
if (!group) return;
|
||||||
|
|
@ -40,13 +42,23 @@ const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) =>
|
||||||
label="Translate"
|
label="Translate"
|
||||||
icon={TranslateIcon}
|
icon={TranslateIcon}
|
||||||
action={async () => {
|
action={async () => {
|
||||||
const trans = await translate("received", message.content);
|
const trans = await translate("received", content);
|
||||||
handleTranslate(message.id, trans);
|
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;
|
let tooltipTimeout: any;
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
|
|
@ -64,8 +76,9 @@ export default definePlugin({
|
||||||
|
|
||||||
renderChatBarButton: TranslateChatBarIcon,
|
renderChatBarButton: TranslateChatBarIcon,
|
||||||
|
|
||||||
renderMessagePopoverButton(message) {
|
renderMessagePopoverButton(message: Message) {
|
||||||
if (!message.content) return null;
|
const content = getMessageContent(message);
|
||||||
|
if (!content) return null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
label: "Translate",
|
label: "Translate",
|
||||||
|
|
@ -73,7 +86,7 @@ export default definePlugin({
|
||||||
message,
|
message,
|
||||||
channel: ChannelStore.getChannel(message.channel_id),
|
channel: ChannelStore.getChannel(message.channel_id),
|
||||||
onClick: async () => {
|
onClick: async () => {
|
||||||
const trans = await translate("received", message.content);
|
const trans = await translate("received", content);
|
||||||
handleTranslate(message.id, trans);
|
handleTranslate(message.id, trans);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue