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:
@@ -51,46 +51,23 @@ export default async function GuildsPage() {
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
let botGuilds: GuildSummary[] = [];
|
||||
let userGuilds: any[] = [];
|
||||
let userDiscordError: string | null = null;
|
||||
// Bot API already filters to guilds the user can manage — no second Discord @me/guilds call
|
||||
let guilds: GuildSummary[] = [];
|
||||
let botError: string | null = null;
|
||||
|
||||
try {
|
||||
botGuilds = await api.listGuilds();
|
||||
guilds = await api.listGuilds();
|
||||
} catch (err: any) {
|
||||
console.error("Failed to fetch bot guilds:", err);
|
||||
botError = err.message || "Failed to load bot servers.";
|
||||
}
|
||||
|
||||
try {
|
||||
const { fetchUserGuilds } = await import("@/lib/guild-auth");
|
||||
userGuilds = await fetchUserGuilds(session.accessToken);
|
||||
} catch (err: any) {
|
||||
console.error("Discord API Error:", err);
|
||||
userDiscordError = err?.message || "Error connecting to Discord.";
|
||||
}
|
||||
|
||||
const MANAGE_GUILD = BigInt(0x20);
|
||||
const ADMINISTRATOR = BigInt(0x8);
|
||||
const adminUserGuilds = userGuilds.filter((g) => {
|
||||
try {
|
||||
const perms = BigInt(g.permissions);
|
||||
return (
|
||||
(perms & ADMINISTRATOR) === ADMINISTRATOR ||
|
||||
(perms & MANAGE_GUILD) === MANAGE_GUILD ||
|
||||
g.owner === true
|
||||
);
|
||||
} catch {
|
||||
return g.owner === true;
|
||||
const msg = typeof err?.message === "string" ? err.message : "Failed to load bot servers.";
|
||||
botError = msg;
|
||||
// Expired OAuth — force re-login
|
||||
if (err?.status === 401 || /expired|sign in again/i.test(msg)) {
|
||||
redirect("/");
|
||||
}
|
||||
});
|
||||
|
||||
const adminGuildIds = new Set(adminUserGuilds.map((g) => String(g.id)));
|
||||
const guilds = botGuilds.filter((g) => adminGuildIds.has(String(g.id)));
|
||||
}
|
||||
|
||||
const inviteConfigured = Boolean(getBotInviteUrl());
|
||||
// Hard failure only when bot API is down — Discord sync issues still show invite CTA
|
||||
const hardError = botError;
|
||||
|
||||
return (
|
||||
@@ -132,11 +109,6 @@ export default async function GuildsPage() {
|
||||
Invite Axiom to a Discord server where you have <span className="text-slate-200">Manage Server</span>{" "}
|
||||
permissions, then come back here to configure it.
|
||||
</p>
|
||||
{userDiscordError && (
|
||||
<p className="text-amber-400/90 text-sm mt-4 max-w-md mx-auto">
|
||||
Note: {userDiscordError}
|
||||
</p>
|
||||
)}
|
||||
<div className="mt-8 flex flex-col sm:flex-row gap-3 justify-center items-center">
|
||||
{inviteConfigured ? (
|
||||
<AddBotButton className="px-8 h-12 text-base shadow-lg shadow-primary/25" />
|
||||
|
||||
Reference in New Issue
Block a user