Random number in text at pastebin.com

Hi, I made an 8ball command and read the pastebin passages randomly and I would like to add a random number to the text … how do I do it?

My code:
!commands edit !8ball /me -cd=5 :8ball: $(eval responses = $(urlfetch json https://pastebin.com/raw/RNNZZunt).split(","); responses[Math.floor(Math.random() * responses.length)]:wink:

Unfortunately, I have only this in the text:
You win $(eval Math.floor((Math.random() * 100) + 1)) points…,

btw: I am sorry for my bad English

Having $(eval) or $(user) in a pastebin file will have it simply printed out exactly as is. Nightbot won’t know it must replace the $(eval) with random numbers.

I copied your pastebin and fixed it up a bit so I can use code to add random numbers to the output. Take a look at this: https://pastebin.com/raw/phsy9Asi

If the output contains randNum it will replace it with a random number from 1-100. If it contains commandUser it will replace it with the name of the person using the command.

Use my example to fix your paste (or feel free to use my paste link instead of yours), then run this command in your chat:

!editcom !8ball /me $(eval a=$(urlfetch json https://pastebin.com/raw/RNNZZunt).split(",");a[Math.floor(Math.random()*a.length)].replace(/randNum/g,function(rng){return Math.floor(Math.random()*100)+1;}).replace(/commandUser/g,"$(user)"))

1 Like

Thank you very much, that’s exactly what I needed :slight_smile:
I do not understand why this command must be written in this way
.replace(/randNum/g,function(rng){return Math.floor(Math.random()*100)+1;})
what is function(rng) ?
I understand that this will cause a calculation command in some way, but what is the argument “rng” ?

The function I wrote within the replace method replaces each randNum with a new random number. Without it, it’ll replace all randNum with the same number. The rng argument isn’t completely necessary, I just kept it there while I was writing the rest of the code to remind myself what the function was for (rng stands for random number generator).

1 Like

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