I know there are lots of other posts asking this same questions, but none of the others had the solution to my problem. I am using NodeJS and DiscordJS to create a discord bot, and I need to get the UUID of a Minecraft player from just their username, which will be provided as an argument in the mand.
This is the function I have created to do this, however it doesn't seem to be working.
function getId(playername) {
const { data } = fetch(`/${args[2]}`)
.then(data => data.json())
.then(({ player }) => {
return player.id;
});
}
args[2]
is the third argument of the mand, which is formatted like this: <prefix> id <playername>
. fetch
is part of the 'node-fetch' npm module, which I have installed. I call the function when the mand is sent, and it fetches the data from Mojang's API, but it can't get the UUID. This is the error:
(node:39416) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
at C:\Users\archi\OneDrive\Documents\Programming\Discord Bots\Hypixel Discord Bot\index.js:161:21
at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:39416) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:39416) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
It is saying it cannot read the property of 'id', which if you use the Mojang API, is the key for the UUID of a player. Any ideas?
I know there are lots of other posts asking this same questions, but none of the others had the solution to my problem. I am using NodeJS and DiscordJS to create a discord bot, and I need to get the UUID of a Minecraft player from just their username, which will be provided as an argument in the mand.
This is the function I have created to do this, however it doesn't seem to be working.
function getId(playername) {
const { data } = fetch(`https://api.mojang./users/profiles/minecraft/${args[2]}`)
.then(data => data.json())
.then(({ player }) => {
return player.id;
});
}
args[2]
is the third argument of the mand, which is formatted like this: <prefix> id <playername>
. fetch
is part of the 'node-fetch' npm module, which I have installed. I call the function when the mand is sent, and it fetches the data from Mojang's API, but it can't get the UUID. This is the error:
(node:39416) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
at C:\Users\archi\OneDrive\Documents\Programming\Discord Bots\Hypixel Discord Bot\index.js:161:21
at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:39416) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:39416) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
It is saying it cannot read the property of 'id', which if you use the Mojang API, is the key for the UUID of a player. Any ideas?
Share Improve this question asked May 29, 2020 at 15:10 SirArchibaldSirArchibald 5283 gold badges9 silver badges28 bronze badges 4-
the mojang api says that it returns an array with an object in it. You are assuming it returns just an object.
return player[0].id
perhaps. – James Commented May 29, 2020 at 15:18 - That page also shows how the payload should be formatted I'm not sure you've got that quite right. Check your browser dev tools network tab, locate the api call and check that the params look right. – James Commented May 29, 2020 at 15:24
- @James unfortunately that didn't work, although I see where you are ing from. On the wiki there are two different ways of getting the UUID from a name, one is POST and one is GET, is there a difference to how this would be implemented in JS? – SirArchibald Commented May 29, 2020 at 18:30
- For reference, using fetch – James Commented Jun 1, 2020 at 18:50
1 Answer
Reset to default 3Try using the playername
instead of args[2]
in the request URL, and making it return a promise. There's also no need to use { player }
, as the object the API returns doesn't have a player propriety. Just use player
as the argument for the arrow function.
function getId(playername) {
return fetch(`https://api.mojang./users/profiles/minecraft/${playername}`)
.then(data => data.json())
.then(player => player.id);
}
Then call it like so in your code:
// Using .then (anywhere)
getId(args[2]).then(id => {
console.log(`ID is ${id}`)
})
// Using await (inside of an async function)
const id = await getId(args[2])
console.log(`ID is ${id}`)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744993329a4605038.html
评论列表(0条)