From cbd92de7a57245950f1480c3a85d31c5dc7f7f10 Mon Sep 17 00:00:00 2001
From: creations <creations@creations.works>
Date: Sat, 19 Apr 2025 20:47:06 -0400
Subject: [PATCH] add Enmity shame!

---
 config/environment.ts | 17 ++++++------
 src/helpers/badges.ts | 61 +++++++++++++++++++++++++++++++++++--------
 types/badge.d.ts      | 11 ++++++++
 types/config.d.ts     |  5 ----
 4 files changed, 69 insertions(+), 25 deletions(-)

diff --git a/config/environment.ts b/config/environment.ts
index 03a3107..7591352 100644
--- a/config/environment.ts
+++ b/config/environment.ts
@@ -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`,
+		}),
+	},
 ];
diff --git a/src/helpers/badges.ts b/src/helpers/badges.ts
index 892233d..fb0208b 100644
--- a/src/helpers/badges.ts
+++ b/src/helpers/badges.ts
@@ -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) {
diff --git a/types/badge.d.ts b/types/badge.d.ts
index 04b58c9..0731370 100644
--- a/types/badge.d.ts
+++ b/types/badge.d.ts
@@ -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;
+		  });
+};
diff --git a/types/config.d.ts b/types/config.d.ts
index 2d583ee..57584ed 100644
--- a/types/config.d.ts
+++ b/types/config.d.ts
@@ -3,8 +3,3 @@ type Environment = {
 	host: string;
 	development: boolean;
 };
-
-type badgeURLMap = {
-	service: string;
-	url: string | ((userId: string) => string);
-};