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.
This commit is contained in:
@@ -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)");
|
||||
|
||||
Reference in New Issue
Block a user