hi i have this api: https://api.coingecko.com/api/v3/simple/price?ids=smooth-love-potion&vs_curre… and i want nightbot to respond: "Precio SLP: current value". Right now it responds {"smooth-love-potion":{"usd":0.252183}} and i dont know how to fix it. Thanks :)
Crypto command, SLP
5 replies
@Marble Crab Hiya, use $(eval) to evaluate such JSON data.
!commands add -cd=5 !slp $(eval data=$(urlfetch json https://api.coingecko.com/api/v3/simple/price?ids=smooth-love-potion&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=false&include_last_updated_at=false); `Precio SLP: ` + data.smooth-love-potion.usd)
If didn't worked let me know
Hey @Maple Goose!
Your code doesn't work because JavaScript considers the following as an operation: smooth-love-potion.
It's one of the subtleties of the language, it tries to subtract love from smooth, and it'll then try to subtract potion to the previous operation, it doesn't consider that text to be a key of the object, to fix that we use the bracket notation.
Hey @Marble Crab!
Here's the fix:
!addcom !slp $(eval value=$(urlfetch json https://api.coingecko.com/api/v3/simple/price?ids=smooth-love-potion&vs_currencies=usd)["smooth-love-potion"].usd; `Precio SLP: ${value}`)
Btw, you can use a simpler/shorter URL for the same result, I just removed what was false as it's the default state.
Polar Cat said:
Your code doesn’t work because JavaScript considers the following as an operation:smooth-love-potion.
It’s one of the subtleties of the language, it tries to subtractlovefromsmooth, and it’ll then try to subtractpotionto the previous operation, it doesn’t consider that text to be a key of the object, to fix that we use the bracket notation.
Make sense, I will take care of it. Somewhere I was aware of it. I don't know how I ignore. Uff
Didn't find your answer? The community is on Discord.
Ask on Discord