add Enmity shame!
All checks were successful
Code quality checks / biome (push) Successful in 13s

This commit is contained in:
creations 2025-04-19 20:47:06 -04:00
parent 9ff5d1adee
commit cbd92de7a5
Signed by: creations
GPG key ID: 8F553AA4320FC711
4 changed files with 69 additions and 25 deletions

View file

@ -9,11 +9,6 @@ export const redisTtl: number = process.env.REDIS_TTL
? Number.parseInt(process.env.REDIS_TTL, 10)
: 60 * 60 * 1; // 1 hour
// not sure the point ?
// function getClientModBadgesUrl(userId: string): string {
// return `https://cdn.jsdelivr.net/gh/Equicord/ClientModBadges-API@main/users/${userId}.json`;
// }
export const badgeServices: badgeURLMap[] = [
{
service: "Vencord",
@ -31,8 +26,12 @@ export const badgeServices: badgeURLMap[] = [
service: "ReviewDb",
url: "https://manti.vendicated.dev/api/reviewdb/badges",
},
// {
// service: "ClientMods",
// url: getClientModBadgesUrl,
// }
{
service: "Enmity",
url: (userId: string) => ({
user: `https://raw.githubusercontent.com/enmity-mod/badges/main/${userId}.json`,
badge: (id: string) =>
`https://raw.githubusercontent.com/enmity-mod/badges/main/data/${id}.json`,
}),
},
];

View file

@ -32,23 +32,23 @@ export async function fetchBadges(
}
}
let url: string;
if (typeof entry.url === "function") {
url = entry.url(userId);
} else {
url = entry.url;
}
const result: Badge[] = [];
try {
const res = await fetch(url);
if (!res.ok) return;
const data = await res.json();
const result: Badge[] = [];
let url: string | { user: string; badge: (id: string) => string };
if (typeof entry.url === "function") {
url = entry.url(userId);
} else {
url = entry.url;
}
switch (serviceKey) {
case "vencord":
case "equicord": {
const res = await fetch(url as string);
if (!res.ok) break;
const data = await res.json();
const userBadges = data[userId];
if (Array.isArray(userBadges)) {
for (const b of userBadges) {
@ -62,6 +62,10 @@ export async function fetchBadges(
}
case "nekocord": {
const res = await fetch(url as string);
if (!res.ok) break;
const data = await res.json();
const userBadgeIds = data.users?.[userId]?.badges;
if (Array.isArray(userBadgeIds)) {
for (const id of userBadgeIds) {
@ -78,6 +82,10 @@ export async function fetchBadges(
}
case "reviewdb": {
const res = await fetch(url as string);
if (!res.ok) break;
const data = await res.json();
for (const b of data) {
if (b.discordID === userId) {
result.push({
@ -88,6 +96,37 @@ export async function fetchBadges(
}
break;
}
case "enmity": {
if (
typeof url !== "object" ||
typeof url.user !== "string" ||
typeof url.badge !== "function"
)
break;
const userRes = await fetch(url.user);
if (!userRes.ok) break;
const badgeIds: string[] = await userRes.json();
if (!Array.isArray(badgeIds)) break;
await Promise.all(
badgeIds.map(async (id) => {
const badgeRes = await fetch(url.badge(id));
if (!badgeRes.ok) return;
const badge = await badgeRes.json();
if (!badge?.name || !badge?.url?.dark) return;
result.push({
tooltip: badge.name,
badge: badge.url.dark,
});
}),
);
break;
}
}
if (result.length > 0) {

11
types/badge.d.ts vendored
View file

@ -9,3 +9,14 @@ interface FetchBadgesOptions {
nocache?: boolean;
separated?: boolean;
}
type badgeURLMap = {
service: string;
url:
| string
| ((userId: string) => string)
| ((userId: string) => {
user: string;
badge: (id: string) => string;
});
};

5
types/config.d.ts vendored
View file

@ -3,8 +3,3 @@ type Environment = {
host: string;
development: boolean;
};
type badgeURLMap = {
service: string;
url: string | ((userId: string) => string);
};