From c8817e805f9d0ea2b5cff370eb0c9e10b7a60f59 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Thu, 13 Apr 2023 21:04:19 +0200 Subject: [PATCH] Fix badges --- src/api/Badges.ts | 15 +++++++++------ src/plugins/apiBadges.tsx | 33 +++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/api/Badges.ts b/src/api/Badges.ts index d4aabaf21..9abaefe2b 100644 --- a/src/api/Badges.ts +++ b/src/api/Badges.ts @@ -29,11 +29,12 @@ export enum BadgePosition { export interface ProfileBadge { /** The tooltip to show on hover. Required for image badges */ - tooltip?: string; + description?: string; /** Custom component for the badge (tooltip not included) */ component?: ComponentType; /** The custom image to use */ image?: string; + link?: string; /** Action to perform when you click the badge */ onClick?(): void; /** Should the user display this badge? */ @@ -69,17 +70,19 @@ export function removeBadge(badge: ProfileBadge) { * Inject badges into the profile badges array. * You probably don't need to use this. */ -export function inject(badgeArray: ProfileBadge[], args: BadgeUserArgs) { +export function _getBadges(args: BadgeUserArgs) { + const badges = [] as ProfileBadge[]; for (const badge of Badges) { if (!badge.shouldShow || badge.shouldShow(args)) { badge.position === BadgePosition.START - ? badgeArray.unshift({ ...badge, ...args }) - : badgeArray.push({ ...badge, ...args }); + ? badges.unshift({ ...badge, ...args }) + : badges.push({ ...badge, ...args }); } } - (Plugins.BadgeAPI as any).addDonorBadge(badgeArray, args.user.id); + const donorBadge = (Plugins.BadgeAPI as any).getDonorBadge(args.user.id); + if (donorBadge) badges.unshift(donorBadge); - return badgeArray; + return badges; } export interface BadgeUserArgs { diff --git a/src/plugins/apiBadges.tsx b/src/plugins/apiBadges.tsx index 975a7d662..6167dd002 100644 --- a/src/plugins/apiBadges.tsx +++ b/src/plugins/apiBadges.tsx @@ -35,7 +35,7 @@ const CONTRIBUTOR_BADGE = "https://cdn.discordapp.com/attachments/10336802034336 const contributorIds: string[] = Object.values(Devs).map(d => d.id.toString()); const ContributorBadge: ProfileBadge = { - tooltip: "Vencord Contributor", + description: "Vencord Contributor", image: CONTRIBUTOR_BADGE, position: BadgePosition.START, props: { @@ -45,10 +45,10 @@ const ContributorBadge: ProfileBadge = { } }, shouldShow: ({ user }) => contributorIds.includes(user.id), - onClick: () => VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/Vendicated/Vencord") + link: "https://github.com/Vendicated/Vencord" }; -const DonorBadges = {} as Record>; +const DonorBadges = {} as Record>; export default definePlugin({ name: "BadgeAPI", @@ -58,10 +58,10 @@ export default definePlugin({ patches: [ /* Patch the badges array */ { - find: "Messages.PROFILE_USER_BADGES,", + find: "Messages.ACTIVE_DEVELOPER_BADGE_TOOLTIP", replacement: { - match: /&&((\i)\.push\({tooltip:\i\.\i\.Messages\.PREMIUM_GUILD_SUBSCRIPTION_TOOLTIP\.format.+?;)(?:return\s\i;?})/, - replace: (_, m, badgeArray) => `&&${m} return Vencord.Api.Badges.inject(${badgeArray}, arguments[0]);}`, + match: /(?<=void 0:)\i.getBadges\(\)/, + replace: "Vencord.Api.Badges._getBadges(arguments[0]).concat($&??[])", } }, /* Patch the badge list component on user profiles */ @@ -69,13 +69,18 @@ export default definePlugin({ find: "Messages.PROFILE_USER_BADGES,role:", replacement: [ { - match: /src:(\i)\[(\i)\.key\],/g, - // - replace: (_, imageMap, badge) => `src: ${badge}.image ?? ${imageMap}[${badge}.key], ...${badge}.props,` + // alt: "", aria-hidden: false, src: originalSrc + match: /alt:" ","aria-hidden":!0,src:(?=.{0,10}\b(\i)\.(?:icon|key))/g, + // ...badge.props, ..., src: badge.image ?? ... + replace: "...$1.props,$& $1.image??" }, { match: /children:function(?<=(\i)\.(?:tooltip|description),spacing:\d.+?)/g, replace: "children:$1.component ? () => $self.renderBadgeComponent($1) : function" + }, + { + match: /onClick:function(?=.{0,200}href:(\i)\.link)/, + replace: "onClick:$1.onClick??function" } ] } @@ -95,15 +100,15 @@ export default definePlugin({ return; } for (const line of lines) { - const [id, tooltip, image] = line.split(","); - DonorBadges[id] = { image, tooltip }; + const [id, description, image] = line.split(","); + DonorBadges[id] = { image, description }; } }, - addDonorBadge(badges: ProfileBadge[], userId: string) { + getDonorBadge(userId: string) { const badge = DonorBadges[userId]; if (badge) { - badges.unshift({ + return { ...badge, position: BadgePosition.START, props: { @@ -167,7 +172,7 @@ export default definePlugin({ )); }, - }); + }; } } });