diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-03-24 23:26:01 -0500 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-03-24 23:26:01 -0500 |
commit | ef809b7a057ca3aca993863502ee204d44b3dfa0 (patch) | |
tree | f51812ef84e04ae4e0d2cd13b82b50b4a5a466e4 /src/mojang.ts | |
parent | 0687f585211b613dfa2e3d644968a16a28489f0e (diff) | |
download | skyblock-api-ef809b7a057ca3aca993863502ee204d44b3dfa0.tar.gz skyblock-api-ef809b7a057ca3aca993863502ee204d44b3dfa0.tar.bz2 skyblock-api-ef809b7a057ca3aca993863502ee204d44b3dfa0.zip |
completely replace ashcon with mojang
Diffstat (limited to 'src/mojang.ts')
-rw-r--r-- | src/mojang.ts | 64 |
1 files changed, 19 insertions, 45 deletions
diff --git a/src/mojang.ts b/src/mojang.ts index 61e5b55..ce5b0bb 100644 --- a/src/mojang.ts +++ b/src/mojang.ts @@ -21,7 +21,7 @@ interface MojangApiResponse { /** * Get mojang api data from the session server */ -export async function mojangDataFromUuid(uuid: string): Promise<MojangApiResponse> { +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)}`, @@ -32,10 +32,7 @@ export async function mojangDataFromUuid(uuid: string): Promise<MojangApiRespons data = await fetchResponse.json() } catch { // if it errors, just return null - return { - uuid: null, - username: null - } + return { uuid: null, username: null } } return { uuid: data.id, @@ -44,51 +41,28 @@ export async function mojangDataFromUuid(uuid: string): Promise<MojangApiRespons } -export async function uuidFromUsername(username: string): Promise<string> { +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.ashcon.app/mojang/v2/uuid/${username}`, + `https://api.mojang.com/users/profiles/minecraft/${username}`, { agent: () => httpsAgent } ) - const userUuid = await fetchResponse.text() - return userUuid.replace(/-/g, '') -} - -export async function usernameFromUuid(uuid: string): Promise<string> { - const userJson = await mojangDataFromUuid(uuid) - return userJson.username -} - - - - -/** - * Fetch the uuid from a user - * @param user A user can be either a uuid or a username - */ -export async function uuidFromUser(user: string): Promise<string> { - if (isUuid(user)) - // already a uuid, just return it undashed - return undashUuid(user) - else - return await uuidFromUsername(user) + let data + try { + data = await fetchResponse.json() + } catch { + return { uuid: null, username: null } + } + return { + uuid: data.id, + username: data.name + } } -export async function mojangDataFromUser(user: string): Promise<MojangApiResponse> { - if (!isUuid(user)) - return await mojangDataFromUuid(await uuidFromUsername(user)) - else - return await mojangDataFromUuid(user) +export async function profileFromUser(user: string): Promise<MojangApiResponse> { + if (isUuid(user)) { + return await profileFromUuid(user) + } else + return await profileFromUsername(user) } - -/** - * Fetch the username from a user - * @param user A user can be either a uuid or a username - */ -export async function usernameFromUser(user: string): Promise<string> { - // we do this to fix the capitalization - const data = await mojangDataFromUser(user) - return data.username -} - |