Compare commits

..

1 commit
main ... main

Author SHA1 Message Date
wont-stream
06c955e1b7 Add Discord Badges 2025-04-19 21:20:21 -04:00
8 changed files with 155 additions and 210 deletions

View file

@ -5,5 +5,4 @@ PORT=8080
REDIS_URL=redis://username:password@localhost:6379 REDIS_URL=redis://username:password@localhost:6379
REDIS_TTL=3600 # seconds REDIS_TTL=3600 # seconds
# if you wish to get discord badges DISCORD_TOKEN=discord_bot_token
DISCORD_TOKEN=discord_bot_token

View file

@ -37,9 +37,6 @@ REDIS_URL=redis://username:password@localhost:6379
# Value is in seconds # Value is in seconds
REDIS_TTL=3600 REDIS_TTL=3600
#only use this if you want to show discord badges
DISCORD_TOKEN=discord_bot_token
``` ```
## Endpoint ## Endpoint

View file

@ -1,87 +0,0 @@
export const discordBadges = {
// User badges
HYPESQUAD: 2 << 2,
HYPESQUAD_ONLINE_HOUSE_1: 2 << 6,
HYPESQUAD_ONLINE_HOUSE_2: 2 << 7,
HYPESQUAD_ONLINE_HOUSE_3: 2 << 8,
STAFF: 2 << 0,
PARTNER: 2 << 1,
CERTIFIED_MODERATOR: 2 << 18,
VERIFIED_DEVELOPER: 2 << 17,
ACTIVE_DEVELOPER: 2 << 22,
PREMIUM_EARLY_SUPPORTER: 2 << 9,
BUG_HUNTER_LEVEL_1: 2 << 3,
BUG_HUNTER_LEVEL_2: 2 << 14,
// Bot badges
SUPPORTS_COMMANDS: 2 << 23,
USES_AUTOMOD: 2 << 24,
};
export const discordBadgeDetails = {
HYPESQUAD: {
tooltip: "HypeSquad Events",
icon: "/public/badges/discord/HYPESQUAD.svg",
},
HYPESQUAD_ONLINE_HOUSE_1: {
tooltip: "HypeSquad Bravery",
icon: "/public/badges/discord/HYPESQUAD_ONLINE_HOUSE_1.svg",
},
HYPESQUAD_ONLINE_HOUSE_2: {
tooltip: "HypeSquad Brilliance",
icon: "/public/badges/discord/HYPESQUAD_ONLINE_HOUSE_2.svg",
},
HYPESQUAD_ONLINE_HOUSE_3: {
tooltip: "HypeSquad Balance",
icon: "/public/badges/discord/HYPESQUAD_ONLINE_HOUSE_3.svg",
},
STAFF: {
tooltip: "Discord Staff",
icon: "/public/badges/discord/STAFF.svg",
},
PARTNER: {
tooltip: "Discord Partner",
icon: "/public/badges/discord/PARTNER.svg",
},
CERTIFIED_MODERATOR: {
tooltip: "Certified Moderator",
icon: "/public/badges/discord/CERTIFIED_MODERATOR.svg",
},
VERIFIED_DEVELOPER: {
tooltip: "Verified Bot Developer",
icon: "/public/badges/discord/VERIFIED_DEVELOPER.svg",
},
ACTIVE_DEVELOPER: {
tooltip: "Active Developer",
icon: "/public/badges/discord/ACTIVE_DEVELOPER.svg",
},
PREMIUM_EARLY_SUPPORTER: {
tooltip: "Premium Early Supporter",
icon: "/public/badges/discord/PREMIUM_EARLY_SUPPORTER.svg",
},
BUG_HUNTER_LEVEL_1: {
tooltip: "Bug Hunter (Level 1)",
icon: "/public/badges/discord/BUG_HUNTER_LEVEL_1.svg",
},
BUG_HUNTER_LEVEL_2: {
tooltip: "Bug Hunter (Level 2)",
icon: "/public/badges/discord/BUG_HUNTER_LEVEL_2.svg",
},
SUPPORTS_COMMANDS: {
tooltip: "Supports Commands",
icon: "/public/badges/discord/SUPPORTS_COMMANDS.svg",
},
USES_AUTOMOD: {
tooltip: "Uses AutoMod",
icon: "/public/badges/discord/USES_AUTOMOD.svg",
},
};

View file

@ -9,35 +9,39 @@ 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",
url: "https://badges.vencord.dev/badges.json", url: "https://badges.vencord.dev/badges.json",
authType: "none"
}, },
{ {
service: "Equicord", // Ekwekord ! WOOP service: "Equicord", // Ekwekord ! WOOP
url: "https://raw.githubusercontent.com/Equicord/Equibored/refs/heads/main/badges.json", url: "https://raw.githubusercontent.com/Equicord/Equibored/refs/heads/main/badges.json",
authType: "none"
}, },
{ {
service: "Nekocord", service: "Nekocord",
url: "https://nekocord.dev/assets/badges.json", url: "https://nekocord.dev/assets/badges.json",
authType: "none"
}, },
{ {
service: "ReviewDb", service: "ReviewDb",
url: "https://manti.vendicated.dev/api/reviewdb/badges", url: "https://manti.vendicated.dev/api/reviewdb/badges",
authType: "none"
}, },
{ // {
service: "Enmity", // service: "ClientMods",
url: (userId: string) => ({ // url: getClientModBadgesUrl,
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`,
}),
},
{ {
service: "Discord", service: "Discord",
url: (userId: string) => `https://discord.com/api/v10/users/${userId}`, url: (id) => `https://discord.com/api/v10/users${id}`,
}, authType: "DISCORD"
}
]; ];
export const botToken: string | undefined = process.env.DISCORD_TOKEN;

View file

@ -1,12 +1,98 @@
import { discordBadgeDetails, discordBadges } from "@config/discordBadges"; import { badgeServices, redisTtl } from "@config/environment";
import { badgeServices, botToken, redisTtl } from "@config/environment";
import { fetch, redis } from "bun"; import { fetch, redis } from "bun";
const DISCORD_BADGES = {
// User badges
HYPESQUAD: 2 << 2,
HYPESQUAD_ONLINE_HOUSE_1: 2 << 6,
HYPESQUAD_ONLINE_HOUSE_2: 2 << 7,
HYPESQUAD_ONLINE_HOUSE_3: 2 << 8,
STAFF: 2 << 0,
PARTNER: 2 << 1,
CERTIFIED_MODERATOR: 2 << 18,
VERIFIED_DEVELOPER: 2 << 17,
ACTIVE_DEVELOPER: 2 << 22,
PREMIUM_EARLY_SUPPORTER: 2 << 9,
BUG_HUNTER_LEVEL_1: 2 << 3,
BUG_HUNTER_LEVEL_2: 2 << 14,
// Bot badges
SUPPORTS_COMMANDS: 2 << 23,
USES_AUTOMOD: 2 << 24,
};
const DISCORD_BADGE_DETAILS = {
HYPESQUAD: {
tooltip: "HypeSquad Events",
icon: "/public/badges/discord/HYPESQUAD.svg",
},
HYPESQUAD_ONLINE_HOUSE_1: {
tooltip: "HypeSquad Bravery",
icon: "/public/badges/discord/HYPESQUAD_ONLINE_HOUSE_1.svg",
},
HYPESQUAD_ONLINE_HOUSE_2: {
tooltip: "HypeSquad Brilliance",
icon: "/public/badges/discord/HYPESQUAD_ONLINE_HOUSE_2.svg",
},
HYPESQUAD_ONLINE_HOUSE_3: {
tooltip: "HypeSquad Balance",
icon: "/public/badges/discord/HYPESQUAD_ONLINE_HOUSE_3.svg",
},
STAFF: {
tooltip: "Discord Staff",
icon: "/public/badges/discord/STAFF.svg",
},
PARTNER: {
tooltip: "Discord Partner",
icon: "/public/badges/discord/PARTNER.svg",
},
CERTIFIED_MODERATOR: {
tooltip: "Certified Moderator",
icon: "/public/badges/discord/CERTIFIED_MODERATOR.svg",
},
VERIFIED_DEVELOPER: {
tooltip: "Verified Bot Developer",
icon: "/public/badges/discord/VERIFIED_DEVELOPER.svg",
},
ACTIVE_DEVELOPER: {
tooltip: "Active Developer",
icon: "/public/badges/discord/ACTIVE_DEVELOPER.svg",
},
PREMIUM_EARLY_SUPPORTER: {
tooltip: "Premium Early Supporter",
icon: "/public/badges/discord/PREMIUM_EARLY_SUPPORTER.svg",
},
BUG_HUNTER_LEVEL_1: {
tooltip: "Bug Hunter (Level 1)",
icon: "/public/badges/discord/BUG_HUNTER_LEVEL_1.svg",
},
BUG_HUNTER_LEVEL_2: {
tooltip: "Bug Hunter (Level 2)",
icon: "/public/badges/discord/BUG_HUNTER_LEVEL_2.svg",
},
SUPPORTS_COMMANDS: {
tooltip: "Supports Commands",
icon: "/public/badges/discord/SUPPORTS_COMMANDS.svg",
},
USES_AUTOMOD: {
tooltip: "Uses AutoMod",
icon: "/public/badges/discord/USES_AUTOMOD.svg",
},
}
export async function fetchBadges( export async function fetchBadges(
userId: string, userId: string,
services: string[], services: string[],
options?: FetchBadgesOptions, options?: FetchBadgesOptions,
request?: Request,
): Promise<BadgeResult> { ): Promise<BadgeResult> {
const { nocache = false, separated = false } = options ?? {}; const { nocache = false, separated = false } = options ?? {};
const results: Record<string, Badge[]> = {}; const results: Record<string, Badge[]> = {};
@ -34,23 +120,50 @@ export async function fetchBadges(
} }
} }
const result: Badge[] = []; let url: string;
let headers: Record<string, string> = {};
if (typeof entry.url === "function") {
url = entry.url(userId);
} else {
url = entry.url;
}
if (entry.authType === "DISCORD") {
headers = {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`
};
}
try { try {
let url: string | { user: string; badge: (id: string) => string }; const res = await fetch(url, { headers });
if (typeof entry.url === "function") { if (!res.ok) return;
url = entry.url(userId); const data = await res.json();
} else {
url = entry.url; const result: Badge[] = [];
}
switch (serviceKey) { switch (serviceKey) {
case "discord": {
if (data.avatar.startsWith("a_")) {
result.push({
tooltip: "Discord Nitro",
badge: "/public/badges/discord/NITRO.svg",
});
}
for (const [flag, bitwise] of Object.entries(DISCORD_BADGES)) {
if (data.flags & bitwise) {
const badge = DISCORD_BADGE_DETAILS[flag as keyof typeof DISCORD_BADGE_DETAILS];
result.push({
tooltip: badge.tooltip,
badge: badge.icon,
});
}
}
break;
}
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) {
@ -64,10 +177,6 @@ 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) {
@ -84,10 +193,6 @@ 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({
@ -98,69 +203,6 @@ 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;
}
case "discord": {
if (!botToken) break;
const res = await fetch(url as string, {
headers: {
Authorization: `Bot ${botToken}`,
},
});
if (!res.ok) break;
const data = await res.json();
if (data.avatar.startsWith("a_")) {
result.push({
tooltip: "Discord Nitro",
badge: `${request ? new URL(request.url).origin : ""}/public/badges/discord/NITRO.svg`,
});
}
for (const [flag, bitwise] of Object.entries(discordBadges)) {
if (data.flags & bitwise) {
const badge =
discordBadgeDetails[flag as keyof typeof discordBadgeDetails];
result.push({
tooltip: badge.tooltip,
badge: `${request ? new URL(request.url).origin : ""}${badge.icon}`,
});
}
}
break;
}
} }
if (result.length > 0) { if (result.length > 0) {
@ -170,7 +212,7 @@ export async function fetchBadges(
await redis.expire(cacheKey, redisTtl); await redis.expire(cacheKey, redisTtl);
} }
} }
} catch (_) {} } catch (_) { }
}), }),
); );

View file

@ -58,15 +58,10 @@ async function handler(request: ExtendedRequest): Promise<Response> {
validServices = badgeServices.map((b) => b.service); validServices = badgeServices.map((b) => b.service);
} }
const badges: BadgeResult = await fetchBadges( const badges: BadgeResult = await fetchBadges(userId, validServices, {
userId, nocache: cache !== "true",
validServices, separated: seperated === "true",
{ });
nocache: cache !== "true",
separated: seperated === "true",
},
request,
);
if (badges instanceof Error) { if (badges instanceof Error) {
return Response.json( return Response.json(

11
types/badge.d.ts vendored
View file

@ -9,14 +9,3 @@ 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;
});
};

6
types/config.d.ts vendored
View file

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