aboutsummaryrefslogtreecommitdiff
path: root/src/mojang.ts
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-04-13 16:30:27 -0500
committerGitHub <noreply@github.com>2021-04-13 16:30:27 -0500
commitac79dfbc6ad6eb56bc814471d49b95f014d3eb5f (patch)
tree600846dd4804c487d16d33dd99ad4073d815e8f3 /src/mojang.ts
parentc0d03a8d0c1e75967f48cb27d9c55bc4707af22c (diff)
downloadskyblock-api-ac79dfbc6ad6eb56bc814471d49b95f014d3eb5f.tar.gz
skyblock-api-ac79dfbc6ad6eb56bc814471d49b95f014d3eb5f.tar.bz2
skyblock-api-ac79dfbc6ad6eb56bc814471d49b95f014d3eb5f.zip
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
Diffstat (limited to 'src/mojang.ts')
-rw-r--r--src/mojang.ts38
1 files changed, 29 insertions, 9 deletions
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<MojangApiResponse> {
- 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<MojangApiResponse>
export async function profileFromUsername(username: string): Promise<MojangApiResponse> {
// 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()