From fdee813a0b19971588d4978ddc80cc7ea6208830 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 19 Feb 2024 14:52:59 +0000 Subject: [PATCH] Discord stats --- src/lib/discord.ts | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/lib/discord.ts b/src/lib/discord.ts index 314dc2135..014aa06ed 100644 --- a/src/lib/discord.ts +++ b/src/lib/discord.ts @@ -2,6 +2,7 @@ const formatter = Intl.NumberFormat('en-US', { notation: 'compact', }); +let discordStats: any = null; export async function getDiscordInfo(): Promise<{ url: string; total: number; @@ -9,15 +10,32 @@ export async function getDiscordInfo(): Promise<{ online: number; onlineFormatted: string; }> { + if (discordStats) { + return discordStats; + } + const response = await fetch( - 'https://discord.com/api/v9/invites/cJpEt5Qbwa?with_counts=true' + 'https://discord.com/api/v9/invites/cJpEt5Qbwa?with_counts=true', ); - const json = await response.json(); - return { - url: `https://discord.gg/${json.code}`, - total: json.approximate_member_count, - totalFormatted: formatter.format(json.approximate_member_count), - online: json.approximate_presence_count, - onlineFormatted: formatter.format(json.approximate_presence_count), - }; + try { + const json: any = await response.json(); + + discordStats = { + url: `https://discord.gg/${json.code}`, + total: json.approximate_member_count, + totalFormatted: formatter.format(json.approximate_member_count), + online: json.approximate_presence_count, + onlineFormatted: formatter.format(json.approximate_presence_count), + }; + } catch (e) { + discordStats = { + url: `https://discord.gg/cJpEt5Qbwa`, + total: 17000, + totalFormatted: '17k', + online: 0, + onlineFormatted: formatter.format(0), + }; + } + + return discordStats; }