!song command json

I’m trying to make a !song command with https://nowplaying.tinyrobot.co/
the API url returns something like this:

{
    "isPlaying": true,
    "artists": [
        {
            "name": "Ali Gatie",
            "uri": "spotify:artist:4rTv3Ejc7hKMtmoBOK1B4T"
        },
        {
            "name": "Tate McRae",
            "uri": "spotify:artist:45dkTj5sMRSjrmBSBeiHym"
        }
    ],
    "albumName": "The Idea Of Her",
    "albumUri": "spotify:album:5SBdHir3OHo2WZN0F3Pnbr",
    "songName": "lie to me",
    "songUri": "spotify:track:4NUgJ9gYHuAhaT3bj9ONh3"
}

I wrote this:

$(eval const api = $(urlfetch json https://nowplaying-api.tinyrobot.co/playbackstate?key=REDACTED); `${api.artists[0].name} - ${api.songName} → ${api.songUri.replace('spotify:track:','open.spotify.com/track/')}`)

But there’s somethings I’m trying to add but don’t know how, I want NightBot to return Not currently playing song when ${api.isPlaying} is set to false, and also if I added ${api.artists[1].name} it will only work with songs that has 2 or more artists, otherwise if the song has only one artist it will return Cannot read properties of undefined (reading 'name')

Hey @karimawi!

I advise you learn how to manipulate Objects and Arrays in JavaScript, I also used a ternary operator.
I appreciate you added a sample of what the API returns, it made my life easier, so thanks for that!

$(eval const api = $(urlfetch json https://nowplaying-api.tinyrobot.co/playbackstate?key=REDACTED); let artistNames = []; api.artists.forEach(artist => artistNames.push(artist.name)); api.isPlaying ? `${artistNames.join(', ')} - ${api.songName} → ${api.songUri.replace('spotify:track:','open.spotify.com/track/')}` : `Not currently playing a song.`)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.