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 {
/** The tooltip to show on hover. Required for image badges */
tooltip?: string;
description?: string;
/** Custom component for the badge (tooltip not included) */
component?: ComponentType<ProfileBadge & BadgeUserArgs>;
/** 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 {

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 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<string, Pick<ProfileBadge, "image" | "tooltip">>;
const DonorBadges = {} as Record<string, Pick<ProfileBadge, "image" | "description">>;
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,
// <img src={badge.image ?? imageMap[badge.key]} {...badge.props} />
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({
</ErrorBoundary>
));
},
});
};
}
}
});