External Javascript Countdown Not Working

Asked Mar 26, 2021·3 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.

Edit: I figured it out... Javascript single line comments don't work with Nightbot and Pastebin.

I'm trying to write external Javascript to work as a countdown command to output a countdown depending on certain times of day and the week. For now I'm just trying to get a single countdown string to output in chat and then build onto the script. Yes, I'm familiar with the Nightbot $(countdown) variable, but it won't work for the purpose of my script.

The Javascript code works as intended in a web browser console, but when I try it with a Nightbot command, it returns an "undefined" error as the chat message. What am I doing wrong here?

Nightbot command:

!addcom !countdown $(eval $(urlfetch json https://pastebin.com/raw/yCnnEkww))

External Javascript:

var countDownDate = new Date('Sep 25, 2025 15:00:00').getTime();

// Get todays date and time
var now = new Date().getTime();

// Find the distance between now an the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output result
var output = hours + "h " + minutes + "m " + seconds + "s ";

output;

Output in web browser console:

Output in Twitch chat:

Screenshot_2021-03-24_210448.pngScreenshot_2021-03-24_210835.png

3 replies

Hey @Perky Frog!

Glad you figured it out. Just answering now because I find it interesting that Nightbot doesn't read single line comments properly when it's from a Pastebin, I've stored many commands in pastes and never had that issue because I didn't comment the code. Anyway, my question is, was the paste text format set up to JS or simply text? I'm wondering if it would have recognized the comments if the format matched the language. Thank you for your edit btw, this will help us assist people in the future if someone experiences the same issue some day.

@Polar Cat The syntax highlighting in the Pastebin is set to JS. Once I removed all the comments and changed the date string from double quotations to single quotations in the countDownDate variable it worked. Block commenting seems to work just fine though, FYI.

Thank you very much for your precious feedback, we really appreciate it!