fix starcount

This commit is contained in:
not-nullptr 2024-03-14 15:58:52 +00:00
parent 0d596bbedc
commit 3a364092eb
2 changed files with 40 additions and 40 deletions

View file

@ -4,51 +4,51 @@ import { DISCORD_USER_TOKEN, GITLAB_API_TOKEN } from "$env/static/private";
let memberCount = 0; let memberCount = 0;
let starCount = 0; let starCount = 0;
let roleMembers = { let roleMembers = {
"1214817156420862012": 50, "1214817156420862012": 50,
}; };
async function setMemberCount() { async function fetchServerSideData() {
console.log("Fetching member count"); console.log("Fetching member count");
const promises = [
fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"),
DISCORD_USER_TOKEN
? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", {
headers: {
Authorization: DISCORD_USER_TOKEN,
},
})
: Promise.resolve({ json: () => roleMembers }),
GITLAB_API_TOKEN
? fetch("https://gitlab.com/api/v4/projects/suyu-emu%2Fsuyu/", {
headers: {
Authorization: `Bearer ${GITLAB_API_TOKEN}`,
},
})
: Promise.resolve({ json: () => ({ star_count: 0 }) }), // Default to 0 if no token is provided
];
const [res, roles, gitlabRes] = await Promise.all(promises); const promises = [
const jsonPromises = [res.json(), roles.json(), gitlabRes.json()]; fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"),
const [resJson, rolesJson, gitlabResJson] = await Promise.all(jsonPromises); DISCORD_USER_TOKEN
? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", {
headers: {
Authorization: DISCORD_USER_TOKEN,
},
})
: Promise.resolve({ json: () => roleMembers }),
GITLAB_API_TOKEN
? fetch("https://gitlab.com/api/v4/projects/suyu-emu%2Fsuyu/", {
headers: {
Authorization: `Bearer ${GITLAB_API_TOKEN}`,
},
})
: Promise.resolve({ json: () => ({ star_count: 0 }) }), // Default to 0 if no token is provided
];
memberCount = resJson.approximate_member_count; const [res, roles, gitlabRes] = await Promise.all(promises);
starCount = gitlabResJson.star_count; const jsonPromises = [res.json(), roles.json(), gitlabRes.json()];
if (DISCORD_USER_TOKEN) roleMembers = rolesJson; const [resJson, rolesJson, gitlabResJson] = await Promise.all(jsonPromises);
console.log("Member count:", memberCount); memberCount = resJson.approximate_member_count;
console.log("Stars count:", starCount); starCount = gitlabResJson.star_count;
if (DISCORD_USER_TOKEN) roleMembers = rolesJson;
console.log("Member count:", memberCount);
console.log("Stars count:", starCount);
} }
if (!building) { if (!building) {
await setMemberCount(); await fetchServerSideData();
setInterval(setMemberCount, 1000 * 60 * 10); setInterval(fetchServerSideData, 1000 * 60 * 10);
} }
export async function load(opts) { export async function load(opts) {
return { return {
memberCount, memberCount,
starCount, starCount,
roleMembers, roleMembers,
}; };
} }

View file

@ -8,7 +8,7 @@
export let data: PageData; export let data: PageData;
$: memberCount = parseFloat(data.memberCount.toPrecision(2)); $: memberCount = parseFloat(data.memberCount.toPrecision(2));
$: contributors = parseFloat(data.roleMembers["1214817156420862012"].toPrecision(2)); $: contributors = parseFloat(data.roleMembers["1214817156420862012"].toPrecision(2));
$: starCount = data.starCount; $: starCount = parseFloat(data.starCount.toPrecision(2));
let metadata = { let metadata = {
url: "https://suyu.dev", url: "https://suyu.dev",
title: "suyu - Open-source, non-profit Switch emulator", title: "suyu - Open-source, non-profit Switch emulator",
@ -162,7 +162,7 @@
<h2 class="flex items-center gap-1 text-[40px] leading-[1.1]"> <h2 class="flex items-center gap-1 text-[40px] leading-[1.1]">
{starCount}+ {starCount}+
</h2> </h2>
<div class="text-[#A6A5A7]">Gitlab stars</div> <div class="text-[#A6A5A7]">GitLab stars</div>
</div> </div>
<div class="flex flex-col gap-0"> <div class="flex flex-col gap-0">
<h2 class="flex items-center gap-1 text-[40px] leading-[1.1]"> <h2 class="flex items-center gap-1 text-[40px] leading-[1.1]">
@ -239,8 +239,8 @@
> >
<h2 class="text-[24px] leading-[1.41] md:text-[60px] md:leading-[1.1]">Discord</h2> <h2 class="text-[24px] leading-[1.41] md:text-[60px] md:leading-[1.1]">Discord</h2>
<p class="mt-2 text-lg leading-relaxed"> <p class="mt-2 text-lg leading-relaxed">
Join our Discord server to chat with {memberCount}+ suyu users and developers. Get the latest Join our Discord server to chat with {memberCount}+ suyu users and developers. Get the
updates and help with any issues you have. latest updates and help with any issues you have.
</p> </p>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"