add discord badges from https://git.creations.works/seth pr, fixed a few things
All checks were successful
Code quality checks / biome (push) Successful in 8s
All checks were successful
Code quality checks / biome (push) Successful in 8s
This commit is contained in:
parent
cbd92de7a5
commit
c73b8725c1
21 changed files with 178 additions and 5 deletions
|
@ -1,10 +1,12 @@
|
|||
import { badgeServices, redisTtl } from "@config/environment";
|
||||
import { discordBadgeDetails, discordBadges } from "@config/discordBadges";
|
||||
import { badgeServices, botToken, redisTtl } from "@config/environment";
|
||||
import { fetch, redis } from "bun";
|
||||
|
||||
export async function fetchBadges(
|
||||
userId: string,
|
||||
services: string[],
|
||||
options?: FetchBadgesOptions,
|
||||
request?: Request,
|
||||
): Promise<BadgeResult> {
|
||||
const { nocache = false, separated = false } = options ?? {};
|
||||
const results: Record<string, Badge[]> = {};
|
||||
|
@ -127,6 +129,38 @@ export async function fetchBadges(
|
|||
);
|
||||
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) {
|
||||
|
|
|
@ -58,10 +58,15 @@ async function handler(request: ExtendedRequest): Promise<Response> {
|
|||
validServices = badgeServices.map((b) => b.service);
|
||||
}
|
||||
|
||||
const badges: BadgeResult = await fetchBadges(userId, validServices, {
|
||||
nocache: cache !== "true",
|
||||
separated: seperated === "true",
|
||||
});
|
||||
const badges: BadgeResult = await fetchBadges(
|
||||
userId,
|
||||
validServices,
|
||||
{
|
||||
nocache: cache !== "true",
|
||||
separated: seperated === "true",
|
||||
},
|
||||
request,
|
||||
);
|
||||
|
||||
if (badges instanceof Error) {
|
||||
return Response.json(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue