Nightbot custom commands: execute $(urlfetch) conditionally

Currently working on a set of custom nightbot commands and I’m running into an issue where all $(urlfetch) requests are run, irrespective of the if/else logic that surrounds them. Some pseudocode to help clarify:

Call this command !logUserID

if ($(userlevel) == 'subscriber') {
'$(urlfetch$(userid))'; 'subscriber logged';
} else {
'$(urlfetch$(userid))'; 'nonsub logged';

What I find is that the logic works correctly in terms of the ‘subscriber logged’ and ‘nonsub logged’ being printed to chat by nightbot when a sub vs nonsub uses !logUserID. However, when I go look at my database, I see the following entries logged:


This indicates to me that, while the if/else logic is being executed correctly by nightbot, both of the urlfetch requests are being executed. Presumably there’s some scope issue that I’m unaware of.

At the end of the day, I really just need to be able to log things to my database conditionally based on whether someone is a sub or nonsub. Workarounds I’ve tried are the following

  1. Use the if/else logic to write the data to a variable and then use that in a single urlfetch request, like so:
if ($(userlevel) == 'subscriber') {
s = 'subscriber-$(userid)';
} else {
s = '$(userid)';

Unfortunately, I find that $(urlfetch) needs to be in ‘’ quotes to work properly and the insertion of a variable with ${s} requires `` ticks. These seem incompatible, but hopefully that’s just my lack of familiarity with javascript. This would be a totally viable solution for what I’m trying to do.

  1. I was alternatively hoping that Nightbot would allow redundant commands that execute different code based on the userlevel of the caller is. i.e. a follower calls !logUserID and that executes command A, while a subscriber calls !logUserID and it executes command B. However, Nightbot just yells at me for creating redundant command names and that’s that.

Hopefully this is clear enough to understand. I’ve spent a couple days trying to sort this out without any luck. Thanks in advance for any assistance!

Hiya, in your first piece of code you are correct, both urlfetches are executed. Urlfetches are executed before the eval code is executed, since sometimes the response from a urlfetch is used inside the eval code.

I would suggest using one urlfetch request, and check serverside if the user is the user is a follower/sub etc. You can use the Nightbot headers in the request to check the userid / userlevel of the use, info: UrlFetch - Nightbot Docs

Thanks for confirming and explaining that behavior. Exactly what I was looking for.

Also, your comment made it obvious how I’d workaround this without needing to declare a variable and insert it into my urlfetch url:


Then treat the data differently based on userlevel on the server side. Easy. Thanks again for the help.

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