Error importing Pastebin lines

I’m using the following command to try and input a random line from a pastebin, but I keep getting “undefined” instead of an imported line:

$(user) has challenged $(touser) to a $(eval chaltype = “$(urlfetch “.split(”;”); chaltype[Math.floor(Math.random() * 29)])

How can I fix this command?

Hey @kirascendant!

Here’s how I’d write your command:

$(eval p = `$(urlfetch json`.split(`;`); `$(user) has challenged $(touser) to a ${p[Math.floor(Math.random() * p.length)]}.`;)

Assuming the content of your Pastebin exactly looks like this, it should work:

option 1;
option 2;
option N

Adding a semicolon at the end of the last option can create issues.

I made your code a bit more flexible, so you don’t have to update the random pick every time, instead it bases it on the number of entries you have on your Pastebin:

p[Math.floor(Math.random() * p.length)]

If you put a semicolon at the end of the last option, use p.length - 1 instead of p.length.

The error was created because you split with a semicolon: a semicolon indicates the end of an instruction, except if put inside backticks (`).

Thanks for your help! I’m new to Nightbot and greatly appreciate information on the language.

I’m actually using this code twice in the same command, once for a random question and once for a random answer. Both sets of prompts are separated within the same pastebin, which is why I’m currently using a set number. I intend to use a Math.random() * (second part length) + (first part length) for the answer section of the command.

Would you mind explaining how backticks affect a command? I understand brackets being used for arrays and parentheses being used for scope.

It makes sense for backticks to replace quotation marks in selecting the splitting character, since ; can cause other errors, and that you declare the array with a ; before calling it, but why are there backticks around the section that calls the defined variable?

Update: Using your line of code exactly as written, but with the correct pastebin address, Nightbot responds “Unexpected identifier”. Is there something I’m missing?

Nightbot uses JavaScript inside the $(eval) variable, with specific variables here and there occasionally, nothing too extravagant.

Then use the .length property and divide it by two? I’m trying to help you write code that you won’t need to update every time you edit your Pastebin, which is how ideal code should work, it has to be flexible.

p[Math.floor(Math.random() * Math.floor(p.length / 2))]

That’d work if all the answers are at the end of the Pastebin, I have to assume it’s the case given you haven’t shared it.

p[Math.floor(Math.random() * Math.floor(p.length / 2)) + Math.floor(p.length / 2)]

The advantage of the two last bits of code is that even if you add a semicolon to the last entry of your Pastebin, this won’t create an issue, contrary to what I said before, and that’s thanks to Math.floor() when dividing in half.

Clean code also respect the DRY principle, so if we notice an element that’s repeating, you want to store it in a variable, or something else, with all that in mind, that’s how I’d rewrite the code:

$(eval p = `$(urlfetch json`.split(`;`); s = Math.floor(p.length / 2); `$(user) has challenged $(touser) to a ${p[Math.floor(Math.random() * s)]}. ${p[Math.floor(Math.random() * s) + s]}`;)

Nope, that’s my bad, I somehow forgot your Pastebin was full of semicolons, which messes with how JavaScript works, therefore you have to put the $(urlfetch) between backticks as well.

The backticks here are used to create template literals, it’s just a way to write cleaner strings, and you can write simple operations in them too, I could have used regular quotes and + for it:

const population = 8000000000;
const string1 = 'hello world, there are ' + population + ' people on your back!';
const string2 = `hello world, there are ${population} people on your back!`;
// string1 and string2 are identical

Depends of what you mean by brackets:
[] are for arrays,
{} are for objects;
and parentheses are used to call functions.
For the most part at least, because this isn’t the unique use for brackets and parentheses.

But I’m not here to teach you how to write code, I already have enough work on my plate.
Writing code for Nightbot users is a favor we volunteers do, NightDev is in no way required to produce code for their users, it’s supposed to be for advanced users.

Also, I would have written the code you were looking for the first time around if you had specified the second part of your Pastebin, I recommend reading this to help us provide you better support in the future.

Hi there! I am also having a similar issue. When the command fetches a random outcome from pastebin, it’s adding

  • to the beginning of the outcome.

    Here’s the command for reference:
    !commands add !challenge $(eval u=$(user); t=$(touser);r=$(urlfetch json|); r[Math.floor(Math.random()*r.length)]:wink:

    And the random outcomes come out in chat like this:

  • [user] absolutely destroyed [user]

    Not quite sure where I’ve gone wrong here!

  • Hey @ehayys!

    Use the raw version of the Pastebin:

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