NightDev Community Forums

[SOLVED] Need some help with an 8-Ball-esque command (ping the user and have a default message)

I’m trying to make a command that causes Nightbot to give a random response to any question from a big list of responses on Pastebin.

Here’s my code, which I got from this post.

!commands add !ask -cd=5 $(eval responses = $(urlfetch json https://pastebin.com/raw/WURvJ4MR).split(";"); responses[Math.floor(Math.random() * responses.length)])

This command works perfectly fine, but there are two additional things I want to do:

  1. I want Nightbot to ping the user who asked the question at the beginning of the message. (Example: “@Dimentive Without a doubt.”)
  2. I want Nightbot to say a specific message if the user does not input a variable. (So, if someone just uses !ask, without a question, I want the bot to say Please ask a question: !ask *question*

I currently have very little experience with JavaScript, so I don’t know how to do this.
Just to note: User pransfenders did pull both of these things off (except the ping was at the end of the message) in this post, but the command in that post uses a different syntax as it doesn’t invoke its responses from an external site. I’m not adept enough at JavaScript to be able to convert it to an if-else statement, and I don’t know where to put the ping part of the code (@$(user)).

Any help is greatly appreciated!

@Dimentive

To have Nightbot ping the command user, simply put @$(user) at the front of the command response.

Use a ternary operator to determine whether the user input is empty or not.

!commands add !ask -cd=5 @$(user) $(eval responses = $(urlfetch json https://pastebin.com/raw/WURvJ4MR).split(";"); decodeURIComponent("$(querystring)") ? responses[Math.floor(Math.random() * responses.length)] : "Please ask a question: !ask *question*")
1 Like

It works perfectly! Thank you so much. :blue_heart:

Heck%20yeah!

I just have one more question (well, three): If I edit the Pastebin paste, does the bot update its list of responses automatically? If so, how long does it take? Or does it re-check the list from the site each time someone uses the command?

Nightbot fetches the data from the paste every time you use the command. Edits to your paste, and therefore edits to the list of possible responses for the command, should take effect almost always immediately.

1 Like

That’s great to hear. Thank you! :+1:

How come when I try to paste !commands add !ask -cd=5 @$(user) $(eval responses = $(urlfetch json https://pastebin.com/raw/WURvJ4MR).split(";"); decodeURIComponent("$(querystring)") ? responses[Math.floor(Math.random() * responses.length)] : “Please ask a question: !ask question”) it does not fit it the message section any help?

Hiya, where are you trying to paste it? The message fields on Nightbot.tv have a limit of 500 characters, that should be plenty enough for this command.

When I click the Add Command button there’s a field called Message and it doesn’t fit.

Try adding the command in your chat instead, using the following command:
!addcom !ask -cd=5 @$(user) $(eval responses = $(urlfetch json https://pastebin.com/raw/XXXXXXXX).split(";"); decodeURIComponent("$(querystring)") ? responses[Math.floor(Math.random() * responses.length)] : "Please ask a question: !ask *question*")

That’s what I did, and it worked perfectly.

Be sure to replace the Pastebin link with your own. Create a Pastebin account so you can edit the paste. Create a paste and separate each line with a semicolon. Be sure to place a quotation mark at the beginning and end of the paste (not each line, though). Here’s an example paste to show you the correct syntax. Make sure to use a raw Pastebin link too, not a regular Pastebin link.

@MrBored

It appears you’re adding this command on YouTube. Commands on Twitch have a 500 character limit while commands on YouTube have a 200 character limit. I’ve rewritten your command with this in mind.

@$(user) $(eval a=$(urlfetch json https://pastebin.com/raw/WURvJ4MR).split(`;`);decodeURIComponent(`$(querystring)`)?a[Math.floor(Math.random()*a.length)]:`Please ask a question: !ask *question*`)

@RokettoJanpu when I add the @$(user) $(eval a=$(urlfetch json https://pastebin.com/raw/WURvJ4MR).split(;);decodeURIComponent($(querystring))?a[Math.floor(Math.random()*a.length)]:Please ask a question: !ask *question*) command and then type !8ball in my chat its says the whole command : @$(user) $(eval a=$(urlfetch json https://pastebin.com/raw/WURvJ4MR).split(;);decodeURIComponent($(querystring))?a[Math.floor(Math.random()*a.length)]:Please ask a question: !ask *question*) !

@Dimentive how do I add a command in chat, i use youtube btw.

I double checked the command I wrote. It is under 200 characters. There doesn’t seem to be any issue that would cause it to output the entire command response to chat. Try adding the command through the dashboard.

To add a command through chat on YouTube, make sure you are live with your stream set to public. Make sure you are viewing live chat so you can see Nightbot’s responses. Then type:

!addcom [command name] [command response]

@$(user) $(eval a=$(urlfetch json https://pastebin com/raw/WURvJ4MR).split(;);decodeURIComponent($(querystring))?a[Math.floor(Math.random()*a.length)]:Please ask a question: !ask *question*) is 197 letters in total so then when I type [command name] [command response] its doesnt let me since it uses up the space and the parathesis things.

The command is just under 200 characters, therefore I suggest you add the command through the dashboard.

How do i add it through dashboard sorry im new to this.

Just like in this photo you uploaded:

Yes! it works! thx alot @RokettoJanpu .

wait nvm when i just type !8ball it says “@MrBored Please ask a question: !ask question ” is there a way to change the !ask to !8ball? and then when i type !8ball (then question) its says “Response 3” any help?