Discord Roblox 机器人

我正在尝试创建一个命令来输出玩家收藏品并总结recentAveragePrice列出的物品总数。问题是当我尝试输出此 API 的任何部分时,它只是输出undefined.

API URL: https://inventory.roblox.com/v1/users/1417341214/assets/collectibles ?assetType=Hat&sortOrder=Desc&limit=100

if (command === "inv"){

    let getInv = async () => {

        let response = await axios.get("https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100");

        let inv = response.data;

        return inv;

    }

    let invValue = await getInv();

    console.log(invValue);

    message.channel.send(`${invValue.data.name} \n ${invValue.data.recentAveragePrice}`);

}


阿晨1998
浏览 35回答 1
1回答

有只小跳蛙

这是因为返回的数据是一个对象数组。如果您想将它们全部作为消息发送,您可以迭代它们。如果您想将它们一一发送,可以使用以下方法:if (command === 'inv') {  const getInv = async () => {    const response = await axios.get(      'https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100',    );    return response.data;  };  const invValue = await getInv();  let total = 0;  invValue.data.forEach((item) => {    message.channel.send(`${item.name} \n ${item.recentAveragePrice}`);    total += item.recentAveragePrice;  });  message.channel.send(`Total average price: ${total}`);}结果:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript