Response Generator


I would like to create a command that upon it being called in chat it responds with a random joke from a .txt file that I created. Is that possible?


Yes this is possible, the easiest way would be using a mix of $(urlfetch) and $(eval). First you should upload the list of jokes to a site that allows you to get the text in a plaintext format, I would recommend using pastebin.

You should first create a Pastebin account if you don’t have one already, this way you will be able to edit the list of jokes in the future.

Create a new paste and separate the jokes with a comma like so:

Once you save your paste you should be at a page with a link that looks something like

Add /raw/ before the paste id so the link looks more like

Now you can create the command in your chat to display a random joke like so:

!commands add !joke -cd=5 $(eval var jokes = "$(urlfetch YOUR_LINK) ".split(","); jokes[Math.floor(Math.random() * (jokes.length - 1))];)

(Be sure to replace YOUR_LINK with your pastebin link from before, including /raw/)


Thank you so much!!!

1 Like

This is great, thank you!
I have a question to add to this:
Would it be possible to ADD to that Pastebin “database”, say using “edit” instead of “raw” in the link?
So for example !addjoke JOKE and it would get added to the Paste?

Thanks in advance. New to this website so I’m not sure if I should be replying here or making my very own post.

@Monario if you want to add the jokes with a command, might be easier to use the Quote custom API, instead of naming it !quote and !addquote, rename it to !joke and !addjoke.

Done everything but it says after i put in the command that the response must be under 400
characters my pastebin:

@xgerhard – Thanks for the response :slight_smile: Yeah that’s what I have been doing lol, recycling the quote system.
Still it would be awesome to know if it’s possible to do with Pastebin as well, not only to not have to rely on the quote system itself, but for learning as well.
Having gone through the forum and checking requests from people I’ve learned a lot about what Nightbot can do, but I feel like I’m nowhere near close the full scope of it :stuck_out_tongue:

If anyone does know of a way to send the new entries to Pastebin via a command I’d highly appreciate it!
Thanks in advance!

Are you using the raw version of the pastebin link in your command?

Yeah for sure am :slight_smile: .

My script: !commands add !joke -cd=5 $(eval var jokes = “$(urlfetch “.split(”,”); jokes[Math.floor(Math.random() * (jokes.length - 1))];)
NightBots response: [Response must be less than 400 characters]

Is there anyway i can test scripts for nightbot also :smiley:

Looks like the quotation marks (") in your pastebin are interfering with the javascript in the command and cases it to to error. You’ll need to remove or replace them with single quotes (’) and it should work :thumbsup:

1 Like

@themagickick – A good way to test commands for me is to use Discord :stuck_out_tongue:

Oh wow that sounds like a fun thing to do for sure for the community. I would also maybe have the ability for them to write you or so to add their jokes with your permission. This way get a never ending non repeating joke message response. Great idea though

It seems like if the Pastebin file has more than 400 characters total, the command will see it as if it had to spit out the entire list.

Can anyone confirm this, please?

I’m testing it on an 8ball command and I know for sure each entry (before each comma [,]) is right around 20 characters.

1 Like

Nightbot can only return up to 400 characters. As far as I know, the length of the info in the pastebin has no reason to effect it, as long as you have it set to only take a portion of it. Mind pasting your command and the pastebin?

Here you go sir:
editcom !8ball :8ball:: $(eval var q = "$(urlfetch ".split(","); '$(querystring)'.trim() == '' ? 'Please ask a question.' : q[Math.floor(Math.random() * q.length)]; )

The portion after the split is just to check the person has actually asked a question.
There should be 495 characters in the Pastebin file now.

Basically I just changed the split to be at a semi colon instead of comma. So use this:
!editcom !8ball $(eval var q = "$(urlfetch ".split(";"); '$(querystring)'.trim() == '' ? 'Please ask a question.' : q[Math.floor(Math.random() * q.length)];)
You will need to change the link, as this is mine so you can’t edit it, just use yours and edit it, unless you don’t plan on adding more responses. Up to you.
Just replace the commas at the end with a semi colon. As to why it didn’t work now, I’m not 100% sure. If you had a comma within one of the phrases I see how it could mess things up, but it doesn’t, so mind = blown.
This one works for me anyway.

Ok scratch that, tried again and it’s not working. In that case I guess it just simply can’t be over 400 characters. Not really sure though, but can’t think of another reason.

Was just about to reply to you that it still doesn’t work lol :frowning: But thanks for trying anyway.

I suppose it would make sense that it sees more than 400 characters total and that’s what matters… because the .split technically doesn’t change the total number of characters in the file lol… that’s a bit messed up though if that’s how that works D:

Edit: I wonder… What if it were to not only pick an “entry” at random, but a file at random too? And the second file just contains more unique responses…

The responses in that link are seperated by a semicolon, so I replaced the "," in .split() with a ";".
also you don’t need to use $(querystring).trim() either, just $(querystring) should work fine.
lastly you should be multiplying by q.length-1 as the first item in an array is at position 0

Here’s the command I got working:
!editcom !8ball :8ball: $(eval q = "$(urlfetch".split(";"); "$(querystring)" == "" ? "Please ask a question." : q[Math.floor(Math.random() * (q.length-1))];)

Edit: looks like that pastebin has been removed so the command is returning a 404, that was working for me before it had been removed.

@Stoupedog – Sorry, MrBSGaming and I were testing things. Could you try that exact command with the new link please?

Because it still gives me the same 400 characters limit message :frowning:

Edit: The above link should contain a bunch of “Test” entries at the bottom that throws it over the 400 character limit, but Pastebin is being difficult. If it doesn’t, here’s a fresh one:

Edit 2: Also apologies about the commas and semicolons, that was another thing we were testing. The new (hopefully not broken) file should have comas like it was meant to from the beginning.