【发布时间】:2020-10-17 13:10:03
【问题描述】:
我正在尝试将backpack.tf API 用于我正在开发的 Discord Bot。我目前正在尝试其中的UserInfo 部分。在 API URL 中使用我自己的 Steam ID 会导致此输出
基本上,有users,然后是ID,最后是我尝试使用的实际信息。我的代码如下:
const Discord = require("discord.js");
const superagent = require("superagent");
const { apiKey } = require("../db/config.json");
module.exports = {
run: async(client, message, args) => {
if (!args[0]) return message.channel.send("Enter in a steam ID!")
let id = args[0]
console.log(id)
let { body } = await superagent
.get(`https://backpack.tf/api/users/info/v1?steamids=${id}&key=${apiKey}`);
message.channel.send(
new Discord.MessageEmbed()
.setImage(body.users.id.avatar)
);
if (!{ body }) return message.channel.send("Check that ID again!");
},
aliases: ["bp"]
}
其中大部分只是框架,但它基本上是通过 superagent 调用 API。当它返回时,我尝试使用body.users.id.avatar 来获取用户的头像。它没有这样做,而是输出UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'avatar' of undefined 错误。当它只是body.users.id 时,API 似乎没有给出任何错误,但是向它添加另一个层会提示它中断。
我希望它简单地给我我要求的结果。谢谢。
【问题讨论】:
标签: javascript node.js json discord discord.js