Help with javascript command

I’ve been scratching my head on this one for a while. JS isn’t my first language (heh) and I don’t know how to make it work. I feel like it can work… just not if I do it alone.

The goal is to make a command that dynamically works based on input, unless one user uses it. That user should be met with a different response.

Command would be !command 11 (where 11 can be any number, but if no number is provided, it will default to 5)

What I have now:

$(eval `$(user)`.toLowerCase()==`baduser`.toLowerCase()?`Hey, $(user) --> you can't do that.`:` Here is your guess worth $(eval if("$(1)">1){"$(1)"}else{"5"}) points --> `$(urlfetch https://api.url?points=$(eval if("$(1)">1){"$(1)"}else{"5"})))

The first two parts work as expected (selective user and ‘guess worth X points’ but then in the URLfetch the expected number is not being included. My assumption is that I shouldn’t have 3 separate ‘eval’ calls here, and instead there’s probably a more syntactically correct way to do it, I just don’t know.

Any help is greatly appreciated.

The main issue is that big bot can only nest viable up to twice like:

$(eval $(urlfetch $(touser)))

Any more that that the variable will just be treated as the text within from my testing awhile ago.

So what you have is:

$(eval $(urlfetch $(eval $(1))))

Which is more than nightbot recognizes

What nightbot reads it as:

$(eval $(urlfetch $(eval 1)))

I originally thought it would be impossible to make this just one command but I was able to get it working in just one like this.

$(eval `$(user)`.toLowerCase()==`baduser`.toLowerCase()||isNaN(`$(1)`)?`Hey, $(user) --> you can't do that.`:` Here is your guess worth ${`$(1)`>=1?`$(1)`:`5`} points --> `)$(urlfetch $(eval `$(user)`.toLowerCase()==`baduser`.toLowerCase()||!isNaN(`$(1)`)?`https://api.url?points=${`$(1)`>=1?`$(1)`:`5`}`:`https://pastebin.com/raw/0tBLkhCp`))

Please note I assume you watch numbers greater than and equal to 1 so I changed the > to >= that can be easily fixed.
If you have any trouble implementing this don’t be afriad to ask for more help :slight_smile:

@potatoeaterlove , thank you so much for the response and the thorough explanation.
I tried the snippet provided but got an unexpected result when ran as baduser. When running the command as the user that should be told no, I get both the text response AND the result from the external API.

Response as baduser running !command 19:

"Hey, baduser --> you can't do that.https://api.url?points=19"

The expected result is

"Hey, baduser --> you can't do that."

I triple checked that I didn’t misconfigure when modifying with the actual API URL or user name. I then tried a few tweaks to the code myself to attempt to get to the expected result but wasn’t able to. Is it necessary to again eval the user name in the second bit if the goal is to never run this portion of code when baduser uses the command?

Thank you for any additional help you can provide.

Yeah that’s my mistake the second “baduser” check should be != instead of == if you don’t know what I mean I edited the code below with the fix.

$(eval `$(user)`.toLowerCase()==`baduser`.toLowerCase()||isNaN(`$(1)`)?`Hey, $(user) --> you can't do that.`:` Here is your guess worth ${`$(1)`>=1?`$(1)`:`5`} points --> `)$(urlfetch $(eval `$(user)`.toLowerCase()!=`baduser`.toLowerCase()||!isNaN(`$(1)`)?`https://api.url?points=${`$(1)`>=1?`$(1)`:`5`}`:`https://pastebin.com/raw/0tBLkhCp`))

Also I forgot to mention befor but I added a isNaN check to ensure the user inputs a number.

Thank you for the quick response. I feel like I know just enough that I should have at least picked up that simply making it a “is not” condition would have fixed it. Apparently not!

I removed the isNaN for two reasons – First: it’s fine if no number is provided, as there is a default of 5 built in. Second, in this part:

$(eval `$(user)`.toLowerCase()!=`baduser`.toLowerCase()||!isNaN(`$(1)`

I believe this means user is not baduser OR $(1) is not blank/null. This meant if the baduser ran it, the unexpected behavior happened again where the first message appeared but also sent the data to the API. I am sure the || could have been swapped out for && and it would have worked but in my use case, I am fine with their being no input by the user.

Thank you again for your help. I now have a working command! I am going to run it through the paces a bit more before fully activating. Have a great day, @potatoeaterlove .

end result is:

$(eval `$(user)`.toLowerCase()==`baduser`.toLowerCase()?`Hey, $(user) --> you can't do that.`:` Here is your guess worth ${`$(1)`>=1?`$(1)`:`5`} points --> `)$(urlfetch $(eval `$(user)`.toLowerCase()!=`baduser`.toLowerCase()?`https://api.url?points=${`$(1)`>=1?`$(1)`:`5`}`:`https://pastebin.com/raw/0tBLkhCp`))
1 Like

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