1
0
Fork 1
mirror of https://github.com/Vendicated/Vencord.git synced 2025-01-10 01:46:23 +00:00

make plugin manager actually use fields

This commit is contained in:
Vendicated 2024-11-21 03:54:31 +01:00
parent bd1dd23ba3
commit cff2c53972
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
8 changed files with 73 additions and 31 deletions

View file

@ -23,7 +23,7 @@ import type { ComponentType, MouseEventHandler } from "react";
const logger = new Logger("MessagePopover"); const logger = new Logger("MessagePopover");
export interface ButtonItem { export interface MessagePopoverButtonItem {
key?: string, key?: string,
label: string, label: string,
icon: ComponentType<any>, icon: ComponentType<any>,
@ -33,23 +33,23 @@ export interface ButtonItem {
onContextMenu?: MouseEventHandler<HTMLButtonElement>; onContextMenu?: MouseEventHandler<HTMLButtonElement>;
} }
export type MessagePopoverButtonFactory = (message: Message) => ButtonItem | null; export type MessagePopoverButtonFactory = (message: Message) => MessagePopoverButtonItem | null;
export const buttons = new Map<string, MessagePopoverButtonFactory>(); export const buttons = new Map<string, MessagePopoverButtonFactory>();
export function addButton( export function addMessagePopoverButton(
identifier: string, identifier: string,
item: MessagePopoverButtonFactory, item: MessagePopoverButtonFactory,
) { ) {
buttons.set(identifier, item); buttons.set(identifier, item);
} }
export function removeButton(identifier: string) { export function removeMessagePopoverButton(identifier: string) {
buttons.delete(identifier); buttons.delete(identifier);
} }
export function _buildPopoverElements( export function _buildPopoverElements(
Component: React.ComponentType<ButtonItem>, Component: React.ComponentType<MessagePopoverButtonItem>,
message: Message message: Message
) { ) {
const items: React.ReactNode[] = []; const items: React.ReactNode[] = [];

View file

@ -17,7 +17,7 @@
*/ */
import { get, set } from "@api/DataStore"; import { get, set } from "@api/DataStore";
import { addButton, removeButton } from "@api/MessagePopover"; import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { ImageInvisible, ImageVisible } from "@components/Icons"; import { ImageInvisible, ImageVisible } from "@components/Icons";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
@ -48,7 +48,7 @@ export default definePlugin({
await getHiddenMessages(); await getHiddenMessages();
await this.buildCss(); await this.buildCss();
addButton("HideAttachments", msg => { addMessagePopoverButton("HideAttachments", msg => {
if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length) return null; if (!msg.attachments.length && !msg.embeds.length && !msg.stickerItems.length) return null;
const isHidden = hiddenMessages.has(msg.id); const isHidden = hiddenMessages.has(msg.id);
@ -66,7 +66,7 @@ export default definePlugin({
stop() { stop() {
style.remove(); style.remove();
hiddenMessages.clear(); hiddenMessages.clear();
removeButton("HideAttachments"); removeMessagePopoverButton("HideAttachments");
}, },
async buildCss() { async buildCss() {

View file

@ -16,8 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { addChatBarButton, removeChatBarButton } from "@api/ChatButtons";
import { registerCommand, unregisterCommand } from "@api/Commands"; import { registerCommand, unregisterCommand } from "@api/Commands";
import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu";
import { addDecorator, removeDecorator } from "@api/MemberListDecorators";
import { addAccessory, removeAccessory } from "@api/MessageAccessories";
import { addDecoration, removeDecoration } from "@api/MessageDecorations";
import { addClickListener, addPreEditListener, addPreSendListener, removeClickListener, removePreEditListener, removePreSendListener } from "@api/MessageEvents";
import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { Settings } from "@api/Settings"; import { Settings } from "@api/Settings";
import { Logger } from "@utils/Logger"; import { Logger } from "@utils/Logger";
import { canonicalizeFind } from "@utils/patches"; import { canonicalizeFind } from "@utils/patches";
@ -83,6 +89,8 @@ function isReporterTestable(p: Plugin, part: ReporterTestable) {
: (p.reporterTestable & part) === part; : (p.reporterTestable & part) === part;
} }
const neededApiPlugins = new Set<string>();
// First round-trip to mark and force enable dependencies // 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 // FIXME: might need to revisit this if there's ever nested (dependencies of dependencies) dependencies since this only
@ -106,10 +114,18 @@ for (const p of pluginsValues) if (isPluginEnabled(p.name)) {
dep.isDependency = true; dep.isDependency = true;
}); });
if (p.commands?.length) { if (p.commands?.length) neededApiPlugins.add("CommandsAPI");
Plugins.CommandsAPI.isDependency = true; if (p.onBeforeMessageEdit || p.onBeforeMessageSend || p.onMessageClick) neededApiPlugins.add("MessageEventsAPI");
settings.CommandsAPI.enabled = true; 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");
}
for (const p of neededApiPlugins) {
Plugins[p].isDependency = true;
settings[p].enabled = true;
} }
for (const p of pluginsValues) { for (const p of pluginsValues) {
@ -215,7 +231,11 @@ export function subscribeAllPluginsFluxEvents(fluxDispatcher: typeof FluxDispatc
} }
export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) { export const startPlugin = traceFunction("startPlugin", function startPlugin(p: Plugin) {
const { name, commands, contextMenus } = p; const {
name, commands, contextMenus,
onBeforeMessageEdit, onBeforeMessageSend, onMessageClick,
renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton
} = p;
if (p.start) { if (p.start) {
logger.info("Starting plugin", name); logger.info("Starting plugin", name);
@ -257,11 +277,25 @@ export const startPlugin = traceFunction("startPlugin", function startPlugin(p:
} }
} }
if (onBeforeMessageEdit) addPreEditListener(onBeforeMessageEdit);
if (onBeforeMessageSend) addPreSendListener(onBeforeMessageSend);
if (onMessageClick) addClickListener(onMessageClick);
if (renderChatBarButton) addChatBarButton(name, renderChatBarButton);
if (renderMemberListDecorator) addDecorator(name, renderMemberListDecorator);
if (renderMessageDecoration) addDecoration(name, renderMessageDecoration);
if (renderMessageAccessory) addAccessory(name, renderMessageAccessory);
if (renderMessagePopoverButton) addMessagePopoverButton(name, renderMessagePopoverButton);
return true; return true;
}, p => `startPlugin ${p.name}`); }, p => `startPlugin ${p.name}`);
export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plugin) { export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plugin) {
const { name, commands, contextMenus } = p; const {
name, commands, contextMenus,
onBeforeMessageEdit, onBeforeMessageSend, onMessageClick,
renderChatBarButton, renderMemberListDecorator, renderMessageAccessory, renderMessageDecoration, renderMessagePopoverButton
} = p;
if (p.stop) { if (p.stop) {
logger.info("Stopping plugin", name); logger.info("Stopping plugin", name);
@ -300,5 +334,15 @@ export const stopPlugin = traceFunction("stopPlugin", function stopPlugin(p: Plu
} }
} }
if (onBeforeMessageEdit) removePreEditListener(onBeforeMessageEdit);
if (onBeforeMessageSend) removePreSendListener(onBeforeMessageSend);
if (onMessageClick) removeClickListener(onMessageClick);
if (renderChatBarButton) removeChatBarButton(name);
if (renderMemberListDecorator) removeDecorator(name);
if (renderMessageDecoration) removeDecoration(name);
if (renderMessageAccessory) removeAccessory(name);
if (renderMessagePopoverButton) removeMessagePopoverButton(name);
return true; return true;
}, p => `stopPlugin ${p.name}`); }, p => `stopPlugin ${p.name}`);

View file

@ -17,7 +17,7 @@
*/ */
import { addChatBarButton, ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons"; import { addChatBarButton, ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons";
import { addButton, removeButton } from "@api/MessagePopover"; import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { updateMessage } from "@api/MessageUpdater"; import { updateMessage } from "@api/MessageUpdater";
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
@ -125,7 +125,7 @@ export default definePlugin({
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/, /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/,
), ),
async start() { async start() {
addButton("InvisibleChat", message => { addMessagePopoverButton("InvisibleChat", message => {
return this.INV_REGEX.test(message?.content) return this.INV_REGEX.test(message?.content)
? { ? {
label: "Decrypt Message", label: "Decrypt Message",
@ -151,8 +151,8 @@ export default definePlugin({
}, },
stop() { stop() {
removeButton("InvisibleChat"); removeMessagePopoverButton("InvisibleChat");
removeButton("InvisibleChat"); removeMessagePopoverButton("InvisibleChat");
}, },
// Gets the Embed of a Link // Gets the Embed of a Link

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { addChatBarButton, ChatBarButton, ChatBarButtonFactory, removeChatBarButton } from "@api/ChatButtons"; import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons";
import { generateId, sendBotMessage } from "@api/Commands"; import { generateId, sendBotMessage } from "@api/Commands";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { StartAt } from "@utils/types"; import definePlugin, { StartAt } from "@utils/types";
@ -121,11 +121,9 @@ export default definePlugin({
name: "PreviewMessage", name: "PreviewMessage",
description: "Lets you preview your message before sending it.", description: "Lets you preview your message before sending it.",
authors: [Devs.Aria], authors: [Devs.Aria],
dependencies: ["ChatInputButtonAPI"],
// start early to ensure we're the first plugin to add our button // start early to ensure we're the first plugin to add our button
// This makes the popping in less awkward // This makes the popping in less awkward
startAt: StartAt.Init, startAt: StartAt.Init,
start: () => addChatBarButton("previewMessage", PreviewButton), renderChatBarButton: PreviewButton,
stop: () => removeChatBarButton("previewMessage"),
}); });

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { addButton, removeButton } from "@api/MessagePopover"; import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import { insertTextIntoChatInputBox } from "@utils/discord"; import { insertTextIntoChatInputBox } from "@utils/discord";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
@ -29,7 +29,7 @@ export default definePlugin({
dependencies: ["MessagePopoverAPI"], dependencies: ["MessagePopoverAPI"],
start() { start() {
addButton("QuickMention", msg => { addMessagePopoverButton("QuickMention", msg => {
const channel = ChannelStore.getChannel(msg.channel_id); const channel = ChannelStore.getChannel(msg.channel_id);
if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return null; if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return null;
@ -43,7 +43,7 @@ export default definePlugin({
}); });
}, },
stop() { stop() {
removeButton("QuickMention"); removeMessagePopoverButton("QuickMention");
}, },
Icon: () => ( Icon: () => (

View file

@ -22,7 +22,7 @@ import { addChatBarButton, removeChatBarButton } from "@api/ChatButtons";
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu"; import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
import { addAccessory, removeAccessory } from "@api/MessageAccessories"; import { addAccessory, removeAccessory } from "@api/MessageAccessories";
import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; import { addPreSendListener, removePreSendListener } from "@api/MessageEvents";
import { addButton, removeButton } from "@api/MessagePopover"; import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { ChannelStore, Menu } from "@webpack/common"; import { ChannelStore, Menu } from "@webpack/common";
@ -68,7 +68,7 @@ export default definePlugin({
addChatBarButton("vc-translate", TranslateChatBarIcon); addChatBarButton("vc-translate", TranslateChatBarIcon);
addButton("vc-translate", message => { addMessagePopoverButton("vc-translate", message => {
if (!message.content) return null; if (!message.content) return null;
return { return {
@ -101,7 +101,7 @@ export default definePlugin({
stop() { stop() {
removePreSendListener(this.preSend); removePreSendListener(this.preSend);
removeChatBarButton("vc-translate"); removeChatBarButton("vc-translate");
removeButton("vc-translate"); removeMessagePopoverButton("vc-translate");
removeAccessory("vc-translation"); removeAccessory("vc-translation");
}, },
}); });

View file

@ -17,7 +17,7 @@
*/ */
import { NavContextMenuPatchCallback } from "@api/ContextMenu"; import { NavContextMenuPatchCallback } from "@api/ContextMenu";
import { addButton, removeButton } from "@api/MessagePopover"; import { addMessagePopoverButton, removeMessagePopoverButton } from "@api/MessagePopover";
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import { CodeBlock } from "@components/CodeBlock"; import { CodeBlock } from "@components/CodeBlock";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
@ -160,7 +160,7 @@ export default definePlugin({
}, },
start() { start() {
addButton("ViewRaw", msg => { addMessagePopoverButton("ViewRaw", msg => {
const handleClick = () => { const handleClick = () => {
if (settings.store.clickMethod === "Right") { if (settings.store.clickMethod === "Right") {
copyWithToast(msg.content); copyWithToast(msg.content);
@ -197,6 +197,6 @@ export default definePlugin({
}, },
stop() { stop() {
removeButton("ViewRaw"); removeMessagePopoverButton("ViewRaw");
} }
}); });