External JS Trouble

Asked Sep 18, 2020·2 replies·Last activity 5 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

Heya,

I couldn't type the message, that is more than 200 words, so I decided to code on Pastebin, then evaluate from that.

Pastebin:

X="$(urlfetch json https://www.googleapis.com/youtube/v3/channels?part=statistics&id=*CHANNEL_ID*&key=*API_KEY*)"; "Subscriber: " + X.items[0].statistics.subscriberCount + " | Views: " + X.items[0].statistics.viewCount + " | Total Video: " + X.items[0].statistics.videoCount

Nightbot command: !subs

Nightbot message:

$(eval $(urlfetch json https://pastebin.com/raw/XXXXXXXX))

that API link display: (in browser)

{
"kind": "youtube#channelListResponse",
"etag": "zJ408xk_bLGOHLlwCG8x4w6GWKI",
"pageInfo": {
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "PWKvUb45dNnpoUrK6mW93W_hj34",
"id": "UCzkVF-CXaXTiLQXQbLSAeDQ",
"statistics": {
"viewCount": "1687231",
"commentCount": "0",
"subscriberCount": "11700",
"hiddenSubscriberCount": false,
"videoCount": "11"
}
}
]
}

this is actually my friend's yt channel's stats

BUT WHEN I CALL THE COMMAND, IT DISPLAY "Cannot read property '0' of undefined"
can you tell me why is it so??
Thanks

2 replies

It’s because x is defined as a string in your command get rid of the quotation marks surrounding the urlfetch. That should fix the problem. Oh and also you can’t have a urlfetch inside a pastebin you’ll have to define X in the eval and then link the pastebin after.

Also if length is an issue I would suggest adding commands through the nightbot dashboard.
And for future reference I wouldn’t recommend putting an api key in a pastebin even if it’s unlisted.

ok thanks for reply,
I will try..
thank you so much ;)