【问题标题】:Trying to display JSON data from API as an array尝试将来自 API 的 JSON 数据显示为数组
【发布时间】:2020-07-03 14:22:06
【问题描述】:

所以我一直在尝试在 Node.js 中制作 Discord Bot,我从外部 api 获取数据并使用 BOT 来显示数据,问题是当我调用该函数时,它会一一显示一条消息中的所有内容。 我想在一条消息中显示所有内容。

const token = 'my discord token is here';
const Discord = require('discord.js');
const axios = require('axios');
const client = new Discord.Client();


client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`)
})
client.on('message', async msg => {
    if (msg.content === '!inventario') {
      let getInv =  async () => {
          let response = await axios.get('my api link where im getting the info is here')
          let inventario = response.data
          return inventario
      }
      let inventarioValue = await getInv ()
      var inv = inventarioValue.total_inventory_count
      msg.channel.send(`Total de Itens no inventário: ${inventarioValue.total_inventory_count} \nSkins:\n`);
       for (var i=0;i<inv;i++)
{

    var itens = inventarioValue.descriptions[i].market_name
    msg.channel.send(itens);
}
    }
  });
client.login(token);

我想执行这个 inventarioValue.descriptions[i].market_name,然后在执行后显示完整结果而不是一一显示。

谢谢

【问题讨论】:

    标签: arrays node.js json discord.js


    【解决方案1】:

    您可以映射对象数组元素并添加到消息中。为了获得更好的视觉反射,您可以将它们添加到嵌入中。

    const token = 'my discord token is here';
    const Discord = require('discord.js');
    const axios = require('axios');
    const client = new Discord.Client();
    client.on('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`)
    })
    client.on('message', async msg => {
        if (msg.content === '!inventario') {
            let getInv = async () => {
                let response = await axios.get('my api link where im getting the info is here')
                let inventario = response.data
                return inventario
            }
            let inventarioValue = await getInv()
            var inv = inventarioValue.total_inventory_count
            let embed = new Discord.MessageEmbed()
            embed.setDescription(`${inventarioValue.descriptions.map(val => val.market_name).join('\n')}`)
            msg.channel.send(`Total de Itens no inventário: ${inventarioValue.total_inventory_count} \nSkins:\n`, embed);
        }
    });
    client.login(token);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-23
      • 2021-08-02
      • 2021-10-17
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多