perf : not ask me i copy from another projeft
This commit is contained in:
+52
-21
@@ -10,11 +10,51 @@ export interface DiscordRole {
|
||||
position: number;
|
||||
}
|
||||
|
||||
export async function getGuildRoles(): Promise<DiscordRole[]> {
|
||||
// --- In-memory caches with TTL ---
|
||||
interface CacheEntry<T> { data: T; expiresAt: number }
|
||||
const rolesCache: { entry: CacheEntry<DiscordRole[]> | null } = { entry: null };
|
||||
const profileCache = new Map<string, CacheEntry<any>>();
|
||||
const CACHE_TTL = 60_000; // 60 seconds
|
||||
|
||||
function getCached<T>(entry: CacheEntry<T> | null | undefined): T | null {
|
||||
if (entry && Date.now() < entry.expiresAt) return entry.data;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Internal: fetch guild roles without auth check (for use by other server functions). */
|
||||
async function getGuildRolesInternal(): Promise<DiscordRole[]> {
|
||||
const cached = getCached(rolesCache.entry);
|
||||
if (cached) return cached;
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = process.env.DISCORD_GUILD_ID;
|
||||
if (!token || !guildId) return [];
|
||||
|
||||
try {
|
||||
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/roles`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
|
||||
if (!res.ok) return [];
|
||||
|
||||
const roles: any[] = await res.json();
|
||||
const result = roles.map(r => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
color: r.color,
|
||||
position: r.position,
|
||||
})).sort((a, b) => b.position - a.position);
|
||||
|
||||
rolesCache.entry = { data: result, expiresAt: Date.now() + CACHE_TTL };
|
||||
return result;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** Public: fetch guild roles with admin auth check. */
|
||||
export async function getGuildRoles(): Promise<DiscordRole[]> {
|
||||
const session = await getServerSession(authOptions);
|
||||
const discordId = (session?.user as any)?.discordId;
|
||||
const admins = (process.env.ADMIN_DISCORD_IDS || "").split(",");
|
||||
@@ -22,24 +62,7 @@ export async function getGuildRoles(): Promise<DiscordRole[]> {
|
||||
throw new Error("Unauthorized");
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/roles`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
next: { revalidate: 60 }, // Cache for 60s
|
||||
});
|
||||
|
||||
if (!res.ok) return [];
|
||||
|
||||
const roles: any[] = await res.json();
|
||||
return roles.map(r => ({
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
color: r.color,
|
||||
position: r.position,
|
||||
})).sort((a, b) => b.position - a.position);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
return getGuildRolesInternal();
|
||||
}
|
||||
|
||||
export async function getGuildMemberRoles(discordId: string): Promise<string[]> {
|
||||
@@ -51,6 +74,7 @@ export async function getGuildMemberRoles(discordId: string): Promise<string[]>
|
||||
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordId}`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
cache: "no-store", // Always fresh for access control
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
|
||||
if (!res.ok) return [];
|
||||
@@ -63,6 +87,9 @@ export async function getGuildMemberRoles(discordId: string): Promise<string[]>
|
||||
}
|
||||
|
||||
export async function getDiscordMemberProfile(discordId: string) {
|
||||
const cached = getCached(profileCache.get(discordId));
|
||||
if (cached) return cached;
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = process.env.DISCORD_GUILD_ID;
|
||||
if (!token || !guildId || !discordId) return null;
|
||||
@@ -72,8 +99,9 @@ export async function getDiscordMemberProfile(discordId: string) {
|
||||
fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordId}`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
cache: "no-store",
|
||||
signal: AbortSignal.timeout(5000),
|
||||
}),
|
||||
getGuildRoles().catch(() => []) // Fallback to empty array if unauthorized
|
||||
getGuildRolesInternal(),
|
||||
]);
|
||||
|
||||
if (!memberRes.ok) return null;
|
||||
@@ -102,7 +130,7 @@ export async function getDiscordMemberProfile(discordId: string) {
|
||||
bannerUrl = `https://cdn.discordapp.com/banners/${user.id}/${user.banner}.${ext}?size=512`;
|
||||
}
|
||||
|
||||
return {
|
||||
const result = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
globalName: user.global_name || null,
|
||||
@@ -111,6 +139,9 @@ export async function getDiscordMemberProfile(discordId: string) {
|
||||
accentColor: user.accent_color || null,
|
||||
roles,
|
||||
};
|
||||
|
||||
profileCache.set(discordId, { data: result, expiresAt: Date.now() + CACHE_TTL });
|
||||
return result;
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch member profile", e);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user