【发布时间】:2021-07-01 09:30:33
【问题描述】:
我目前正在为我的不和谐机器人开发计算器功能。我做了一个获取steam市场物品价格的命令,然后根据公式计算:([price] - [price * 0.15]) * amount of cases,其中0.15是市场费用。这就是问题出现的地方。
程序将json.lowest_price 视为一个词,而不是一个数字(我认为)。结果,bot 发送带有NaN 的消息。我不知道如何让我的代码正确地将 JSON 视为一个数字。
这是我的代码:
const Discord = require('discord.js');
const fetch = require('node-fetch');
const client = new Discord.Client();
client.login('[TOKEN]');
client.on('ready', () => {
console.log(`Logged in as TEST`);
});
//////////////////////////////////
const prefix = "!";
client.on('message', message =>{
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'calculate') {
if (!args.length){
return message.channel.send(`Invalid argument`);
} else if (args[0] === 'breakout'){
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(
`From ${args[1]] breakout cases you will get ${((json.lowest_price)-((json.lowest_price)*0.15))*(args[1])}`,
),
)
.catch((error) => {
console.log(error);
message.channel.send('tracking breakout price failed');
});
}
}
});
【问题讨论】:
标签: javascript node.js discord.js node-fetch