diff --git a/apps/home/static/home/js/servers.js b/apps/home/static/home/js/servers.js index be3a65c..d6e57f4 100644 --- a/apps/home/static/home/js/servers.js +++ b/apps/home/static/home/js/servers.js @@ -58,6 +58,63 @@ function loadedChannels(serverId) { return _loadedChannels[serverId] } +const rateLimitedLoadingChannels = retryAfterSeconds => { + createModal({ + title: "Failed to Fetch Server Channels", + texts: [ + { content: "Discord is rate-limiting your request." }, + { content: `This happens when making requests too quickly. Retry after ${retryAfterSeconds} seconds to continue without issue.` } + ], + buttons: [ + { + className: "btn-warning px-4", + iconClass: "bi-arrow-return-right", + closeModal: true + } + ] + }); +} + +const notAuthorisedLoadingChannels = () => { + // Mark the sidebar item as non-operational + $(`.sidebar .sidebar-item[data-id="${serverId}"]`).addClass("is-not-operational"); + + const inviteBotToServer = () => { + window.open( + `https://discord.com/oauth2/authorize + ?client_id=${discordClientId} + &permissions=2147534848 + &scope=bot+applications.commands + &guild_id=${serverId} + &disable_guild_select=true`, + "_blank" + ); + } + + // Inform the user auth problem + createModal({ + title: "Failed to Fetch Server Channels", + texts: [ + { content: "The Discord Bot is unable to access this server's channels, certain features will not work." }, + { content: "Ensure the Bot is a member, and has the neccessary permissions to operate." } + ], + buttons: [ + { + text: "Invite the Bot", + className: "btn-primary me-3", + iconClass: "bi-envelope-plus", + closeModal: true, + onClick: inviteBotToServer + }, + { + className: "btn-secondary", + iconClass: "bi-arrow-return-right", + closeModal: true + } + ] + }); +} + $(document).on("selectedServerChange", async function() { serverId = selectedServer.id; // take note incase 'selectedServer' changes @@ -71,43 +128,20 @@ $(document).on("selectedServerChange", async function() { catch (error) { logError(error); - // Mark the sidebar item as non-operational - $(`.sidebar .sidebar-item[data-id="${serverId}"]`).addClass("is-not-operational"); + switch (error?.status) { + case 429: + rateLimitedLoadingChannels(error.responseJSON.retry_after); + break; - const inviteBotToServer = () => { - window.open( - `https://discord.com/oauth2/authorize -?client_id=${discordClientId} -&permissions=2147534848 -&scope=bot+applications.commands -&guild_id=${serverId} -&disable_guild_select=true`, - "_blank" - ); + case 403: + notAuthorisedLoadingChannels(); + break; + + default: + alert("unknown error loading channels"); + break; } - - // Inform the user of the problem - createModal({ - title: "Failed to Fetch Server Channels", - texts: [ - { content: "The Discord Bot is unable to access this server's channels, certain features will not work." }, - { content: "Ensure the Bot is a member, and has the neccessary permissions to operate." } - ], - buttons: [ - { - text: "Invite the Bot", - className: "btn-primary me-3", - iconClass: "bi-envelope-plus", - closeModal: true, - onClick: inviteBotToServer - }, - { - className: "btn-secondary", - iconClass: "bi-arrow-return-right", - closeModal: true - } - ] - }); + } finally { $(".sidebar .sidebar-item").prop("disabled", false); @@ -237,7 +271,7 @@ async function loadServers() { $(".sidebar .server-rate-limit").hide(); try { - const servers = await generateServers(); + const servers = await ajaxRequest("/generate-servers/", "GET"); servers.forEach(server => addToLoadedServers(server, false)); } catch (error) {