I'm writing a Discord bot that sometimes requires a Minecraft account UUID to be looked up so this is the function (and MWE) I have written to do this.
'use strict';
const axios = require('axios');
const EDITION = {
JAVA: 0,
BEDROCK: 1,
};
async function getUuid(username, edition) {
try {
const result = (edition === EDITION.JAVA)
? await axios.get(`https://playerdb.co/api/player/minecraft/${username}`)
: await axios.get(`https://playerdb.co/api/player/xbox/${username}`);
const player = result.data.data.player;
if (player !== undefined) {
return (edition === EDITION.JAVA)
? player.id
: parseInt(player.id)
.toString(16)
.padStart(32, '0')
.replace(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/, '$1-$2-$3-$4-$5');
}
return null;
}
catch (error) {
console.error(`Failed to lookup UUID. Reason: ${error.message}`);
return null;
}
}
(async () => {
console.log(await getUuid('somename', EDITION.JAVA));
console.log(await getUuid('somename1234', EDITION.BEDROCK));
})();
My concern is with the way that I return a Bedrock UUID using str.replace(regexp, newSubstr)
I feel like a more efficient RegEx could be used here but I'm unsure how to write it.
Of course, if there's anything else wrong this code, I'd like to know, I'm still rather new to using Node.js.