diff --git a/src/api/Badges.ts b/src/api/Badges.ts index 7a041f1ee..ee2f3a30c 100644 --- a/src/api/Badges.ts +++ b/src/api/Badges.ts @@ -57,7 +57,7 @@ const Badges = new Set(); * Register a new badge with the Badges API * @param badge The badge to register */ -export function addBadge(badge: ProfileBadge) { +export function addProfileBadge(badge: ProfileBadge) { badge.component &&= ErrorBoundary.wrap(badge.component, { noop: true }); Badges.add(badge); } @@ -66,7 +66,7 @@ export function addBadge(badge: ProfileBadge) { * Unregister a badge from the Badges API * @param badge The badge to remove */ -export function removeBadge(badge: ProfileBadge) { +export function removeProfileBadge(badge: ProfileBadge) { return Badges.delete(badge); } @@ -100,20 +100,3 @@ export interface BadgeUserArgs { userId: string; guildId: string; } - -interface ConnectedAccount { - type: string; - id: string; - name: string; - verified: boolean; -} - -interface Profile { - connectedAccounts: ConnectedAccount[]; - premiumType: number; - premiumSince: string; - premiumGuildSince?: any; - lastFetched: number; - profileFetchFailed: boolean; - application?: any; -} diff --git a/src/api/ChatButtons.tsx b/src/api/ChatButtons.tsx index d38f4ff50..c24e3886f 100644 --- a/src/api/ChatButtons.tsx +++ b/src/api/ChatButtons.tsx @@ -74,9 +74,9 @@ export interface ChatBarProps { }; } -export type ChatBarButton = (props: ChatBarProps & { isMainChat: boolean; }) => JSX.Element | null; +export type ChatBarButtonFactory = (props: ChatBarProps & { isMainChat: boolean; }) => JSX.Element | null; -const buttonFactories = new Map(); +const buttonFactories = new Map(); const logger = new Logger("ChatButtons"); export function _injectButtons(buttons: ReactNode[], props: ChatBarProps) { @@ -91,7 +91,7 @@ export function _injectButtons(buttons: ReactNode[], props: ChatBarProps) { } } -export const addChatBarButton = (id: string, button: ChatBarButton) => buttonFactories.set(id, button); +export const addChatBarButton = (id: string, button: ChatBarButtonFactory) => buttonFactories.set(id, button); export const removeChatBarButton = (id: string) => buttonFactories.delete(id); export interface ChatBarButtonProps { diff --git a/src/api/MemberListDecorators.ts b/src/api/MemberListDecorators.tsx similarity index 61% rename from src/api/MemberListDecorators.ts rename to src/api/MemberListDecorators.tsx index ba5ec8d14..2199f4a6c 100644 --- a/src/api/MemberListDecorators.ts +++ b/src/api/MemberListDecorators.tsx @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import ErrorBoundary from "@components/ErrorBoundary"; import { Channel, User } from "discord-types/general/index.js"; import { JSX } from "react"; @@ -39,27 +40,32 @@ interface DecoratorProps { user: User; [key: string]: any; } -export type Decorator = (props: DecoratorProps) => JSX.Element | null; +export type MemberListDecoratorFactory = (props: DecoratorProps) => JSX.Element | null; type OnlyIn = "guilds" | "dms"; -export const decorators = new Map(); +export const decorators = new Map(); -export function addDecorator(identifier: string, decorator: Decorator, onlyIn?: OnlyIn) { - decorators.set(identifier, { decorator, onlyIn }); +export function addMemberListDecorator(identifier: string, render: MemberListDecoratorFactory, onlyIn?: OnlyIn) { + decorators.set(identifier, { render, onlyIn }); } -export function removeDecorator(identifier: string) { +export function removeMemberListDecorator(identifier: string) { decorators.delete(identifier); } export function __getDecorators(props: DecoratorProps): (JSX.Element | null)[] { const isInGuild = !!(props.guildId); - return Array.from(decorators.values(), decoratorObj => { - const { decorator, onlyIn } = decoratorObj; - // this can most likely be done cleaner - if (!onlyIn || (onlyIn === "guilds" && isInGuild) || (onlyIn === "dms" && !isInGuild)) { - return decorator(props); + return Array.from( + decorators.entries(), + ([key, { render: Decorator, onlyIn }]) => { + if ((onlyIn === "guilds" && !isInGuild) || (onlyIn === "dms" && isInGuild)) + return null; + + return ( + + + + ); } - return null; - }); + ); } diff --git a/src/api/MessageAccessories.ts b/src/api/MessageAccessories.tsx similarity index 63% rename from src/api/MessageAccessories.ts rename to src/api/MessageAccessories.tsx index 8454732f4..71664e93a 100644 --- a/src/api/MessageAccessories.ts +++ b/src/api/MessageAccessories.tsx @@ -16,28 +16,29 @@ * along with this program. If not, see . */ -import { JSX } from "react"; +import ErrorBoundary from "@components/ErrorBoundary"; +import { JSX, ReactNode } from "react"; -export type AccessoryCallback = (props: Record) => JSX.Element | null | Array; -export type Accessory = { - callback: AccessoryCallback; +export type MessageAccessoryFactory = (props: Record) => ReactNode; +export type MessageAccessory = { + render: MessageAccessoryFactory; position?: number; }; -export const accessories = new Map(); +export const accessories = new Map(); -export function addAccessory( +export function addMessageAccessory( identifier: string, - callback: AccessoryCallback, + render: MessageAccessoryFactory, position?: number ) { accessories.set(identifier, { - callback, + render, position, }); } -export function removeAccessory(identifier: string) { +export function removeMessageAccessory(identifier: string) { accessories.delete(identifier); } @@ -45,15 +46,12 @@ export function _modifyAccessories( elements: JSX.Element[], props: Record ) { - for (const accessory of accessories.values()) { - let accessories = accessory.callback(props); - if (accessories == null) - continue; - - if (!Array.isArray(accessories)) - accessories = [accessories]; - else if (accessories.length === 0) - continue; + for (const [key, accessory] of accessories.entries()) { + const res = ( + + + + ); elements.splice( accessory.position != null @@ -62,7 +60,7 @@ export function _modifyAccessories( : accessory.position : elements.length, 0, - ...accessories.filter(e => e != null) as JSX.Element[] + res ); } diff --git a/src/api/MessageDecorations.ts b/src/api/MessageDecorations.tsx similarity index 65% rename from src/api/MessageDecorations.ts rename to src/api/MessageDecorations.tsx index 0d69ab11c..740c95876 100644 --- a/src/api/MessageDecorations.ts +++ b/src/api/MessageDecorations.tsx @@ -16,10 +16,11 @@ * along with this program. If not, see . */ +import ErrorBoundary from "@components/ErrorBoundary"; import { Channel, Message } from "discord-types/general/index.js"; import { JSX } from "react"; -interface DecorationProps { +export interface MessageDecorationProps { author: { /** * Will be username if the user has no nickname @@ -45,20 +46,25 @@ interface DecorationProps { message: Message; [key: string]: any; } -export type Decoration = (props: DecorationProps) => JSX.Element | null; +export type MessageDecorationFactory = (props: MessageDecorationProps) => JSX.Element | null; -export const decorations = new Map(); +export const decorations = new Map(); -export function addDecoration(identifier: string, decoration: Decoration) { +export function addMessageDecoration(identifier: string, decoration: MessageDecorationFactory) { decorations.set(identifier, decoration); } -export function removeDecoration(identifier: string) { +export function removeMessageDecoration(identifier: string) { decorations.delete(identifier); } -export function __addDecorationsToMessage(props: DecorationProps): (JSX.Element | null)[] { - return [...decorations.values()].map(decoration => { - return decoration(props); - }); +export function __addDecorationsToMessage(props: MessageDecorationProps): (JSX.Element | null)[] { + return Array.from( + decorations.entries(), + ([key, Decoration]) => ( + + + + ) + ); } diff --git a/src/api/MessageEvents.ts b/src/api/MessageEvents.ts index d6eba748f..1b55ff340 100644 --- a/src/api/MessageEvents.ts +++ b/src/api/MessageEvents.ts @@ -73,11 +73,11 @@ export interface MessageExtra { openWarningPopout: (props: any) => any; } -export type SendListener = (channelId: string, messageObj: MessageObject, extra: MessageExtra) => Promisable; -export type EditListener = (channelId: string, messageId: string, messageObj: MessageObject) => Promisable; +export type MessageSendListener = (channelId: string, messageObj: MessageObject, extra: MessageExtra) => Promisable; +export type MessageEditListener = (channelId: string, messageId: string, messageObj: MessageObject) => Promisable; -const sendListeners = new Set(); -const editListeners = new Set(); +const sendListeners = new Set(); +const editListeners = new Set(); export async function _handlePreSend(channelId: string, messageObj: MessageObject, extra: MessageExtra, replyOptions: MessageReplyOptions) { extra.replyOptions = replyOptions; @@ -111,29 +111,29 @@ export async function _handlePreEdit(channelId: string, messageId: string, messa /** * Note: This event fires off before a message is sent, allowing you to edit the message. */ -export function addPreSendListener(listener: SendListener) { +export function addMessagePreSendListener(listener: MessageSendListener) { sendListeners.add(listener); return listener; } /** * Note: This event fires off before a message's edit is applied, allowing you to further edit the message. */ -export function addPreEditListener(listener: EditListener) { +export function addMessagePreEditListener(listener: MessageEditListener) { editListeners.add(listener); return listener; } -export function removePreSendListener(listener: SendListener) { +export function removeMessagePreSendListener(listener: MessageSendListener) { return sendListeners.delete(listener); } -export function removePreEditListener(listener: EditListener) { +export function removeMessagePreEditListener(listener: MessageEditListener) { return editListeners.delete(listener); } // Message clicks -type ClickListener = (message: Message, channel: Channel, event: MouseEvent) => void; +export type MessageClickListener = (message: Message, channel: Channel, event: MouseEvent) => void; -const listeners = new Set(); +const listeners = new Set(); export function _handleClick(message: Message, channel: Channel, event: MouseEvent) { // message object may be outdated, so (try to) fetch latest one @@ -147,11 +147,11 @@ export function _handleClick(message: Message, channel: Channel, event: MouseEve } } -export function addClickListener(listener: ClickListener) { +export function addMessageClickListener(listener: MessageClickListener) { listeners.add(listener); return listener; } -export function removeClickListener(listener: ClickListener) { +export function removeMessageClickListener(listener: MessageClickListener) { return listeners.delete(listener); } diff --git a/src/api/MessagePopover.tsx b/src/api/MessagePopover.tsx index eb68ed2d6..717879546 100644 --- a/src/api/MessagePopover.tsx +++ b/src/api/MessagePopover.tsx @@ -23,7 +23,7 @@ import type { ComponentType, MouseEventHandler } from "react"; const logger = new Logger("MessagePopover"); -export interface ButtonItem { +export interface MessagePopoverButtonItem { key?: string, label: string, icon: ComponentType, @@ -33,23 +33,23 @@ export interface ButtonItem { onContextMenu?: MouseEventHandler; } -export type getButtonItem = (message: Message) => ButtonItem | null; +export type MessagePopoverButtonFactory = (message: Message) => MessagePopoverButtonItem | null; -export const buttons = new Map(); +export const buttons = new Map(); -export function addButton( +export function addMessagePopoverButton( identifier: string, - item: getButtonItem, + item: MessagePopoverButtonFactory, ) { buttons.set(identifier, item); } -export function removeButton(identifier: string) { +export function removeMessagePopoverButton(identifier: string) { buttons.delete(identifier); } export function _buildPopoverElements( - Component: React.ComponentType, + Component: React.ComponentType, message: Message ) { const items: React.ReactNode[] = []; diff --git a/src/api/ServerList.ts b/src/api/ServerList.tsx similarity index 64% rename from src/api/ServerList.ts rename to src/api/ServerList.tsx index 462745b04..7a673c9df 100644 --- a/src/api/ServerList.ts +++ b/src/api/ServerList.tsx @@ -16,41 +16,36 @@ * along with this program. If not, see . */ -import { Logger } from "@utils/Logger"; -import { JSX } from "react"; - -const logger = new Logger("ServerListAPI"); +import ErrorBoundary from "@components/ErrorBoundary"; +import { ComponentType } from "react"; export const enum ServerListRenderPosition { Above, In, } -const renderFunctionsAbove = new Set(); -const renderFunctionsIn = new Set(); +const componentsAbove = new Set(); +const componentsBelow = new Set(); function getRenderFunctions(position: ServerListRenderPosition) { - return position === ServerListRenderPosition.Above ? renderFunctionsAbove : renderFunctionsIn; + return position === ServerListRenderPosition.Above ? componentsAbove : componentsBelow; } -export function addServerListElement(position: ServerListRenderPosition, renderFunction: Function) { +export function addServerListElement(position: ServerListRenderPosition, renderFunction: ComponentType) { getRenderFunctions(position).add(renderFunction); } -export function removeServerListElement(position: ServerListRenderPosition, renderFunction: Function) { +export function removeServerListElement(position: ServerListRenderPosition, renderFunction: ComponentType) { getRenderFunctions(position).delete(renderFunction); } export const renderAll = (position: ServerListRenderPosition) => { - const ret: Array = []; - - for (const renderFunction of getRenderFunctions(position)) { - try { - ret.unshift(renderFunction()); - } catch (e) { - logger.error("Failed to render server list element:", e); - } - } - - return ret; + return Array.from( + getRenderFunctions(position), + (Component, i) => ( + + + + ) + ); }; diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx index 60ff1faf2..bb2df3421 100644 --- a/src/components/ErrorBoundary.tsx +++ b/src/components/ErrorBoundary.tsx @@ -70,8 +70,7 @@ const ErrorBoundary = LazyComponent(() => { componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { this.props.onError?.({ error, errorInfo, props: this.props.wrappedProps }); - logger.error("A component threw an Error\n", error); - logger.error("Component Stack", errorInfo.componentStack); + logger.error(`${this.props.message || "A component threw an Error"}\n`, error, errorInfo.componentStack); } render() { diff --git a/src/plugins/_api/badges/index.tsx b/src/plugins/_api/badges/index.tsx index acdfb1f1f..2a83809d6 100644 --- a/src/plugins/_api/badges/index.tsx +++ b/src/plugins/_api/badges/index.tsx @@ -102,8 +102,9 @@ export default definePlugin({ } }, + userProfileBadge: ContributorBadge, + async start() { - Vencord.Api.Badges.addBadge(ContributorBadge); await loadBadges(); }, diff --git a/src/plugins/_core/supportHelper.tsx b/src/plugins/_core/supportHelper.tsx index 8687f843b..72b683249 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 { addAccessory } from "@api/MessageAccessories"; import { definePluginSettings } from "@api/Settings"; import { getUserSettingLazy } from "@api/UserSettings"; import ErrorBoundary from "@components/ErrorBoundary"; @@ -143,7 +142,7 @@ export default definePlugin({ required: true, description: "Helps us provide support to you", authors: [Devs.Ven], - dependencies: ["UserSettingsAPI", "MessageAccessoriesAPI"], + dependencies: ["UserSettingsAPI"], settings, @@ -236,6 +235,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; @@ -250,85 +328,4 @@ export default definePlugin({ ); }, { noop: true }), - - start() { - addAccessory("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/accountPanelServerProfile/index.tsx b/src/plugins/accountPanelServerProfile/index.tsx index e2dc220b7..a2fed5d79 100644 --- a/src/plugins/accountPanelServerProfile/index.tsx +++ b/src/plugins/accountPanelServerProfile/index.tsx @@ -23,7 +23,7 @@ const UserProfile = findComponentByCodeLazy("UserProfilePopoutWrapper: user cann const styles = findByPropsLazy("accountProfilePopoutWrapper"); let openAlternatePopout = false; -let accountPanelRef: React.MutableRefObject | null> = { current: null }; +let accountPanelRef: React.RefObject | null> = { current: null }; const AccountPanelContextMenu = ErrorBoundary.wrap(() => { const { prioritizeServerProfile } = settings.use(["prioritizeServerProfile"]); diff --git a/src/plugins/clearURLs/index.ts b/src/plugins/clearURLs/index.ts index d1be6c6f5..f00c751d4 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,18 @@ export default definePlugin({ name: "ClearURLs", description: "Removes tracking garbage from URLs", authors: [Devs.adryd], - dependencies: ["MessageEventsAPI"], + + start() { + this.createRules(); + }, + + onBeforeMessageSend(_, msg) { + return this.onSend(msg); + }, + + onBeforeMessageEdit(_cid, _mid, msg) { + return this.onSend(msg); + }, escapeRegExp(str: string) { return (str && reHasRegExpChar.test(str)) @@ -133,17 +140,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/fakeNitro/index.tsx b/src/plugins/fakeNitro/index.tsx index df8b4cd87..d87170ad8 100644 --- a/src/plugins/fakeNitro/index.tsx +++ b/src/plugins/fakeNitro/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { addPreEditListener, addPreSendListener, removePreEditListener, removePreSendListener } from "@api/MessageEvents"; +import { addMessagePreEditListener, addMessagePreSendListener, removeMessagePreEditListener, removeMessagePreSendListener } from "@api/MessageEvents"; import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import { ApngBlendOp, ApngDisposeOp, importApngJs } from "@utils/dependencies"; @@ -853,7 +853,7 @@ export default definePlugin({ }); } - this.preSend = addPreSendListener(async (channelId, messageObj, extra) => { + this.preSend = addMessagePreSendListener(async (channelId, messageObj, extra) => { const { guildId } = this; let hasBypass = false; @@ -941,7 +941,7 @@ export default definePlugin({ return { cancel: false }; }); - this.preEdit = addPreEditListener(async (channelId, __, messageObj) => { + this.preEdit = addMessagePreEditListener(async (channelId, __, messageObj) => { if (!s.enableEmojiBypass) return; let hasBypass = false; @@ -973,7 +973,7 @@ export default definePlugin({ }, stop() { - removePreSendListener(this.preSend); - removePreEditListener(this.preEdit); + removeMessagePreSendListener(this.preSend); + removeMessagePreEditListener(this.preEdit); } }); diff --git a/src/plugins/hideAttachments/index.tsx b/src/plugins/hideAttachments/index.tsx index fe7f4ab92..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 { addButton, removeButton } 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(); - - addButton("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(); - removeButton("HideAttachments"); }, async buildCss() { diff --git a/src/plugins/index.ts b/src/plugins/index.ts index 129e42a0d..9d672c634 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -16,12 +16,19 @@ * along with this program. If not, see . */ +import { addProfileBadge, removeProfileBadge } from "@api/Badges"; +import { addChatBarButton, removeChatBarButton } from "@api/ChatButtons"; import { registerCommand, unregisterCommand } from "@api/Commands"; import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; +import { addMemberListDecorator, removeMemberListDecorator } from "@api/MemberListDecorators"; +import { addMessageAccessory, removeMessageAccessory } from "@api/MessageAccessories"; +import { addMessageDecoration, removeMessageDecoration } from "@api/MessageDecorations"; +import { addMessageClickListener, addMessagePreEditListener, addMessagePreSendListener, removeMessageClickListener, removeMessagePreEditListener, removeMessagePreSendListener } from "@api/MessageEvents"; +import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover"; import { Settings } from "@api/Settings"; import { Logger } from "@utils/Logger"; import { canonicalizeFind } from "@utils/patches"; -import { Patch, Plugin, ReporterTestable, StartAt } from "@utils/types"; +import { Patch, Plugin, PluginDef, ReporterTestable, StartAt } from "@utils/types"; import { FluxDispatcher } from "@webpack/common"; import { FluxEvents } from "@webpack/types"; @@ -83,6 +90,13 @@ function isReporterTestable(p: Plugin, part: ReporterTestable) { : (p.reporterTestable & part) === part; } +const pluginKeysToBind: Array = [ + "onBeforeMessageEdit", "onBeforeMessageSend", "onMessageClick", + "renderChatBarButton", "renderMemberListDecorator", "renderMessageAccessory", "renderMessageDecoration", "renderMessagePopoverButton" +]; + +const neededApiPlugins = new Set(); + // First round-trip to mark and force enable dependencies // // FIXME: might need to revisit this if there's ever nested (dependencies of dependencies) dependencies since this only @@ -106,12 +120,25 @@ for (const p of pluginsValues) if (isPluginEnabled(p.name)) { dep.isDependency = true; }); - if (p.commands?.length) { - Plugins.CommandsAPI.isDependency = true; - settings.CommandsAPI.enabled = true; + if (p.commands?.length) neededApiPlugins.add("CommandsAPI"); + if (p.onBeforeMessageEdit || p.onBeforeMessageSend || p.onMessageClick) neededApiPlugins.add("MessageEventsAPI"); + if (p.renderChatBarButton) neededApiPlugins.add("ChatInputButtonAPI"); + if (p.renderMemberListDecorator) neededApiPlugins.add("MemberListDecoratorsAPI"); + if (p.renderMessageAccessory) neededApiPlugins.add("MessageAccessoriesAPI"); + if (p.renderMessageDecoration) neededApiPlugins.add("MessageDecorationsAPI"); + if (p.renderMessagePopoverButton) neededApiPlugins.add("MessagePopoverAPI"); + if (p.userProfileBadge) neededApiPlugins.add("BadgeAPI"); + + for (const key of pluginKeysToBind) { + p[key] &&= p[key].bind(p) as any; } } +for (const p of neededApiPlugins) { + Plugins[p].isDependency = true; + settings[p].enabled = true; +} + for (const p of pluginsValues) { if (p.settings) { p.settings.pluginName = p.name; @@ -215,7 +242,11 @@ export function subscribeAllPluginsFluxEvents(fluxDispatcher: typeof FluxDispatc } export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) { - const { name, commands, contextMenus } = p; + const { + name, commands, contextMenus, userProfileBadge, + onBeforeMessageEdit, onBeforeMessageSend, onMessageClick, + renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton + } = p; if (p.start) { logger.info("Starting plugin", name); @@ -249,7 +280,6 @@ export const startPlugin = traceFunction("startPlugin", function startPlugin(p: subscribePluginFluxEvents(p, FluxDispatcher); } - if (contextMenus) { logger.debug("Adding context menus patches of plugin", name); for (const navId in contextMenus) { @@ -257,11 +287,27 @@ export const startPlugin = traceFunction("startPlugin", function startPlugin(p: } } + if (userProfileBadge) addProfileBadge(userProfileBadge); + + if (onBeforeMessageEdit) addMessagePreEditListener(onBeforeMessageEdit); + if (onBeforeMessageSend) addMessagePreSendListener(onBeforeMessageSend); + if (onMessageClick) addMessageClickListener(onMessageClick); + + if (renderChatBarButton) addChatBarButton(name, renderChatBarButton); + if (renderMemberListDecorator) addMemberListDecorator(name, renderMemberListDecorator); + if (renderMessageDecoration) addMessageDecoration(name, renderMessageDecoration); + if (renderMessageAccessory) addMessageAccessory(name, renderMessageAccessory); + if (renderMessagePopoverButton) addMessagePopoverButton(name, renderMessagePopoverButton); + return true; }, p => `startPlugin ${p.name}`); export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plugin) { - const { name, commands, contextMenus } = p; + const { + name, commands, contextMenus, userProfileBadge, + onBeforeMessageEdit, onBeforeMessageSend, onMessageClick, + renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton + } = p; if (p.stop) { logger.info("Stopping plugin", name); @@ -300,5 +346,17 @@ export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plu } } + if (userProfileBadge) removeProfileBadge(userProfileBadge); + + if (onBeforeMessageEdit) removeMessagePreEditListener(onBeforeMessageEdit); + if (onBeforeMessageSend) removeMessagePreSendListener(onBeforeMessageSend); + if (onMessageClick) removeMessageClickListener(onMessageClick); + + if (renderChatBarButton) removeChatBarButton(name); + if (renderMemberListDecorator) removeMemberListDecorator(name); + if (renderMessageDecoration) removeMessageDecoration(name); + if (renderMessageAccessory) removeMessageAccessory(name); + if (renderMessagePopoverButton) removeMessagePopoverButton(name); + return true; }, p => `stopPlugin ${p.name}`); diff --git a/src/plugins/invisibleChat.desktop/index.tsx b/src/plugins/invisibleChat.desktop/index.tsx index 1af8f4e5d..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 } from "@api/ChatButtons"; -import { addButton, removeButton } from "@api/MessagePopover"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { updateMessage } from "@api/MessageUpdater"; import { definePluginSettings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; @@ -66,7 +65,7 @@ function Indicator() { } -const ChatBarIcon: ChatBarButton = ({ isMainChat }) => { +const ChatBarIcon: ChatBarButtonFactory = ({ isMainChat }) => { if (!isMainChat) return null; return ( @@ -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() { - addButton("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() { - removeButton("InvisibleChat"); - removeButton("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 7437cace7..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 { addClickListener, removeClickListener } 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 = addClickListener((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() { - removeClickListener(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/messageLinkEmbeds/index.tsx b/src/plugins/messageLinkEmbeds/index.tsx index 98a153937..c28e78017 100644 --- a/src/plugins/messageLinkEmbeds/index.tsx +++ b/src/plugins/messageLinkEmbeds/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { addAccessory, removeAccessory } from "@api/MessageAccessories"; +import { addMessageAccessory, removeMessageAccessory } from "@api/MessageAccessories"; import { updateMessage } from "@api/MessageUpdater"; import { definePluginSettings } from "@api/Settings"; import { getUserSettingLazy } from "@api/UserSettings"; @@ -373,7 +373,7 @@ export default definePlugin({ settings, start() { - addAccessory("messageLinkEmbed", props => { + addMessageAccessory("messageLinkEmbed", props => { if (!messageLinkRegex.test(props.message.content)) return null; @@ -391,6 +391,6 @@ export default definePlugin({ }, stop() { - removeAccessory("messageLinkEmbed"); + removeMessageAccessory("messageLinkEmbed"); } }); diff --git a/src/plugins/platformIndicators/index.tsx b/src/plugins/platformIndicators/index.tsx index 1dc76e9d3..d2b722eff 100644 --- a/src/plugins/platformIndicators/index.tsx +++ b/src/plugins/platformIndicators/index.tsx @@ -18,9 +18,9 @@ import "./style.css"; -import { addBadge, BadgePosition, BadgeUserArgs, ProfileBadge, removeBadge } from "@api/Badges"; -import { addDecorator, removeDecorator } from "@api/MemberListDecorators"; -import { addDecoration, removeDecoration } from "@api/MessageDecorations"; +import { addProfileBadge, BadgePosition, BadgeUserArgs, ProfileBadge, removeProfileBadge } from "@api/Badges"; +import { addMemberListDecorator, removeMemberListDecorator } from "@api/MemberListDecorators"; +import { addMessageDecoration, removeMessageDecoration } from "@api/MessageDecorations"; import { Settings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; import { Devs } from "@utils/constants"; @@ -172,26 +172,26 @@ const badge: ProfileBadge = { const indicatorLocations = { list: { description: "In the member list", - onEnable: () => addDecorator("platform-indicator", props => + onEnable: () => addMemberListDecorator("platform-indicator", props => ), - onDisable: () => removeDecorator("platform-indicator") + onDisable: () => removeMemberListDecorator("platform-indicator") }, badges: { description: "In user profiles, as badges", - onEnable: () => addBadge(badge), - onDisable: () => removeBadge(badge) + onEnable: () => addProfileBadge(badge), + onDisable: () => removeProfileBadge(badge) }, messages: { description: "Inside messages", - onEnable: () => addDecoration("platform-indicator", props => + onEnable: () => addMessageDecoration("platform-indicator", props => ), - onDisable: () => removeDecoration("platform-indicator") + onDisable: () => removeMessageDecoration("platform-indicator") } }; diff --git a/src/plugins/previewMessage/index.tsx b/src/plugins/previewMessage/index.tsx index fe6b227a5..7b03e31d7 100644 --- a/src/plugins/previewMessage/index.tsx +++ b/src/plugins/previewMessage/index.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { addChatBarButton, ChatBarButton, removeChatBarButton } from "@api/ChatButtons"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { generateId, sendBotMessage } from "@api/Commands"; import { Devs } from "@utils/constants"; import definePlugin, { StartAt } from "@utils/types"; @@ -73,7 +73,7 @@ const getAttachments = async (channelId: string) => ); -const PreviewButton: ChatBarButton = ({ isMainChat, isEmpty, type: { attachments } }) => { +const PreviewButton: ChatBarButtonFactory = ({ isMainChat, isEmpty, type: { attachments } }) => { const channelId = SelectedChannelStore.getChannelId(); const draft = useStateFromStores([DraftStore], () => getDraft(channelId)); @@ -121,11 +121,9 @@ export default definePlugin({ name: "PreviewMessage", description: "Lets you preview your message before sending it.", authors: [Devs.Aria], - dependencies: ["ChatInputButtonAPI"], // start early to ensure we're the first plugin to add our button // This makes the popping in less awkward startAt: StartAt.Init, - start: () => addChatBarButton("previewMessage", PreviewButton), - stop: () => removeChatBarButton("previewMessage"), + renderChatBarButton: PreviewButton, }); diff --git a/src/plugins/quickMention/index.tsx b/src/plugins/quickMention/index.tsx index df86e9b70..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 { addButton, removeButton } 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() { - addButton("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() { - removeButton("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 2306c20cd..437df1a58 100644 --- a/src/plugins/sendTimestamps/index.tsx +++ b/src/plugins/sendTimestamps/index.tsx @@ -18,8 +18,7 @@ import "./styles.css"; -import { addChatBarButton, ChatBarButton, 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"; @@ -123,7 +122,7 @@ function PickerModal({ rootProps, close }: { rootProps: ModalProps, close(): voi ); } -const ChatBarIcon: ChatBarButton = ({ isMainChat }) => { +const ChatBarIcon: ChatBarButtonFactory = ({ isMainChat }) => { if (!isMainChat) return null; return ( @@ -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 d7a7ed7f0..00e605099 100644 --- a/src/plugins/silentMessageToggle/index.tsx +++ b/src/plugins/silentMessageToggle/index.tsx @@ -16,8 +16,8 @@ * along with this program. If not, see . */ -import { addChatBarButton, ChatBarButton, removeChatBarButton } from "@api/ChatButtons"; -import { addPreSendListener, removePreSendListener, SendListener } from "@api/MessageEvents"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; +import { addMessagePreSendListener, MessageSendListener, removeMessagePreSendListener } from "@api/MessageEvents"; import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; @@ -41,7 +41,7 @@ const settings = definePluginSettings({ } }); -const SilentMessageToggle: ChatBarButton = ({ isMainChat }) => { +const SilentMessageToggle: ChatBarButtonFactory = ({ isMainChat }) => { const [enabled, setEnabled] = useState(lastState); function setEnabledValue(value: boolean) { @@ -50,15 +50,15 @@ const SilentMessageToggle: ChatBarButton = ({ isMainChat }) => { } useEffect(() => { - const listener: SendListener = (_, message) => { + const listener: MessageSendListener = (_, message) => { if (enabled) { if (settings.store.autoDisable) setEnabledValue(false); if (!message.content.startsWith("@silent ")) message.content = "@silent " + message.content; } }; - addPreSendListener(listener); - return () => void removePreSendListener(listener); + addMessagePreSendListener(listener); + return () => void removeMessagePreSendListener(listener); }, [enabled]); if (!isMainChat) return null; @@ -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 d06ae8373..92bdbc49e 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, 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"; @@ -43,7 +43,7 @@ const settings = definePluginSettings({ } }); -const SilentTypingToggle: ChatBarButton = ({ isMainChat }) => { +const SilentTypingToggle: ChatBarButtonFactory = ({ isMainChat }) => { const { isEnabled, showIcon } = settings.use(["isEnabled", "showIcon"]); const toggle = () => settings.store.isEnabled = !settings.store.isEnabled; @@ -96,11 +96,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"', @@ -136,6 +137,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/TranslateIcon.tsx b/src/plugins/translate/TranslateIcon.tsx index fa1d9abf6..1b77fb94c 100644 --- a/src/plugins/translate/TranslateIcon.tsx +++ b/src/plugins/translate/TranslateIcon.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { ChatBarButton } from "@api/ChatButtons"; +import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { classes } from "@utils/misc"; import { openModal } from "@utils/modal"; import { Alerts, Forms, Tooltip, useEffect, useState } from "@webpack/common"; @@ -40,7 +40,7 @@ export function TranslateIcon({ height = 24, width = 24, className }: { height?: export let setShouldShowTranslateEnabledTooltip: undefined | ((show: boolean) => void); -export const TranslateChatBarIcon: ChatBarButton = ({ isMainChat }) => { +export const TranslateChatBarIcon: ChatBarButtonFactory = ({ isMainChat }) => { const { autoTranslate, showChatBarButton } = settings.use(["autoTranslate", "showChatBarButton"]); const [shouldShowTranslateEnabledTooltip, setter] = useState(false); diff --git a/src/plugins/translate/index.tsx b/src/plugins/translate/index.tsx index de61cef9c..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 { addAccessory, removeAccessory } from "@api/MessageAccessories"; -import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; -import { addButton, removeButton } 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() { - addAccessory("vc-translation", props => ); + renderMessageAccessory: props => , - addChatBarButton("vc-translate", TranslateChatBarIcon); + renderChatBarButton: TranslateChatBarIcon, - addButton("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"); - removeButton("vc-translate"); - removeAccessory("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/userVoiceShow/index.tsx b/src/plugins/userVoiceShow/index.tsx index e0d5d8abd..f3063f590 100644 --- a/src/plugins/userVoiceShow/index.tsx +++ b/src/plugins/userVoiceShow/index.tsx @@ -18,8 +18,8 @@ import "./style.css"; -import { addDecorator, removeDecorator } from "@api/MemberListDecorators"; -import { addDecoration, removeDecoration } from "@api/MessageDecorations"; +import { addMemberListDecorator, removeMemberListDecorator } from "@api/MemberListDecorators"; +import { addMessageDecoration, removeMessageDecoration } from "@api/MessageDecorations"; import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; @@ -96,16 +96,16 @@ export default definePlugin({ start() { if (settings.store.showInMemberList) { - addDecorator("UserVoiceShow", ({ user }) => user == null ? null : ); + addMemberListDecorator("UserVoiceShow", ({ user }) => user == null ? null : ); } if (settings.store.showInMessages) { - addDecoration("UserVoiceShow", ({ message }) => message?.author == null ? null : ); + addMessageDecoration("UserVoiceShow", ({ message }) => message?.author == null ? null : ); } }, stop() { - removeDecorator("UserVoiceShow"); - removeDecoration("UserVoiceShow"); + removeMemberListDecorator("UserVoiceShow"); + removeMessageDecoration("UserVoiceShow"); }, VoiceChannelIndicator diff --git a/src/plugins/viewRaw/index.tsx b/src/plugins/viewRaw/index.tsx index 8ee1ca8d7..b45919a21 100644 --- a/src/plugins/viewRaw/index.tsx +++ b/src/plugins/viewRaw/index.tsx @@ -17,7 +17,6 @@ */ import { NavContextMenuPatchCallback } from "@api/ContextMenu"; -import { addButton, removeButton } 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() { - addButton("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() { - removeButton("ViewRaw"); + return { + label, + icon: CopyIcon, + message: msg, + channel: ChannelStore.getChannel(msg.channel_id), + onClick: handleClick, + onContextMenu: handleContextMenu + }; } }); diff --git a/src/utils/types.ts b/src/utils/types.ts index 02760d964..b2210ffa5 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -16,8 +16,15 @@ * along with this program. If not, see . */ +import { ProfileBadge } from "@api/Badges"; +import { ChatBarButtonFactory } from "@api/ChatButtons"; import { Command } from "@api/Commands"; import { NavContextMenuPatchCallback } from "@api/ContextMenu"; +import { MemberListDecoratorFactory } from "@api/MemberListDecorators"; +import { MessageAccessoryFactory } from "@api/MessageAccessories"; +import { MessageDecorationFactory } from "@api/MessageDecorations"; +import { MessageClickListener, MessageEditListener, MessageSendListener } from "@api/MessageEvents"; +import { MessagePopoverButtonFactory } from "@api/MessagePopover"; import { FluxEvents } from "@webpack/types"; import { JSX } from "react"; import { Promisable } from "type-fest"; @@ -142,6 +149,20 @@ export interface PluginDef { toolboxActions?: Record void>; tags?: string[]; + + userProfileBadge?: ProfileBadge; + + onMessageClick?: MessageClickListener; + onBeforeMessageSend?: MessageSendListener; + onBeforeMessageEdit?: MessageEditListener; + + renderMessagePopoverButton?: MessagePopoverButtonFactory; + renderMessageAccessory?: MessageAccessoryFactory; + renderMessageDecoration?: MessageDecorationFactory; + + renderMemberListDecorator?: MemberListDecoratorFactory; + + renderChatBarButton?: ChatBarButtonFactory; } export const enum StartAt {