From a43543190cccac676a0c38dd0a015bccf7b3e6c9 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 2 Jun 2021 19:26:33 -0500 Subject: add api.ashcon.app as alternative to mojang api --- build/mojang.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'build') diff --git a/build/mojang.js b/build/mojang.js index 9641422..aa60b57 100644 --- a/build/mojang.js +++ b/build/mojang.js @@ -6,7 +6,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.profileFromUser = exports.profileFromUsername = exports.profileFromUuid = void 0; +exports.profileFromUser = exports.profileFromUsernameAlternative = exports.profileFromUsername = exports.profileFromUuid = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const https_1 = require("https"); const util_1 = require("./util"); @@ -58,8 +58,10 @@ async function profileFromUsername(username) { try { data = await fetchResponse.json(); } - catch { - return { uuid: null, username: null }; + catch { } + if (!data.id) { + console.log('mojang api failed, trying ashcon as backup'); + return await profileFromUsernameAlternative(username); } return { uuid: data.id, @@ -67,6 +69,29 @@ async function profileFromUsername(username) { }; } exports.profileFromUsername = profileFromUsername; +async function profileFromUsernameAlternative(username) { + let fetchResponse; + try { + fetchResponse = await node_fetch_1.default(`https://api.ashcon.app/mojang/v2/user/${username}`, { agent: () => httpsAgent }); + } + catch { + // if there's an error, wait a second and try again + await new Promise((resolve) => setTimeout(resolve, 1000)); + return await profileFromUsernameAlternative(username); + } + let data; + try { + data = await fetchResponse.json(); + } + catch { + return { uuid: null, username: null }; + } + return { + uuid: data.uuid, + username: data.username + }; +} +exports.profileFromUsernameAlternative = profileFromUsernameAlternative; async function profileFromUser(user) { if (util_1.isUuid(user)) { return await profileFromUuid(user); -- cgit