0

I'm currently working on a Discord bot that tracks Steam prices and sends them to chat. 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 {"success":true,"lowest_price":"5,92zł","volume":"13,807","median_price":"6,01zł"}

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Brunon
  • 31
  • 2

1 Answers1

2

It's because you send the whole object returned from fetch. You only need to send a property of that object (like json.lowest_price). You'll also need to make sure that you parse the body text as JSON. You'll need to use res.json() instead of 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.

enter image description here

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57