This commit is contained in:
parent
9ff5d1adee
commit
cbd92de7a5
4 changed files with 69 additions and 25 deletions
|
@ -9,11 +9,6 @@ export const redisTtl: number = process.env.REDIS_TTL
|
||||||
? Number.parseInt(process.env.REDIS_TTL, 10)
|
? Number.parseInt(process.env.REDIS_TTL, 10)
|
||||||
: 60 * 60 * 1; // 1 hour
|
: 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[] = [
|
export const badgeServices: badgeURLMap[] = [
|
||||||
{
|
{
|
||||||
service: "Vencord",
|
service: "Vencord",
|
||||||
|
@ -31,8 +26,12 @@ export const badgeServices: badgeURLMap[] = [
|
||||||
service: "ReviewDb",
|
service: "ReviewDb",
|
||||||
url: "https://manti.vendicated.dev/api/reviewdb/badges",
|
url: "https://manti.vendicated.dev/api/reviewdb/badges",
|
||||||
},
|
},
|
||||||
// {
|
{
|
||||||
// service: "ClientMods",
|
service: "Enmity",
|
||||||
// url: getClientModBadgesUrl,
|
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`,
|
||||||
|
}),
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -32,23 +32,23 @@ export async function fetchBadges(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let url: string;
|
const result: Badge[] = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
let url: string | { user: string; badge: (id: string) => string };
|
||||||
if (typeof entry.url === "function") {
|
if (typeof entry.url === "function") {
|
||||||
url = entry.url(userId);
|
url = entry.url(userId);
|
||||||
} else {
|
} else {
|
||||||
url = entry.url;
|
url = entry.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(url);
|
|
||||||
if (!res.ok) return;
|
|
||||||
const data = await res.json();
|
|
||||||
|
|
||||||
const result: Badge[] = [];
|
|
||||||
|
|
||||||
switch (serviceKey) {
|
switch (serviceKey) {
|
||||||
case "vencord":
|
case "vencord":
|
||||||
case "equicord": {
|
case "equicord": {
|
||||||
|
const res = await fetch(url as string);
|
||||||
|
if (!res.ok) break;
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
const userBadges = data[userId];
|
const userBadges = data[userId];
|
||||||
if (Array.isArray(userBadges)) {
|
if (Array.isArray(userBadges)) {
|
||||||
for (const b of userBadges) {
|
for (const b of userBadges) {
|
||||||
|
@ -62,6 +62,10 @@ export async function fetchBadges(
|
||||||
}
|
}
|
||||||
|
|
||||||
case "nekocord": {
|
case "nekocord": {
|
||||||
|
const res = await fetch(url as string);
|
||||||
|
if (!res.ok) break;
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
const userBadgeIds = data.users?.[userId]?.badges;
|
const userBadgeIds = data.users?.[userId]?.badges;
|
||||||
if (Array.isArray(userBadgeIds)) {
|
if (Array.isArray(userBadgeIds)) {
|
||||||
for (const id of userBadgeIds) {
|
for (const id of userBadgeIds) {
|
||||||
|
@ -78,6 +82,10 @@ export async function fetchBadges(
|
||||||
}
|
}
|
||||||
|
|
||||||
case "reviewdb": {
|
case "reviewdb": {
|
||||||
|
const res = await fetch(url as string);
|
||||||
|
if (!res.ok) break;
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
for (const b of data) {
|
for (const b of data) {
|
||||||
if (b.discordID === userId) {
|
if (b.discordID === userId) {
|
||||||
result.push({
|
result.push({
|
||||||
|
@ -88,6 +96,37 @@ export async function fetchBadges(
|
||||||
}
|
}
|
||||||
break;
|
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) {
|
if (result.length > 0) {
|
||||||
|
|
11
types/badge.d.ts
vendored
11
types/badge.d.ts
vendored
|
@ -9,3 +9,14 @@ interface FetchBadgesOptions {
|
||||||
nocache?: boolean;
|
nocache?: boolean;
|
||||||
separated?: 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
5
types/config.d.ts
vendored
|
@ -3,8 +3,3 @@ type Environment = {
|
||||||
host: string;
|
host: string;
|
||||||
development: boolean;
|
development: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type badgeURLMap = {
|
|
||||||
service: string;
|
|
||||||
url: string | ((userId: string) => string);
|
|
||||||
};
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue