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 { 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);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue