首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的不和机器人以一种奇怪的方式发送信息

我的不和机器人以一种奇怪的方式发送信息
EN

Stack Overflow用户
提问于 2021-02-10 13:07:31
回答 1查看 61关注 0票数 0

我目前工作在一个不和谐的机器人,跟踪蒸汽价格,并让他们聊天。我为它做了这个代码:

代码语言:javascript
复制
setInterval(() => {
  const currentDate = new Date();
  var yourchannel = client.channels.cache.get('[CHANNEL ID]');
  fetch('https://steamcommunity.com/market/priceoverview/?appid=730&market_hash_name=Operation%20Breakout%20Weapon%20Case&currency=6', )
    .then(res => res.text())
    .then(text => yourchannel.send(`Breakout case price on ${currentDate.toLocaleDateString(`pl-PL`)} is ${text}`))

  }, 1000 * 60 * 60 * 24);
});

我要我的机器人发送信息“突破的情况下价格的日期是价格。”例如,“10.02.2021上的突破箱价格为5.94zł",但它却发送如下:

Breakout case price on 10.02.2021 is {"success":true,"lowest_price":"5,92zł","volume":"13,807","median_price":"6,01zł"}

EN

回答 1

Stack Overflow用户

发布于 2021-02-10 13:15:37

这是因为您发送了fetch返回的整个对象。您只需要发送该对象的属性(如json.lowest_price)。您还需要确保您的将正文文本解析为JSON。您需要使用res.json()而不是res.text()

代码语言:javascript
复制
if (message.content === 'lowest_price') {
  fetch(
    'https://steamcommunity.com/market/priceoverview/?appid=730&market_hash_name=Operation%20Breakout%20Weapon%20Case&currency=6',
  )
    .then((res) => res.json())
    .then((json) =>
      message.channel.send(
        `Breakout case price on ${new Date().toLocaleDateString('pl-PL')} is ${
          json.lowest_price
        }`,
      ),
    )
    .catch((error) => {
      console.log(error);
      message.channel.send('Oops, there was an error fetching the price');
    });
}

看看MDN上的对象基础

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66137508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档