简体   繁体   English

我的 Discord 机器人以一种奇怪的方式发送消息

[英]My Discord bot sends messages in a weird way

I'm currently working on a Discord bot that tracks Steam prices and sends them to chat.我目前正在开发一个 Discord 机器人来跟踪 Steam 价格并将它们发送到聊天室。 I made this code for it:我为它制作了这段代码:

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);
});

I want my bot to send message "Breakout case price on [date] is [price]."我希望我的机器人发送消息“[日期] 的分拆案例价格是 [价格]。” For example "Breakout case price on 10.02.2021 is 5.94zł" , but instead it sends this:例如"Breakout case price on 10.02.2021 is 5.94zł" ,但它发送的是:

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

It's because you send the whole object returned from fetch.这是因为您发送了从 fetch 返回的整个 object。 You only need to send a property of that object (like json.lowest_price ).您只需发送该 object 的属性(如json.lowest_price )。 You'll also need to make sure that you parse the body text as JSON .您还需要确保将正文文本解析为 JSON You'll need to use res.json() instead of res.text() .您需要使用res.json()而不是res.text()

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');
    });
}

Check out the basics of objects on MDN .查看MDN 上的对象基础知识

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM