From 5da1c261632c118bfc03556b2483ee5d5c2358ba Mon Sep 17 00:00:00 2001 From: Vendicated Date: Thu, 21 Nov 2024 04:11:23 +0100 Subject: [PATCH] migrate plugins --- src/plugins/_core/supportHelper.tsx | 161 ++++++++++---------- src/plugins/clearURLs/index.ts | 28 ++-- src/plugins/hideAttachments/index.tsx | 31 ++-- src/plugins/invisibleChat.desktop/index.tsx | 48 +++--- src/plugins/messageClickActions/index.ts | 101 ++++++------ src/plugins/quickMention/index.tsx | 27 ++-- src/plugins/sendTimestamps/index.tsx | 21 +-- src/plugins/silentMessageToggle/index.tsx | 6 +- src/plugins/silentTyping/index.tsx | 8 +- src/plugins/textReplace/index.tsx | 18 +-- src/plugins/translate/index.tsx | 68 ++++----- src/plugins/unindent/index.ts | 14 +- src/plugins/viewRaw/index.tsx | 69 ++++----- 13 files changed, 264 insertions(+), 336 deletions(-) diff --git a/src/plugins/_core/supportHelper.tsx b/src/plugins/_core/supportHelper.tsx index 12dca2661..125dfebc2 100644 --- a/src/plugins/_core/supportHelper.tsx +++ b/src/plugins/_core/supportHelper.tsx @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import { addMessageAccessory } from "@api/MessageAccessories"; import { definePluginSettings } from "@api/Settings"; import { getUserSettingLazy } from "@api/UserSettings"; import ErrorBoundary from "@components/ErrorBoundary"; @@ -235,6 +234,85 @@ export default definePlugin({ } }, + renderMessageAccessory(props) { + const buttons = [] as JSX.Element[]; + + const shouldAddUpdateButton = + !IS_UPDATER_DISABLED + && ( + (props.channel.id === KNOWN_ISSUES_CHANNEL_ID) || + (props.channel.id === SUPPORT_CHANNEL_ID && props.message.author.id === VENBOT_USER_ID) + ) + && props.message.content?.includes("update"); + + if (shouldAddUpdateButton) { + buttons.push( + + ); + } + + if (props.channel.id === SUPPORT_CHANNEL_ID) { + if (props.message.content.includes("/vencord-debug") || props.message.content.includes("/vencord-plugins")) { + buttons.push( + , + + ); + } + + if (props.message.author.id === VENBOT_USER_ID) { + const match = CodeBlockRe.exec(props.message.content || props.message.embeds[0]?.rawDescription || ""); + if (match) { + buttons.push( + + ); + } + } + } + + return buttons.length + ? {buttons} + : null; + }, + renderContributorDmWarningCard: ErrorBoundary.wrap(({ channel }) => { const userId = channel.getRecipientId(); if (!isPluginDev(userId)) return null; @@ -249,85 +327,4 @@ export default definePlugin({ ); }, { noop: true }), - - start() { - addMessageAccessory("vencord-debug", props => { - const buttons = [] as JSX.Element[]; - - const shouldAddUpdateButton = - !IS_UPDATER_DISABLED - && ( - (props.channel.id === KNOWN_ISSUES_CHANNEL_ID) || - (props.channel.id === SUPPORT_CHANNEL_ID && props.message.author.id === VENBOT_USER_ID) - ) - && props.message.content?.includes("update"); - - if (shouldAddUpdateButton) { - buttons.push( - - ); - } - - if (props.channel.id === SUPPORT_CHANNEL_ID) { - if (props.message.content.includes("/vencord-debug") || props.message.content.includes("/vencord-plugins")) { - buttons.push( - , - - ); - } - - if (props.message.author.id === VENBOT_USER_ID) { - const match = CodeBlockRe.exec(props.message.content || props.message.embeds[0]?.rawDescription || ""); - if (match) { - buttons.push( - - ); - } - } - } - - return buttons.length - ? {buttons} - : null; - }); - }, }); diff --git a/src/plugins/clearURLs/index.ts b/src/plugins/clearURLs/index.ts index d1be6c6f5..398eaf2fe 100644 --- a/src/plugins/clearURLs/index.ts +++ b/src/plugins/clearURLs/index.ts @@ -17,11 +17,7 @@ */ import { - addPreEditListener, - addPreSendListener, - MessageObject, - removePreEditListener, - removePreSendListener + MessageObject } from "@api/MessageEvents"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; @@ -36,7 +32,14 @@ export default definePlugin({ name: "ClearURLs", description: "Removes tracking garbage from URLs", authors: [Devs.adryd], - dependencies: ["MessageEventsAPI"], + + onBeforeMessageSend(_, msg) { + return this.onSend(msg); + }, + + onBeforeMessageEdit(_cid, _mid, msg) { + return this.onSend(msg); + }, escapeRegExp(str: string) { return (str && reHasRegExpChar.test(str)) @@ -133,17 +136,4 @@ export default definePlugin({ ); } }, - - start() { - this.createRules(); - this.preSend = addPreSendListener((_, msg) => this.onSend(msg)); - this.preEdit = addPreEditListener((_cid, _mid, msg) => - this.onSend(msg) - ); - }, - - stop() { - removePreSendListener(this.preSend); - removePreEditListener(this.preEdit); - }, }); diff --git a/src/plugins/hideAttachments/index.tsx b/src/plugins/hideAttachments/index.tsx index 253e3d3be..e122e3cb5 100644 --- a/src/plugins/hideAttachments/index.tsx +++ b/src/plugins/hideAttachments/index.tsx @@ -17,7 +17,6 @@ */ import { get, set } from "@api/DataStore"; -import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; import { ImageInvisible, ImageVisible } from "@components/Icons"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; @@ -38,7 +37,20 @@ export default definePlugin({ name: "HideAttachments", description: "Hide attachments and Embeds for individual messages via hover button", authors: [Devs.Ven], - dependencies: ["MessagePopoverAPI"], + + renderMessagePopoverButton(msg) { + if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length) return null; + + const isHidden = hiddenMessages.has(msg.id); + + return { + label: isHidden ? "Show Attachments" : "Hide Attachments", + icon: isHidden ? ImageVisible : ImageInvisible, + message: msg, + channel: ChannelStore.getChannel(msg.channel_id), + onClick: () => this.toggleHide(msg.id) + }; + }, async start() { style = document.createElement("style"); @@ -47,26 +59,11 @@ export default definePlugin({ await getHiddenMessages(); await this.buildCss(); - - addMessagePopoverButton("HideAttachments", msg => { - if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length) return null; - - const isHidden = hiddenMessages.has(msg.id); - - return { - label: isHidden ? "Show Attachments" : "Hide Attachments", - icon: isHidden ? ImageVisible : ImageInvisible, - message: msg, - channel: ChannelStore.getChannel(msg.channel_id), - onClick: () => this.toggleHide(msg.id) - }; - }); }, stop() { style.remove(); hiddenMessages.clear(); - removeMessagePopoverButton("HideAttachments"); }, async buildCss() { diff --git a/src/plugins/invisibleChat.desktop/index.tsx b/src/plugins/invisibleChat.desktop/index.tsx index 3862f18e4..d6a39cbaf 100644 --- a/src/plugins/invisibleChat.desktop/index.tsx +++ b/src/plugins/invisibleChat.desktop/index.tsx @@ -16,8 +16,7 @@ * along with this program. If not, see . */ -import { addChatBarButton, ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; -import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { updateMessage } from "@api/MessageUpdater"; import { definePluginSettings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; @@ -104,7 +103,7 @@ export default definePlugin({ name: "InvisibleChat", description: "Encrypt your Messages in a non-suspicious way!", authors: [Devs.SammCheese], - dependencies: ["MessagePopoverAPI", "ChatInputButtonAPI", "MessageUpdaterAPI"], + dependencies: ["MessageUpdaterAPI"], reporterTestable: ReporterTestable.Patches, settings, @@ -125,36 +124,31 @@ export default definePlugin({ /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/, ), async start() { - addMessagePopoverButton("InvisibleChat", message => { - return this.INV_REGEX.test(message?.content) - ? { - label: "Decrypt Message", - icon: this.popOverIcon, - message: message, - channel: ChannelStore.getChannel(message.channel_id), - onClick: async () => { - const res = await iteratePasswords(message); - - if (res) - this.buildEmbed(message, res); - else - buildDecModal({ message }); - } - } - : null; - }); - - addChatBarButton("InvisibleChat", ChatBarIcon); - const { default: StegCloak } = await getStegCloak(); steggo = new StegCloak(true, false); }, - stop() { - removeMessagePopoverButton("InvisibleChat"); - removeMessagePopoverButton("InvisibleChat"); + renderMessagePopoverButton(message) { + return this.INV_REGEX.test(message?.content) + ? { + label: "Decrypt Message", + icon: this.popOverIcon, + message: message, + channel: ChannelStore.getChannel(message.channel_id), + onClick: async () => { + const res = await iteratePasswords(message); + + if (res) + this.buildEmbed(message, res); + else + buildDecModal({ message }); + } + } + : null; }, + renderChatBarButton: ChatBarIcon, + // Gets the Embed of a Link async getEmbed(url: URL): Promise { const { body } = await RestAPI.post({ diff --git a/src/plugins/messageClickActions/index.ts b/src/plugins/messageClickActions/index.ts index 5c32cb9b1..19ccaa955 100644 --- a/src/plugins/messageClickActions/index.ts +++ b/src/plugins/messageClickActions/index.ts @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import { addMessageClickListener, removeMessageClickListener } from "@api/MessageEvents"; import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; @@ -57,66 +56,64 @@ export default definePlugin({ name: "MessageClickActions", description: "Hold Backspace and click to delete, double click to edit/reply", authors: [Devs.Ven], - dependencies: ["MessageEventsAPI"], settings, start() { document.addEventListener("keydown", keydown); document.addEventListener("keyup", keyup); - - this.onClick = addMessageClickListener((msg: any, channel, event) => { - const isMe = msg.author.id === UserStore.getCurrentUser().id; - if (!isDeletePressed) { - if (event.detail < 2) return; - if (settings.store.requireModifier && !event.ctrlKey && !event.shiftKey) return; - if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return; - if (msg.deleted === true) return; - - if (isMe) { - if (!settings.store.enableDoubleClickToEdit || EditStore.isEditing(channel.id, msg.id) || msg.state !== "SENT") return; - - MessageActions.startEditMessage(channel.id, msg.id, msg.content); - event.preventDefault(); - } else { - if (!settings.store.enableDoubleClickToReply) return; - - const EPHEMERAL = 64; - if (msg.hasFlag(EPHEMERAL)) return; - - const isShiftPress = event.shiftKey && !settings.store.requireModifier; - const NoReplyMention = Vencord.Plugins.plugins.NoReplyMention as any as typeof import("../noReplyMention").default; - const shouldMention = Vencord.Plugins.isPluginEnabled("NoReplyMention") - ? NoReplyMention.shouldMention(msg, isShiftPress) - : !isShiftPress; - - FluxDispatcher.dispatch({ - type: "CREATE_PENDING_REPLY", - channel, - message: msg, - shouldMention, - showMentionToggle: channel.guild_id !== null - }); - } - } else if (settings.store.enableDeleteOnClick && (isMe || PermissionStore.can(PermissionsBits.MANAGE_MESSAGES, channel))) { - if (msg.deleted) { - FluxDispatcher.dispatch({ - type: "MESSAGE_DELETE", - channelId: channel.id, - id: msg.id, - mlDeleted: true - }); - } else { - MessageActions.deleteMessage(channel.id, msg.id); - } - event.preventDefault(); - } - }); }, stop() { - removeMessageClickListener(this.onClick); document.removeEventListener("keydown", keydown); document.removeEventListener("keyup", keyup); - } + }, + + onMessageClick(msg: any, channel, event) { + const isMe = msg.author.id === UserStore.getCurrentUser().id; + if (!isDeletePressed) { + if (event.detail < 2) return; + if (settings.store.requireModifier && !event.ctrlKey && !event.shiftKey) return; + if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return; + if (msg.deleted === true) return; + + if (isMe) { + if (!settings.store.enableDoubleClickToEdit || EditStore.isEditing(channel.id, msg.id) || msg.state !== "SENT") return; + + MessageActions.startEditMessage(channel.id, msg.id, msg.content); + event.preventDefault(); + } else { + if (!settings.store.enableDoubleClickToReply) return; + + const EPHEMERAL = 64; + if (msg.hasFlag(EPHEMERAL)) return; + + const isShiftPress = event.shiftKey && !settings.store.requireModifier; + const NoReplyMention = Vencord.Plugins.plugins.NoReplyMention as any as typeof import("../noReplyMention").default; + const shouldMention = Vencord.Plugins.isPluginEnabled("NoReplyMention") + ? NoReplyMention.shouldMention(msg, isShiftPress) + : !isShiftPress; + + FluxDispatcher.dispatch({ + type: "CREATE_PENDING_REPLY", + channel, + message: msg, + shouldMention, + showMentionToggle: channel.guild_id !== null + }); + } + } else if (settings.store.enableDeleteOnClick && (isMe || PermissionStore.can(PermissionsBits.MANAGE_MESSAGES, channel))) { + if (msg.deleted) { + FluxDispatcher.dispatch({ + type: "MESSAGE_DELETE", + channelId: channel.id, + id: msg.id, + mlDeleted: true + }); + } else { + MessageActions.deleteMessage(channel.id, msg.id); + } + event.preventDefault(); + } + }, }); diff --git a/src/plugins/quickMention/index.tsx b/src/plugins/quickMention/index.tsx index 2c7e7f6f3..8d275354f 100644 --- a/src/plugins/quickMention/index.tsx +++ b/src/plugins/quickMention/index.tsx @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; import { Devs } from "@utils/constants"; import { insertTextIntoChatInputBox } from "@utils/discord"; import definePlugin from "@utils/types"; @@ -26,24 +25,18 @@ export default definePlugin({ name: "QuickMention", authors: [Devs.kemo], description: "Adds a quick mention button to the message actions bar", - dependencies: ["MessagePopoverAPI"], - start() { - addMessagePopoverButton("QuickMention", msg => { - const channel = ChannelStore.getChannel(msg.channel_id); - if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return null; + renderMessagePopoverButton(msg) { + const channel = ChannelStore.getChannel(msg.channel_id); + if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return null; - return { - label: "Quick Mention", - icon: this.Icon, - message: msg, - channel, - onClick: () => insertTextIntoChatInputBox(`<@${msg.author.id}> `) - }; - }); - }, - stop() { - removeMessagePopoverButton("QuickMention"); + return { + label: "Quick Mention", + icon: this.Icon, + message: msg, + channel, + onClick: () => insertTextIntoChatInputBox(`<@${msg.author.id}> `) + }; }, Icon: () => ( diff --git a/src/plugins/sendTimestamps/index.tsx b/src/plugins/sendTimestamps/index.tsx index 487fba668..4600e0480 100644 --- a/src/plugins/sendTimestamps/index.tsx +++ b/src/plugins/sendTimestamps/index.tsx @@ -18,8 +18,7 @@ import "./styles.css"; -import { addChatBarButton, ChatBarButton, ChatBarButtonFactory, removeChatBarButton } from "@api/ChatButtons"; -import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { definePluginSettings } from "@api/Settings"; import { classNameFactory } from "@api/Styles"; import { Devs } from "@utils/constants"; @@ -160,22 +159,14 @@ export default definePlugin({ name: "SendTimestamps", description: "Send timestamps easily via chat box button & text shortcuts. Read the extended description!", authors: [Devs.Ven, Devs.Tyler, Devs.Grzesiek11], - dependencies: ["MessageEventsAPI", "ChatInputButtonAPI"], - settings, - start() { - addChatBarButton("SendTimestamps", ChatBarIcon); - this.listener = addPreSendListener((_, msg) => { - if (settings.store.replaceMessageContents) { - msg.content = msg.content.replace(/`\d{1,2}:\d{2} ?(?:AM|PM)?`/gi, parseTime); - } - }); - }, + renderChatBarButton: ChatBarIcon, - stop() { - removeChatBarButton("SendTimestamps"); - removePreSendListener(this.listener); + onBeforeMessageSend(_, msg) { + if (settings.store.replaceMessageContents) { + msg.content = msg.content.replace(/`\d{1,2}:\d{2} ?(?:AM|PM)?`/gi, parseTime); + } }, settingsAboutComponent() { diff --git a/src/plugins/silentMessageToggle/index.tsx b/src/plugins/silentMessageToggle/index.tsx index 8ae0cbb3d..d0ae7857f 100644 --- a/src/plugins/silentMessageToggle/index.tsx +++ b/src/plugins/silentMessageToggle/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { addChatBarButton, ChatBarButton, ChatBarButtonFactory, removeChatBarButton } from "@api/ChatButtons"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { addPreSendListener, MessageSendListener, removePreSendListener } from "@api/MessageEvents"; import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; @@ -91,9 +91,7 @@ export default definePlugin({ name: "SilentMessageToggle", authors: [Devs.Nuckyz, Devs.CatNoir], description: "Adds a button to the chat bar to toggle sending a silent message.", - dependencies: ["MessageEventsAPI", "ChatInputButtonAPI"], settings, - start: () => addChatBarButton("SilentMessageToggle", SilentMessageToggle), - stop: () => removeChatBarButton("SilentMessageToggle") + renderChatBarButton: SilentMessageToggle, }); diff --git a/src/plugins/silentTyping/index.tsx b/src/plugins/silentTyping/index.tsx index 6de9d96c7..85a067e61 100644 --- a/src/plugins/silentTyping/index.tsx +++ b/src/plugins/silentTyping/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { addChatBarButton, ChatBarButton, ChatBarButtonFactory, removeChatBarButton } from "@api/ChatButtons"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { ApplicationCommandInputType, ApplicationCommandOptionType, findOption, sendBotMessage } from "@api/Commands"; import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu"; import { definePluginSettings } from "@api/Settings"; @@ -88,11 +88,12 @@ export default definePlugin({ name: "SilentTyping", authors: [Devs.Ven, Devs.Rini, Devs.ImBanana], description: "Hide that you are typing", - dependencies: ["ChatInputButtonAPI"], settings, + contextMenus: { "textarea-context": ChatBarContextCheckbox }, + patches: [ { find: '.dispatch({type:"TYPING_START_LOCAL"', @@ -128,6 +129,5 @@ export default definePlugin({ FluxDispatcher.dispatch({ type: "TYPING_START_LOCAL", channelId }); }, - start: () => addChatBarButton("SilentTyping", SilentTypingToggle), - stop: () => removeChatBarButton("SilentTyping"), + renderChatBarButton: SilentTypingToggle, }); diff --git a/src/plugins/textReplace/index.tsx b/src/plugins/textReplace/index.tsx index 615477d07..bf5d62836 100644 --- a/src/plugins/textReplace/index.tsx +++ b/src/plugins/textReplace/index.tsx @@ -17,7 +17,6 @@ */ import { DataStore } from "@api/index"; -import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; import { definePluginSettings } from "@api/Settings"; import { Flex } from "@components/Flex"; import { DeleteIcon } from "@components/Icons"; @@ -244,22 +243,17 @@ export default definePlugin({ name: "TextReplace", description: "Replace text in your messages. You can find pre-made rules in the #textreplace-rules channel in Vencord's Server", authors: [Devs.AutumnVN, Devs.TheKodeToad], - dependencies: ["MessageEventsAPI"], settings, + onBeforeMessageSend(channelId, msg) { + // Channel used for sharing rules, applying rules here would be messy + if (channelId === TEXT_REPLACE_RULES_CHANNEL_ID) return; + msg.content = applyRules(msg.content); + }, + async start() { stringRules = await DataStore.get(STRING_RULES_KEY) ?? makeEmptyRuleArray(); regexRules = await DataStore.get(REGEX_RULES_KEY) ?? makeEmptyRuleArray(); - - this.preSend = addPreSendListener((channelId, msg) => { - // Channel used for sharing rules, applying rules here would be messy - if (channelId === TEXT_REPLACE_RULES_CHANNEL_ID) return; - msg.content = applyRules(msg.content); - }); - }, - - stop() { - removePreSendListener(this.preSend); } }); diff --git a/src/plugins/translate/index.tsx b/src/plugins/translate/index.tsx index ed667ef33..363897d1b 100644 --- a/src/plugins/translate/index.tsx +++ b/src/plugins/translate/index.tsx @@ -18,11 +18,7 @@ import "./styles.css"; -import { addChatBarButton, removeChatBarButton } from "@api/ChatButtons"; import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu"; -import { addMessageAccessory, removeMessageAccessory } from "@api/MessageAccessories"; -import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; -import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; import { ChannelStore, Menu } from "@webpack/common"; @@ -51,11 +47,12 @@ const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) => )); }; +let tooltipTimeout: any; + export default definePlugin({ name: "Translate", description: "Translate messages with Google Translate or DeepL", authors: [Devs.Ven, Devs.AshtonMemer], - dependencies: ["MessageAccessoriesAPI", "MessagePopoverAPI", "MessageEventsAPI", "ChatInputButtonAPI"], settings, contextMenus: { "message": messageCtxPatch @@ -63,45 +60,34 @@ export default definePlugin({ // not used, just here in case some other plugin wants it or w/e translate, - start() { - addMessageAccessory("vc-translation", props => ); + renderMessageAccessory: props => , - addChatBarButton("vc-translate", TranslateChatBarIcon); + renderChatBarButton: TranslateChatBarIcon, - addMessagePopoverButton("vc-translate", message => { - if (!message.content) return null; + renderMessagePopoverButton(message) { + if (!message.content) return null; - return { - label: "Translate", - icon: TranslateIcon, - message, - channel: ChannelStore.getChannel(message.channel_id), - onClick: async () => { - const trans = await translate("received", message.content); - handleTranslate(message.id, trans); - } - }; - }); - - let tooltipTimeout: any; - this.preSend = addPreSendListener(async (_, message) => { - if (!settings.store.autoTranslate) return; - if (!message.content) return; - - setShouldShowTranslateEnabledTooltip?.(true); - clearTimeout(tooltipTimeout); - tooltipTimeout = setTimeout(() => setShouldShowTranslateEnabledTooltip?.(false), 2000); - - const trans = await translate("sent", message.content); - message.content = trans.text; - - }); + return { + label: "Translate", + icon: TranslateIcon, + message, + channel: ChannelStore.getChannel(message.channel_id), + onClick: async () => { + const trans = await translate("received", message.content); + handleTranslate(message.id, trans); + } + }; }, - stop() { - removePreSendListener(this.preSend); - removeChatBarButton("vc-translate"); - removeMessagePopoverButton("vc-translate"); - removeMessageAccessory("vc-translation"); - }, + async onBeforeMessageSend(_, message) { + if (!settings.store.autoTranslate) return; + if (!message.content) return; + + setShouldShowTranslateEnabledTooltip?.(true); + clearTimeout(tooltipTimeout); + tooltipTimeout = setTimeout(() => setShouldShowTranslateEnabledTooltip?.(false), 2000); + + const trans = await translate("sent", message.content); + message.content = trans.text; + } }); diff --git a/src/plugins/unindent/index.ts b/src/plugins/unindent/index.ts index a197ef4e9..d8853a93f 100644 --- a/src/plugins/unindent/index.ts +++ b/src/plugins/unindent/index.ts @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { addPreEditListener, addPreSendListener, MessageObject, removePreEditListener, removePreSendListener } from "@api/MessageEvents"; +import { MessageObject } from "@api/MessageEvents"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; @@ -24,7 +24,7 @@ export default definePlugin({ name: "Unindent", description: "Trims leading indentation from codeblocks", authors: [Devs.Ven], - dependencies: ["MessageEventsAPI"], + patches: [ { find: "inQuote:", @@ -55,13 +55,11 @@ export default definePlugin({ }); }, - start() { - this.preSend = addPreSendListener((_, msg) => this.unindentMsg(msg)); - this.preEdit = addPreEditListener((_cid, _mid, msg) => this.unindentMsg(msg)); + onBeforeMessageSend(_, msg) { + return this.unindentMsg(msg); }, - stop() { - removePreSendListener(this.preSend); - removePreEditListener(this.preEdit); + onBeforeMessageEdit(_cid, _mid, msg) { + return this.unindentMsg(msg); } }); diff --git a/src/plugins/viewRaw/index.tsx b/src/plugins/viewRaw/index.tsx index b0764cd59..b45919a21 100644 --- a/src/plugins/viewRaw/index.tsx +++ b/src/plugins/viewRaw/index.tsx @@ -17,7 +17,6 @@ */ import { NavContextMenuPatchCallback } from "@api/ContextMenu"; -import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; import { definePluginSettings } from "@api/Settings"; import { CodeBlock } from "@components/CodeBlock"; import ErrorBoundary from "@components/ErrorBoundary"; @@ -149,8 +148,8 @@ export default definePlugin({ name: "ViewRaw", description: "Copy and view the raw content/data of any message, channel or guild", authors: [Devs.KingFish, Devs.Ven, Devs.rad, Devs.ImLvna], - dependencies: ["MessagePopoverAPI"], settings, + contextMenus: { "guild-context": MakeContextCallback("Guild"), "channel-context": MakeContextCallback("Channel"), @@ -159,44 +158,38 @@ export default definePlugin({ "user-context": MakeContextCallback("User") }, - start() { - addMessagePopoverButton("ViewRaw", msg => { - const handleClick = () => { - if (settings.store.clickMethod === "Right") { - copyWithToast(msg.content); - } else { - openViewRawModalMessage(msg); - } - }; + renderMessagePopoverButton(msg) { + const handleClick = () => { + if (settings.store.clickMethod === "Right") { + copyWithToast(msg.content); + } else { + openViewRawModalMessage(msg); + } + }; - const handleContextMenu = e => { - if (settings.store.clickMethod === "Left") { - e.preventDefault(); - e.stopPropagation(); - copyWithToast(msg.content); - } else { - e.preventDefault(); - e.stopPropagation(); - openViewRawModalMessage(msg); - } - }; + const handleContextMenu = e => { + if (settings.store.clickMethod === "Left") { + e.preventDefault(); + e.stopPropagation(); + copyWithToast(msg.content); + } else { + e.preventDefault(); + e.stopPropagation(); + openViewRawModalMessage(msg); + } + }; - const label = settings.store.clickMethod === "Right" - ? "Copy Raw (Left Click) / View Raw (Right Click)" - : "View Raw (Left Click) / Copy Raw (Right Click)"; + const label = settings.store.clickMethod === "Right" + ? "Copy Raw (Left Click) / View Raw (Right Click)" + : "View Raw (Left Click) / Copy Raw (Right Click)"; - return { - label, - icon: CopyIcon, - message: msg, - channel: ChannelStore.getChannel(msg.channel_id), - onClick: handleClick, - onContextMenu: handleContextMenu - }; - }); - }, - - stop() { - removeMessagePopoverButton("ViewRaw"); + return { + label, + icon: CopyIcon, + message: msg, + channel: ChannelStore.getChannel(msg.channel_id), + onClick: handleClick, + onContextMenu: handleContextMenu + }; } });