如何从 NodeJS 中的用户名获取 Minecraft 玩家的 UUID?
How can I get a Minecraft player's UUID from their username in NodeJS?
我知道还有很多其他帖子在问同样的问题,但是 none 其他帖子已经解决了我的问题。我正在使用 NodeJS 和 DiscordJS 创建一个 discord 机器人,我需要从他们的用户名中获取 Minecraft 玩家的 UUID,这将作为命令中的参数提供。
这是我为此创建的函数,但它似乎不起作用。
function getId(playername) {
const { data } = fetch(`https://api.mojang.com/users/profiles/minecraft/${args[2]}`)
.then(data => data.json())
.then(({ player }) => {
return player.id;
});
}
args[2]
是命令的第三个参数,格式如下:<prefix> id <playername>
。 fetch
是我安装的 'node-fetch' npm 模块的一部分。我在发送命令时调用该函数,它从 Mojang 的 API 中获取数据,但它无法获取 UUID。这是错误:
(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.
据说无法读取'id'的属性,如果你使用Mojang API,这是播放器UUID的关键。有什么想法吗?
尝试在请求 URL 中使用 playername
而不是 args[2]
,并使其成为 return 承诺。也没有必要使用 { player }
,因为 API return 对象没有播放器权限。只需使用 player
作为箭头函数的参数。
function getId(playername) {
return fetch(`https://api.mojang.com/users/profiles/minecraft/${playername}`)
.then(data => data.json())
.then(player => player.id);
}
然后在您的代码中这样调用它:
// 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}`)
我知道还有很多其他帖子在问同样的问题,但是 none 其他帖子已经解决了我的问题。我正在使用 NodeJS 和 DiscordJS 创建一个 discord 机器人,我需要从他们的用户名中获取 Minecraft 玩家的 UUID,这将作为命令中的参数提供。
这是我为此创建的函数,但它似乎不起作用。
function getId(playername) {
const { data } = fetch(`https://api.mojang.com/users/profiles/minecraft/${args[2]}`)
.then(data => data.json())
.then(({ player }) => {
return player.id;
});
}
args[2]
是命令的第三个参数,格式如下:<prefix> id <playername>
。 fetch
是我安装的 'node-fetch' npm 模块的一部分。我在发送命令时调用该函数,它从 Mojang 的 API 中获取数据,但它无法获取 UUID。这是错误:
(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.
据说无法读取'id'的属性,如果你使用Mojang API,这是播放器UUID的关键。有什么想法吗?
尝试在请求 URL 中使用 playername
而不是 args[2]
,并使其成为 return 承诺。也没有必要使用 { player }
,因为 API return 对象没有播放器权限。只需使用 player
作为箭头函数的参数。
function getId(playername) {
return fetch(`https://api.mojang.com/users/profiles/minecraft/${playername}`)
.then(data => data.json())
.then(player => player.id);
}
然后在您的代码中这样调用它:
// 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}`)