【问题标题】:Discord Roblox botDiscord Roblox 机器人
【发布时间】:2021-04-01 22:41:23
【问题描述】:

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

API 网址: 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}`);
}

【问题讨论】:

    标签: javascript node.js discord.js roblox


    【解决方案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}`);
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 2020-07-03
      • 2021-02-02
      • 2020-07-04
      • 2021-07-01
      • 2021-01-01
      • 2021-03-12
      • 2021-05-14
      • 2021-01-10
      相关资源
      最近更新 更多