refactor(badges): made badges work and made them for kettu not bunny
This commit is contained in:
parent
3da8b08c83
commit
4c2e327ad3
3 changed files with 63 additions and 37 deletions
|
|
@ -74,7 +74,7 @@ const config = {
|
|||
constModules: {
|
||||
globals: {
|
||||
"bunny-build-info": {
|
||||
version: `"1.2.1"`
|
||||
version: `"1.2.2"`
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { after } from "@lib/api/patcher";
|
|||
import { onJsxCreate } from "@lib/api/react/jsx";
|
||||
import { findByName } from "@metro";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { defineCorePlugin } from "..";
|
||||
|
||||
interface BunnyBadge {
|
||||
|
|
@ -15,52 +14,79 @@ const useBadgesModule = findByName("useBadges", false);
|
|||
export default defineCorePlugin({
|
||||
manifest: {
|
||||
id: "bunny.badges",
|
||||
version: "1.0.0",
|
||||
version: "1.1.0",
|
||||
type: "plugin",
|
||||
spec: 3,
|
||||
main: "",
|
||||
display: {
|
||||
name: "Badges",
|
||||
description: "Adds badges to user's profile",
|
||||
authors: [{ name: "pylixonly" }]
|
||||
authors: [{ name: "cocobo1"}, { name: "pylixonly" }]
|
||||
}
|
||||
},
|
||||
|
||||
start() {
|
||||
const propHolder = {} as Record<string, any>;
|
||||
const badgeCache = {} as Record<string, BunnyBadge[]>;
|
||||
|
||||
onJsxCreate("RenderedBadge", (_, ret) => {
|
||||
if (ret.props.id.match(/bunny-\d+-\d+/)) {
|
||||
Object.assign(ret.props, propHolder[ret.props.id]);
|
||||
const badgeProps = {} as Record<string, any>;
|
||||
|
||||
onJsxCreate("ProfileBadge", (component, ret) => {
|
||||
if (ret.props.id?.startsWith("bunny-")) {
|
||||
const cachedProps = badgeProps[ret.props.id];
|
||||
if (cachedProps) {
|
||||
ret.props.source = cachedProps.source;
|
||||
ret.props.label = cachedProps.label;
|
||||
ret.props.id = cachedProps.id;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
after("default", useBadgesModule, ([user], r) => {
|
||||
const [badges, setBadges] = useState<BunnyBadge[]>(user ? badgeCache[user.userId] ??= [] : []);
|
||||
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
fetch(`https://raw.githubusercontent.com/pyoncord/badges/refs/heads/main/${user.userId}.json`)
|
||||
.then(r => r.json())
|
||||
.then(badges => setBadges(badgeCache[user.userId] = badges));
|
||||
|
||||
onJsxCreate("RenderedBadge", (component, ret) => {
|
||||
if (ret.props.id?.startsWith("bunny-")) {
|
||||
const cachedProps = badgeProps[ret.props.id];
|
||||
if (cachedProps) {
|
||||
Object.assign(ret.props, cachedProps);
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
if (user) {
|
||||
badges.forEach((badges, i) => {
|
||||
propHolder[`bunny-${user.userId}-${i}`] = {
|
||||
source: { uri: badges.url },
|
||||
id: `bunny-${i}`,
|
||||
label: badges.label
|
||||
}
|
||||
});
|
||||
|
||||
after("default", useBadgesModule, ([user], result) => {
|
||||
const [badges, setBadges] = useState<BunnyBadge[]>(
|
||||
user ? badgeCache[user.userId] || [] : []
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (user && !badgeCache[user.userId]) {
|
||||
fetch(`https://raw.githubusercontent.com/ra1ncord/badges/refs/heads/main/${user.userId}.json`)
|
||||
.then(r => r.json())
|
||||
.then(fetchedBadges => {
|
||||
badgeCache[user.userId] = fetchedBadges;
|
||||
setBadges(fetchedBadges);
|
||||
})
|
||||
//user has no badges, but maybe they should get some by contributing
|
||||
.catch(() => {
|
||||
badgeCache[user.userId] = [];
|
||||
});
|
||||
}
|
||||
}, [user?.userId]);
|
||||
|
||||
if (user && badges.length > 0) {
|
||||
badges.forEach((badge, i) => {
|
||||
const badgeId = `bunny-${user.userId}-${i}`;
|
||||
|
||||
badgeProps[badgeId] = {
|
||||
id: badgeId,
|
||||
source: { uri: badge.url },
|
||||
label: badge.label,
|
||||
userId: user.userId,
|
||||
};
|
||||
|
||||
r.push({
|
||||
id: `bunny-${user.userId}-${i}`,
|
||||
description: badges.label,
|
||||
icon: "_",
|
||||
result.push({
|
||||
id: badgeId,
|
||||
description: badge.label,
|
||||
icon: "dummy",
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -51,12 +51,6 @@ export default function initSettings() {
|
|||
]
|
||||
});
|
||||
|
||||
// Compat with Vendetta Plugins that use configs in settings
|
||||
registerSection({
|
||||
name: "Vendetta",
|
||||
items: []
|
||||
});
|
||||
|
||||
// Compat with Bunny Plugins that use configs in settings
|
||||
registerSection({
|
||||
name: "Bunny",
|
||||
|
|
@ -68,4 +62,10 @@ export default function initSettings() {
|
|||
name: "Revenge",
|
||||
items: []
|
||||
});
|
||||
|
||||
// Compat with Vendetta Plugins that use configs in settings
|
||||
registerSection({
|
||||
name: "Vendetta",
|
||||
items: []
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue