This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathglobalBadges.tsx
132 lines (116 loc) · 4.62 KB
/
globalBadges.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { addBadge, BadgePosition, ProfileBadge, removeBadge } from "@api/Badges";
import definePlugin, { OptionType } from "@utils/types";
import { React, Tooltip } from "@webpack/common";
import { User } from "discord-types/general";
type CustomBadge = string | {
name: string;
badge: string;
custom?: boolean;
};
interface BadgeCache {
badges: { [mod: string]: CustomBadge[]; };
expires: number;
}
const API_URL = "https://clientmodbadges-api.herokuapp.com/";
const cache = new Map<string, BadgeCache>();
const EXPIRES = 1000 * 60 * 15;
const fetchBadges = (id: string): BadgeCache["badges"] | undefined => {
const cachedValue = cache.get(id);
if (!cache.has(id) || (cachedValue && cachedValue.expires < Date.now())) {
fetch(`${API_URL}users/${id}`)
.then(res => res.json() as Promise<BadgeCache["badges"]>)
.then(body => {
cache.set(id, { badges: body, expires: Date.now() + EXPIRES });
return body;
});
} else if (cachedValue) {
return cachedValue.badges;
}
};
const BadgeComponent = ({ name, img }: { name: string, img: string; }) => {
return (
<Tooltip text={name} >
{(tooltipProps: any) => (
<img
{...tooltipProps}
src={img}
style={{ width: "22px", height: "22px", transform: name.includes("Replugged") ? "scale(0.9)" : null, margin: "0 2px" }}
/>
)}
</Tooltip>
);
};
const GlobalBadges = ({ user }: { user: User; }) => {
const [badges, setBadges] = React.useState<BadgeCache["badges"]>({});
React.useEffect(() => setBadges(fetchBadges(user.id) ?? {}), [user.id]);
if (!badges) return null;
const globalBadges: JSX.Element[] = [];
Object.keys(badges).forEach(mod => {
if (mod.toLowerCase() === "vencord") return;
badges[mod].forEach(badge => {
if (typeof badge === "string") {
const fullNames = { "hunter": "Bug Hunter", "early": "Early User" };
badge = {
name: fullNames[badge as string] ? fullNames[badge as string] : badge,
badge: `${API_URL}badges/${mod}/${(badge as string).replace(mod, "").trim().split(" ")[0]}`
};
} else if (typeof badge === "object") badge.custom = true;
if (!showCustom() && badge.custom) return;
const cleanName = badge.name.replace(mod, "").trim();
const prefix = showPrefix() ? mod : "";
if (!badge.custom) badge.name = `${prefix} ${cleanName.charAt(0).toUpperCase() + cleanName.slice(1)}`;
globalBadges.push(<BadgeComponent name={badge.name} img={badge.badge} />);
});
});
return (
<div className="vc-global-badges" style={{ alignItems: "center", display: "flex" }}>
{globalBadges}
</div>
);
};
const Badge: ProfileBadge = {
component: b => <GlobalBadges {...b} />,
position: BadgePosition.START,
shouldShow: userInfo => !!Object.keys(fetchBadges(userInfo.user.id) ?? {}).length,
key: "GlobalBadges"
};
const showPrefix = () => Vencord.Settings.plugins.GlobalBadges.showPrefix;
const showCustom = () => Vencord.Settings.plugins.GlobalBadges.showCustom;
export default definePlugin({
name: "GlobalBadges",
description: "Adds global badges from other client mods",
authors: [{ name: "HypedDomi", id: 354191516979429376n }],
start: () => addBadge(Badge),
stop: () => removeBadge(Badge),
options: {
showPrefix: {
type: OptionType.BOOLEAN,
description: "Shows the Mod as Prefix",
default: true,
restartNeeded: false
},
showCustom: {
type: OptionType.BOOLEAN,
description: "Show Custom Badges",
default: true,
restartNeeded: false
}
}
});