feat(USRBG): update to new API (#2388)

This commit is contained in:
katlyn 2024-05-03 06:51:53 -08:00 committed by GitHub
parent 315f4f4e58
commit 1af44b25f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,9 +24,14 @@ import definePlugin, { OptionType } from "@utils/types";
import style from "./index.css?managed"; import style from "./index.css?managed";
const BASE_URL = "https://raw.githubusercontent.com/AutumnVN/usrbg/main/usrbg.json"; const API_URL = "https://usrbg.is-hardly.online/users";
let data = {} as Record<string, string>; interface UsrbgApiReturn {
endpoint: string
bucket: string
prefix: string
users: Record<string, string>
}
const settings = definePluginSettings({ const settings = definePluginSettings({
nitroFirst: { nitroFirst: {
@ -48,7 +53,7 @@ const settings = definePluginSettings({
export default definePlugin({ export default definePlugin({
name: "USRBG", name: "USRBG",
description: "Displays user banners from USRBG, allowing anyone to get a banner without Nitro", description: "Displays user banners from USRBG, allowing anyone to get a banner without Nitro",
authors: [Devs.AutumnVN, Devs.pylix, Devs.TheKodeToad], authors: [Devs.AutumnVN, Devs.katlyn, Devs.pylix, Devs.TheKodeToad],
settings, settings,
patches: [ patches: [
{ {
@ -80,8 +85,7 @@ export default definePlugin({
} }
], ],
data: null as UsrbgApiReturn | null,
data,
settingsAboutComponent: () => { settingsAboutComponent: () => {
return ( return (
@ -91,9 +95,9 @@ export default definePlugin({
voiceBackgroundHook({ className, participantUserId }: any) { voiceBackgroundHook({ className, participantUserId }: any) {
if (className.includes("tile_")) { if (className.includes("tile_")) {
if (data[participantUserId]) { if (this.userHasBackground(participantUserId)) {
return { return {
backgroundImage: `url(${data[participantUserId]})`, backgroundImage: `url(${this.getImageUrl(participantUserId)})`,
backgroundSize: "cover", backgroundSize: "cover",
backgroundPosition: "center", backgroundPosition: "center",
backgroundRepeat: "no-repeat" backgroundRepeat: "no-repeat"
@ -104,24 +108,35 @@ export default definePlugin({
useBannerHook({ displayProfile, user }: any) { useBannerHook({ displayProfile, user }: any) {
if (displayProfile?.banner && settings.store.nitroFirst) return; if (displayProfile?.banner && settings.store.nitroFirst) return;
if (data[user.id]) return data[user.id]; if (this.userHasBackground(user.id)) return this.getImageUrl(user.id);
}, },
premiumHook({ userId }: any) { premiumHook({ userId }: any) {
if (data[userId]) return 2; if (this.userHasBackground(userId)) return 2;
}, },
shouldShowBadge({ displayProfile, user }: any) { shouldShowBadge({ displayProfile, user }: any) {
return displayProfile?.banner && (!data[user.id] || settings.store.nitroFirst); return displayProfile?.banner && (!this.userHasBackground(user.id) || settings.store.nitroFirst);
},
userHasBackground(userId: string) {
return !!this.data?.users[userId];
},
getImageUrl(userId: string): string|null {
if (!this.userHasBackground(userId)) return null;
// We can assert that data exists because userHasBackground returned true
const { endpoint, bucket, prefix, users: { [userId]: etag } } = this.data!;
return `${endpoint}/${bucket}/${prefix}${userId}?${etag}`;
}, },
async start() { async start() {
enableStyle(style); enableStyle(style);
const res = await fetch(BASE_URL); const res = await fetch(API_URL);
if (res.ok) { if (res.ok) {
data = await res.json(); this.data = await res.json();
this.data = data;
} }
} }
}); });