From ac79dfbc6ad6eb56bc814471d49b95f014d3eb5f Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Tue, 13 Apr 2021 16:30:27 -0500 Subject: Slayer leaderboard (#4) * add slayers to constants * Compiled TS into JS * change units to also show up when only changing one constant item * Compiled TS into JS * add slayer leaderboards * remove debugging stuff --- src/mojang.ts | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'src/mojang.ts') diff --git a/src/mojang.ts b/src/mojang.ts index ce5b0bb..829de7f 100644 --- a/src/mojang.ts +++ b/src/mojang.ts @@ -3,6 +3,7 @@ */ import fetch from 'node-fetch' +import * as nodeFetch from 'node-fetch' import { Agent } from 'https' import { isUuid, undashUuid } from './util' @@ -22,11 +23,20 @@ interface MojangApiResponse { * Get mojang api data from the session server */ export async function profileFromUuid(uuid: string): Promise { - const fetchResponse = await fetch( - // using mojang directly is faster than ashcon lol, also mojang removed the ratelimits from here - `https://sessionserver.mojang.com/session/minecraft/profile/${undashUuid(uuid)}`, - { agent: () => httpsAgent } - ) + let fetchResponse: nodeFetch.Response + + try { + fetchResponse = await fetch( + // using mojang directly is faster than ashcon lol, also mojang removed the ratelimits from here + `https://sessionserver.mojang.com/session/minecraft/profile/${undashUuid(uuid)}`, + { agent: () => httpsAgent } + ) + } catch { + // if there's an error, wait a second and try again + await new Promise((resolve) => setTimeout(resolve, 1000)) + return await profileFromUuid(uuid) + } + let data try { data = await fetchResponse.json() @@ -43,10 +53,20 @@ export async function profileFromUuid(uuid: string): Promise export async function profileFromUsername(username: string): Promise { // since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/ - const fetchResponse = await fetch( - `https://api.mojang.com/users/profiles/minecraft/${username}`, - { agent: () => httpsAgent } - ) + + let fetchResponse: nodeFetch.Response + + try { + fetchResponse = await fetch( + `https://api.mojang.com/users/profiles/minecraft/${username}`, + { agent: () => httpsAgent } + ) + } catch { + // if there's an error, wait a second and try again + await new Promise((resolve) => setTimeout(resolve, 1000)) + return await profileFromUsername(username) + } + let data try { data = await fetchResponse.json() -- cgit