aboutsummaryrefslogtreecommitdiff
path: root/build/mojang.js
diff options
context:
space:
mode:
Diffstat (limited to 'build/mojang.js')
-rw-r--r--build/mojang.js53
1 files changed, 41 insertions, 12 deletions
diff --git a/build/mojang.js b/build/mojang.js
index b8bfa44..052f813 100644
--- a/build/mojang.js
+++ b/build/mojang.js
@@ -6,39 +6,68 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
-exports.usernameFromUser = exports.uuidFromUser = exports.mojangDataFromUser = void 0;
+exports.usernameFromUser = exports.mojangDataFromUser = exports.uuidFromUser = exports.usernameFromUuid = exports.uuidFromUsername = exports.mojangDataFromUuid = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const https_1 = require("https");
+const util_1 = require("./util");
// We need to create an agent to prevent memory leaks
const httpsAgent = new https_1.Agent({
keepAlive: true
});
/**
- * Get mojang api data from ashcon.app
+ * Get mojang api data from the session server
*/
-async function mojangDataFromUser(user) {
+async function mojangDataFromUuid(uuid) {
+ console.log('mojangDataFromUuid', uuid);
const fetchResponse = await node_fetch_1.default(
- // we use v1 rather than v2 since its more stable
- `https://api.ashcon.app/mojang/v1/user/${user}`, { agent: () => httpsAgent });
- return await fetchResponse.json();
+ // using mojang directly is faster than ashcon lol, also mojang removed the ratelimits from here
+ `https://sessionserver.mojang.com/session/minecraft/profile/${util_1.undashUuid(uuid)}`, { agent: () => httpsAgent });
+ const data = await fetchResponse.json();
+ return {
+ uuid: data.id,
+ username: data.name
+ };
}
-exports.mojangDataFromUser = mojangDataFromUser;
+exports.mojangDataFromUuid = mojangDataFromUuid;
+async function uuidFromUsername(username) {
+ console.log('uuidFromUsername', username);
+ // since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/
+ const fetchResponse = await node_fetch_1.default(`https://api.ashcon.app/mojang/v2/uuid/${username}`, { agent: () => httpsAgent });
+ const userUuid = await fetchResponse.text();
+ return userUuid.replace(/-/g, '');
+}
+exports.uuidFromUsername = uuidFromUsername;
+async function usernameFromUuid(uuid) {
+ const userJson = await mojangDataFromUuid(uuid);
+ return userJson.username;
+}
+exports.usernameFromUuid = usernameFromUuid;
/**
* Fetch the uuid from a user
* @param user A user can be either a uuid or a username
*/
async function uuidFromUser(user) {
- const fetchJSON = await mojangDataFromUser(user);
- return fetchJSON.uuid.replace(/-/g, '');
+ if (util_1.isUuid(user))
+ // already a uuid, just return it undashed
+ return util_1.undashUuid(user);
+ else
+ return await uuidFromUsername(user);
}
exports.uuidFromUser = uuidFromUser;
+async function mojangDataFromUser(user) {
+ if (!util_1.isUuid(user))
+ return await mojangDataFromUuid(await uuidFromUsername(user));
+ else
+ return await mojangDataFromUuid(user);
+}
+exports.mojangDataFromUser = mojangDataFromUser;
/**
* Fetch the username from a user
* @param user A user can be either a uuid or a username
*/
async function usernameFromUser(user) {
- // get a minecraft uuid from a username, using ashcon.app's mojang api
- const fetchJSON = await mojangDataFromUser(user);
- return fetchJSON.username;
+ // we do this to fix the capitalization
+ const data = await mojangDataFromUser(user);
+ return data.username;
}
exports.usernameFromUser = usernameFromUser;