【发布时间】:2020-09-17 05:00:38
【问题描述】:
我知道还有很多其他帖子都在问同样的问题,但其他帖子都没有解决我的问题。我正在使用 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的关键。有什么想法吗?
【问题讨论】:
-
mojang api 表示它返回一个包含对象的数组。您假设它只返回一个对象。
return player[0].id或许。 -
该页面还显示了应如何格式化有效负载我不确定您是否完全正确。检查您的浏览器开发工具网络选项卡,找到 api 调用并检查参数是否正确。
-
@James 不幸的是这不起作用,尽管我知道你来自哪里。在 wiki 上,有两种从名称中获取 UUID 的不同方式,一种是 POST,一种是 GET,这在 JS 中的实现方式有区别吗?
-
供参考,using fetch
标签: javascript node.js discord.js minecraft