I'm currently working on a calculator feature for my discord bot. I've made a command that fetches steam market item price, and then calculate it according to the formula: ([price] - [price * 0.15]) * amount of cases
, where 0.15 is the market fee. That's where the problem shows up.
Program sees json.lowest_price
as a word, not as a number (I think). As a result, bot sends a message with NaN
. I have no idea how to make my code seeing JSON thingy as a number properly.
Here's my code:
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');
});
}
}
});