Fix badges

This commit is contained in:
Vendicated 2023-04-13 21:04:19 +02:00
parent c6f0d0763c
commit c8817e805f
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905
2 changed files with 28 additions and 20 deletions

View file

@ -29,11 +29,12 @@ export enum BadgePosition {
export interface ProfileBadge { export interface ProfileBadge {
/** The tooltip to show on hover. Required for image badges */ /** The tooltip to show on hover. Required for image badges */
tooltip?: string; description?: string;
/** Custom component for the badge (tooltip not included) */ /** Custom component for the badge (tooltip not included) */
component?: ComponentType<ProfileBadge & BadgeUserArgs>; component?: ComponentType<ProfileBadge & BadgeUserArgs>;
/** The custom image to use */ /** The custom image to use */
image?: string; image?: string;
link?: string;
/** Action to perform when you click the badge */ /** Action to perform when you click the badge */
onClick?(): void; onClick?(): void;
/** Should the user display this badge? */ /** Should the user display this badge? */
@ -69,17 +70,19 @@ export function removeBadge(badge: ProfileBadge) {
* Inject badges into the profile badges array. * Inject badges into the profile badges array.
* You probably don't need to use this. * 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) { for (const badge of Badges) {
if (!badge.shouldShow || badge.shouldShow(args)) { if (!badge.shouldShow || badge.shouldShow(args)) {
badge.position === BadgePosition.START badge.position === BadgePosition.START
? badgeArray.unshift({ ...badge, ...args }) ? badges.unshift({ ...badge, ...args })
: badgeArray.push({ ...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 { export interface BadgeUserArgs {

View file

@ -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 contributorIds: string[] = Object.values(Devs).map(d => d.id.toString());
const ContributorBadge: ProfileBadge = { const ContributorBadge: ProfileBadge = {
tooltip: "Vencord Contributor", description: "Vencord Contributor",
image: CONTRIBUTOR_BADGE, image: CONTRIBUTOR_BADGE,
position: BadgePosition.START, position: BadgePosition.START,
props: { props: {
@ -45,10 +45,10 @@ const ContributorBadge: ProfileBadge = {
} }
}, },
shouldShow: ({ user }) => contributorIds.includes(user.id), 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<string, Pick<ProfileBadge, "image" | "tooltip">>; const DonorBadges = {} as Record<string, Pick<ProfileBadge, "image" | "description">>;
export default definePlugin({ export default definePlugin({
name: "BadgeAPI", name: "BadgeAPI",
@ -58,10 +58,10 @@ export default definePlugin({
patches: [ patches: [
/* Patch the badges array */ /* Patch the badges array */
{ {
find: "Messages.PROFILE_USER_BADGES,", find: "Messages.ACTIVE_DEVELOPER_BADGE_TOOLTIP",
replacement: { replacement: {
match: /&&((\i)\.push\({tooltip:\i\.\i\.Messages\.PREMIUM_GUILD_SUBSCRIPTION_TOOLTIP\.format.+?;)(?:return\s\i;?})/, match: /(?<=void 0:)\i.getBadges\(\)/,
replace: (_, m, badgeArray) => `&&${m} return Vencord.Api.Badges.inject(${badgeArray}, arguments[0]);}`, replace: "Vencord.Api.Badges._getBadges(arguments[0]).concat($&??[])",
} }
}, },
/* Patch the badge list component on user profiles */ /* Patch the badge list component on user profiles */
@ -69,13 +69,18 @@ export default definePlugin({
find: "Messages.PROFILE_USER_BADGES,role:", find: "Messages.PROFILE_USER_BADGES,role:",
replacement: [ replacement: [
{ {
match: /src:(\i)\[(\i)\.key\],/g, // alt: "", aria-hidden: false, src: originalSrc
// <img src={badge.image ?? imageMap[badge.key]} {...badge.props} /> match: /alt:" ","aria-hidden":!0,src:(?=.{0,10}\b(\i)\.(?:icon|key))/g,
replace: (_, imageMap, badge) => `src: ${badge}.image ?? ${imageMap}[${badge}.key], ...${badge}.props,` // ...badge.props, ..., src: badge.image ?? ...
replace: "...$1.props,$& $1.image??"
}, },
{ {
match: /children:function(?<=(\i)\.(?:tooltip|description),spacing:\d.+?)/g, match: /children:function(?<=(\i)\.(?:tooltip|description),spacing:\d.+?)/g,
replace: "children:$1.component ? () => $self.renderBadgeComponent($1) : function" 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; return;
} }
for (const line of lines) { for (const line of lines) {
const [id, tooltip, image] = line.split(","); const [id, description, image] = line.split(",");
DonorBadges[id] = { image, tooltip }; DonorBadges[id] = { image, description };
} }
}, },
addDonorBadge(badges: ProfileBadge[], userId: string) { getDonorBadge(userId: string) {
const badge = DonorBadges[userId]; const badge = DonorBadges[userId];
if (badge) { if (badge) {
badges.unshift({ return {
...badge, ...badge,
position: BadgePosition.START, position: BadgePosition.START,
props: { props: {
@ -167,7 +172,7 @@ export default definePlugin({
</ErrorBoundary> </ErrorBoundary>
)); ));
}, },
}); };
} }
} }
}); });