我目前工作在一个不和谐的机器人,跟踪蒸汽价格,并让他们聊天。我为它做了这个代码:
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¤cy=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ł"}
发布于 2021-02-10 13:15:37
这是因为您发送了fetch返回的整个对象。您只需要发送该对象的属性(如json.lowest_price)。您还需要确保您的将正文文本解析为JSON。您需要使用res.json()而不是res.text()。
if (message.content === 'lowest_price') {
fetch(
'https://steamcommunity.com/market/priceoverview/?appid=730&market_hash_name=Operation%20Breakout%20Weapon%20Case¤cy=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上的对象基础。

https://stackoverflow.com/questions/66137508
复制相似问题