Enhance Discord authentication and guild management by implementing caching for user and guild data to reduce API calls and improve performance. Update error handling for Discord API responses and refactor the guilds listing logic in the dashboard to streamline data retrieval. Adjust retry logic for rate limits and improve session error handling in the authentication flow.
Some checks failed
CI / Bot (Python) (push) Successful in 12s
CI / Dashboard (Next.js) (push) Failing after 9s

This commit is contained in:
TheOnlyMace
2026-07-21 22:33:30 +02:00
parent 60390009d1
commit f15c869993
6 changed files with 131 additions and 73 deletions

View File

@@ -14,8 +14,8 @@ export interface DiscordGuild {
const MANAGE_GUILD = BigInt(0x20);
const ADMINISTRATOR = BigInt(0x8);
const CACHE_TTL_MS = 60_000;
const MAX_RETRIES = 3;
const CACHE_TTL_MS = 90_000;
const MAX_RETRIES = 4;
type CacheEntry = { guilds: DiscordGuild[]; expiresAt: number };
@@ -70,19 +70,19 @@ async function fetchUserGuildsUncached(accessToken: string): Promise<DiscordGuil
const header = res.headers.get("retry-after");
retryAfter = header ? Number(header) : 1;
}
const waitMs = Math.min(Math.max(retryAfter * 1000, 300), 5000);
const waitMs = Math.min(Math.max(retryAfter * 1000, 500), 15_000);
console.warn(`Discord guilds rate-limited — retry in ${waitMs}ms (attempt ${attempt}/${MAX_RETRIES})`);
await new Promise((r) => setTimeout(r, waitMs));
continue;
}
if (res.status === 401 || res.status === 403) {
throw new Error("Discord session expired — please sign in again.");
}
const body = await res.text().catch(() => "");
console.error("Discord guilds fetch failed:", res.status, body);
throw new Error(
res.status === 401
? "Discord session expired — please sign in again."
: `Failed to fetch your Discord servers. (${res.status})`
);
throw new Error(`Failed to fetch your Discord servers. (${res.status})`);
}
throw new Error("Failed to fetch your Discord servers. (rate limited)");