fix MoreUserTags (#2599)

This commit is contained in:
ryan-0324 2024-06-19 10:56:01 -04:00 committed by GitHub
parent d12624ac4b
commit 920f326053
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 30 deletions

View file

@ -19,7 +19,6 @@
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 { Settings } from "@api/Settings"; import { Settings } from "@api/Settings";
import { onceDefined } from "@shared/onceDefined";
import { Logger } from "@utils/Logger"; import { Logger } from "@utils/Logger";
import { canonicalizeFind } from "@utils/patches"; import { canonicalizeFind } from "@utils/patches";
import { Patch, Plugin, ReporterTestable, StartAt } from "@utils/types"; import { Patch, Plugin, ReporterTestable, StartAt } from "@utils/types";
@ -34,7 +33,7 @@ const logger = new Logger("PluginManager", "#a6d189");
export const PMLogger = logger; export const PMLogger = logger;
export const plugins = Plugins; export const plugins = Plugins;
export let patches = [] as Patch[]; export const patches = [] as Patch[];
/** Whether we have subscribed to flux events of all the enabled plugins when FluxDispatcher was ready */ /** Whether we have subscribed to flux events of all the enabled plugins when FluxDispatcher was ready */
let enabledPluginsSubscribedFlux = false; let enabledPluginsSubscribedFlux = false;
@ -43,9 +42,6 @@ const subscribedFluxEventsPlugins = new Set<string>();
const pluginsValues = Object.values(Plugins); const pluginsValues = Object.values(Plugins);
const settings = Settings.plugins; const settings = Settings.plugins;
const forceDisabled = new Set([
"MoreUserTags"
]);
export function isPluginEnabled(p: string) { export function isPluginEnabled(p: string) {
return ( return (
Plugins[p]?.required || Plugins[p]?.required ||
@ -126,17 +122,9 @@ for (const p of pluginsValues) {
} }
} }
onceDefined(window, "GLOBAL_ENV", v => {
if (v.SENTRY_TAGS.buildId !== "366c746173a6ca0a801e9f4a4d7b6745e6de45d4") {
patches = patches.filter(p => !forceDisabled.has(p.plugin));
}
});
export const startAllPlugins = traceFunction("startAllPlugins", function startAllPlugins(target: StartAt) { export const startAllPlugins = traceFunction("startAllPlugins", function startAllPlugins(target: StartAt) {
logger.info(`Starting plugins (stage ${target})`); logger.info(`Starting plugins (stage ${target})`);
for (const name in Plugins) { for (const name in Plugins) {
if (window.GLOBAL_ENV?.SENTRY_TAGS.buildId !== "366c746173a6ca0a801e9f4a4d7b6745e6de45d4" && forceDisabled.has(name)) continue;
if (isPluginEnabled(name) && (!IS_REPORTER || isReporterTestable(Plugins[name], ReporterTestable.Start))) { if (isPluginEnabled(name) && (!IS_REPORTER || isReporterTestable(Plugins[name], ReporterTestable.Start))) {
const p = Plugins[name]; const p = Plugins[name];
@ -207,7 +195,7 @@ 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, flux, contextMenus } = p; const { name, commands, contextMenus } = p;
if (p.start) { if (p.start) {
logger.info("Starting plugin", name); logger.info("Starting plugin", name);
@ -253,7 +241,7 @@ export const startPlugin = traceFunction("startPlugin", function startPlugin(p:
}, 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, flux, contextMenus } = p; const { name, commands, contextMenus } = p;
if (p.stop) { if (p.stop) {
logger.info("Stopping plugin", name); logger.info("Stopping plugin", name);

View file

@ -21,12 +21,10 @@ import { Flex } from "@components/Flex";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy, findLazy } from "@webpack"; import { findByCodeLazy, findLazy } from "@webpack";
import { Card, ChannelStore, Forms, GuildStore, PermissionsBits, Switch, TextInput, Tooltip, useState } from "@webpack/common"; import { Card, ChannelStore, Forms, GuildStore, PermissionsBits, Switch, TextInput, Tooltip, useState } from "@webpack/common";
import { RC } from "@webpack/types"; import type { Permissions, RC } from "@webpack/types";
import { Channel, Message, User } from "discord-types/general"; import type { Channel, Guild, Message, User } from "discord-types/general";
type PermissionName = "CREATE_INSTANT_INVITE" | "KICK_MEMBERS" | "BAN_MEMBERS" | "ADMINISTRATOR" | "MANAGE_CHANNELS" | "MANAGE_GUILD" | "CHANGE_NICKNAME" | "MANAGE_NICKNAMES" | "MANAGE_ROLES" | "MANAGE_WEBHOOKS" | "MANAGE_GUILD_EXPRESSIONS" | "CREATE_GUILD_EXPRESSIONS" | "VIEW_AUDIT_LOG" | "VIEW_CHANNEL" | "VIEW_GUILD_ANALYTICS" | "VIEW_CREATOR_MONETIZATION_ANALYTICS" | "MODERATE_MEMBERS" | "SEND_MESSAGES" | "SEND_TTS_MESSAGES" | "MANAGE_MESSAGES" | "EMBED_LINKS" | "ATTACH_FILES" | "READ_MESSAGE_HISTORY" | "MENTION_EVERYONE" | "USE_EXTERNAL_EMOJIS" | "ADD_REACTIONS" | "USE_APPLICATION_COMMANDS" | "MANAGE_THREADS" | "CREATE_PUBLIC_THREADS" | "CREATE_PRIVATE_THREADS" | "USE_EXTERNAL_STICKERS" | "SEND_MESSAGES_IN_THREADS" | "CONNECT" | "SPEAK" | "MUTE_MEMBERS" | "DEAFEN_MEMBERS" | "MOVE_MEMBERS" | "USE_VAD" | "PRIORITY_SPEAKER" | "STREAM" | "USE_EMBEDDED_ACTIVITIES" | "USE_SOUNDBOARD" | "USE_EXTERNAL_SOUNDS" | "REQUEST_TO_SPEAK" | "MANAGE_EVENTS" | "CREATE_EVENTS";
interface Tag { interface Tag {
// name used for identifying, must be alphanumeric + underscores // name used for identifying, must be alphanumeric + underscores
@ -34,7 +32,7 @@ interface Tag {
// name shown on the tag itself, can be anything probably; automatically uppercase'd // name shown on the tag itself, can be anything probably; automatically uppercase'd
displayName: string; displayName: string;
description: string; description: string;
permissions?: PermissionName[]; permissions?: Permissions[];
condition?(message: Message | null, user: User, channel: Channel): boolean; condition?(message: Message | null, user: User, channel: Channel): boolean;
} }
@ -54,10 +52,14 @@ interface TagSettings {
[k: string]: TagSetting; [k: string]: TagSetting;
} }
// PermissionStore.computePermissions is not the same function and doesn't work here // PermissionStore.computePermissions will not work here since it only gets permissions for the current user
const PermissionUtil = findByPropsLazy("computePermissions", "canEveryoneRole") as { const computePermissions: (options: {
computePermissions({ ...args }): bigint; user?: { id: string; } | string | null;
}; context?: Guild | Channel | null;
overwrites?: Channel["permissionOverwrites"] | null;
checkElevated?: boolean /* = true */;
excludeGuildPermissions?: boolean /* = false */;
}) => bigint = findByCodeLazy(".getCurrentUser()", ".computeLurkerPermissionsAllowList()");
const Tag = findLazy(m => m.Types?.[0] === "BOT") as RC<{ type?: number, className?: string, useRemSizes?: boolean; }> & { Types: Record<string, number>; }; const Tag = findLazy(m => m.Types?.[0] === "BOT") as RC<{ type?: number, className?: string, useRemSizes?: boolean; }> & { Types: Record<string, number>; };
@ -193,7 +195,7 @@ export default definePlugin({
patches: [ patches: [
// add tags to the tag list // add tags to the tag list
{ {
find: "BotTagTypes:", find: ".ORIGINAL_POSTER=",
replacement: { replacement: {
match: /\((\i)=\{\}\)\)\[(\i)\.BOT/, match: /\((\i)=\{\}\)\)\[(\i)\.BOT/,
replace: "($1=$self.getTagTypes()))[$2.BOT" replace: "($1=$self.getTagTypes()))[$2.BOT"
@ -222,7 +224,7 @@ export default definePlugin({
}, },
// in messages // in messages
{ {
find: "renderSystemTag:", find: ".Types.ORIGINAL_POSTER",
replacement: { replacement: {
match: /;return\((\(null==\i\?void 0:\i\.isSystemDM\(\).+?.Types.ORIGINAL_POSTER\)),null==(\i)\)/, match: /;return\((\(null==\i\?void 0:\i\.isSystemDM\(\).+?.Types.ORIGINAL_POSTER\)),null==(\i)\)/,
replace: ";$1;$2=$self.getTag({...arguments[0],origType:$2,location:'chat'});return $2 == null" replace: ";$1;$2=$self.getTag({...arguments[0],origType:$2,location:'chat'});return $2 == null"
@ -283,7 +285,7 @@ export default definePlugin({
const guild = GuildStore.getGuild(channel?.guild_id); const guild = GuildStore.getGuild(channel?.guild_id);
if (!guild) return []; if (!guild) return [];
const permissions = PermissionUtil.computePermissions({ user, context: guild, overwrites: channel.permissionOverwrites }); const permissions = computePermissions({ user, context: guild, overwrites: channel.permissionOverwrites });
return Object.entries(PermissionsBits) return Object.entries(PermissionsBits)
.map(([perm, permInt]) => .map(([perm, permInt]) =>
permissions & permInt ? perm : "" permissions & permInt ? perm : ""
@ -330,7 +332,7 @@ export default definePlugin({
}: { }: {
message?: Message, message?: Message,
user: User & { isClyde(): boolean; }, user: User & { isClyde(): boolean; },
channel?: Channel & { isForumPost(): boolean; }, channel?: Channel & { isForumPost(): boolean; isMediaPost(): boolean; },
channelId?: string; channelId?: string;
origType?: number; origType?: number;
location: "chat" | "not-chat"; location: "chat" | "not-chat";
@ -367,7 +369,7 @@ export default definePlugin({
tag.permissions?.some(perm => perms.includes(perm)) || tag.permissions?.some(perm => perms.includes(perm)) ||
(tag.condition?.(message!, user, channel)) (tag.condition?.(message!, user, channel))
) { ) {
if (channel.isForumPost() && channel.ownerId === user.id) if ((channel.isForumPost() || channel.isMediaPost()) && channel.ownerId === user.id)
type = Tag.Types[`${tag.name}-OP`]; type = Tag.Types[`${tag.name}-OP`];
else if (user.bot && !isWebhook(message!, user) && !settings.dontShowBotTag) else if (user.bot && !isWebhook(message!, user) && !settings.dontShowBotTag)
type = Tag.Types[`${tag.name}-BOT`]; type = Tag.Types[`${tag.name}-BOT`];