Skip to content

Commit

Permalink
Discord stats
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranahmedse committed Feb 19, 2024
1 parent 707f009 commit fdee813
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/lib/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,40 @@ const formatter = Intl.NumberFormat('en-US', {
notation: 'compact',
});

let discordStats: any = null;
export async function getDiscordInfo(): Promise<{
url: string;
total: number;
totalFormatted: string;
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;
}

0 comments on commit fdee813

Please sign in to comment.