Unexpected identifier?

This is driving me nuts.

One of the other mods deleted a command I made by accident. I’m trying to restore it. By all accounts this should be exactly what I had before, but it keeps returning unexpected identifier from the third pastebin. Nothing has changed in any of these pastebins from when the command worked. Somehow now it just stopped working. I have no idea what I’m missing.

$(user) is $(eval a="$(urlfetch json https://pastebin.com/raw/i7a191cy)".split(";");a[Math.floor(Math.random()*a.length)]) $(eval a="$(urlfetch json https://pastebin.com/raw/RrjYAA91)".split(";");a[Math.floor(Math.random()*a.length)]) $(eval a="$(urlfetch json https://pastebin.com/raw/xriBtHdy)".split(";");a[Math.floor(Math.random()*a.length)])!

Hey @FancySkunk!

I see a few issues that might break your code, it’s surprising you didn’t experience them earlier:

  • the separator you’re using for .split() is the separator JavaScript uses to end instructions (;), therefore the compiler might misunderstand what you’re trying to do when it reads your code and end an instruction early, which would cause the “unexpected” type of errors.
    The solution is to change the separator, I’d personally recommend a single vertical bar (|) (two are an or statement) or an octothorpe (#) or a combination of multiple characters (|#), or to have JSON in the Pastebin directly (see below).

  • you have a separator after the last item of all of your lists, yet .split() cuts strings every time it encounters a separator, that means the created array’s last item is empty, and you don’t want the code to randomly pick that item.
    The way to fix that is to remove the last separator, or to do the random pick on a.length - 1, or to have JSON in the Pastebin directly:

    [
      "a 3D-rendered",
      "a backwards hat-wearing",
      //...
      "an unobtrusive",
      "an upside-down"
    ]
    

Having a JSON directly in the Pastebin fixes both issues at once, therefore that’s the way I’ll be heading.


On top of fixing the issues, you can improve your code significantly by putting everything in a single $(eval), which should reduce the code execution time and make the bot a bit more responsive; as I always say: if you use more than one $(eval), you’re doing something wrong.

You also want to be efficient with your code, that means avoiding repeating yourself, or to keep the code DRY. Since we don’t use .split() anymore because we switched to a JSON in the Pastebin, the next thing we can refactor so we don’t repeat ourselves, is the random pick of items, for that we’ll use a function:

const random = (array) => array[Math.floor(Math.random() * array.length)];

You could also put all your Pastebin in a single one, which would also reduce the execution time as $(urlfetch) can be expensive, and in that case we no longer need to format the content as JSON since we’ll be using arrays:

const firstArray = [
  'a 3D-rendered',
  'a backwards hat-wearing',
  //...
  'an unobtrusive',
  'an upside-down'
];
const secondArray = [
  'accordion player',
  'accountant',
  //...
  'youtuber',
  'zombie'
];
const thirdArray = [
  'from a land of fools',
  'from a town called Kickapoo',
  //...
  'with long, flowing locks',
  'with moves like Jagger'
];

Subtle change: " becomes ', while you must use double quotes (") when writing JSON, you can use other quote markers when writing JS; arrays being part of JS and single quotes (') being easier to type than double quotes ("), I decided to switch to single quotes (').

Since putting all the arrays in a single Pastebin dramatically simplifies the code and makes it more efficient, that’s the route I’ll take.


Alright, so with all those changes, how does the code looks like now? Like this:

$(eval const random = (array) => array[Math.floor(Math.random() * array.length)]; $(urlfetch json https://pastebin.com/raw/XXXXXXXX) `$(user) is ${random(firstArray)} ${random(secondArray)} ${random(thirdArray)}!`;)

I’m using template literals to create the response.

I tried changing over to |, and I’m encountering the same error. Also it’s super confusing because all three pastebins have the same formatting, and all three eval commands are identical. If it was a problem for the clauses, wouldn’t it also be a problem for the others? All signs keep pointing to there being something wrong in that clauses pastebin, but for the life of me I can’t figure out what it might be.

Alright, that’s definitely just a mistake on my part, and it’s remedied now.

Slight problem. I have another command that calls on these pastebins to return relevant statistics for them, and I really do not want to rework that as well.

I really do appreciate the time you took to show me the proper way to get this done. At the end of the day though, I just want to get this thing back to working status asap, even if it means that my code is going to be inefficient and/or crappy until I have the time to go back and do it the right way. Right now, I just need to know what got screwed up in that pastebin for the clauses.

Alright, I hear you, I’ll write you code that should work with the way you made those Pastebin, I noticed that there still is a semicolon (;) after the last item of your Pastebins, therefore I’ll use a.length - 1, and to avoid risks of the compiler misunderstanding your separator as an end of statement we can use template literals.

I see, so I’ve looked a bit more in your third Pastebin, and the issue is that you used double quotes (") somewhere in your list, and since you use those to define strings in your code as well, this breaks it, so to prevent issues on that down the line, I’ll also use template literals.


Here’s code that works with the Pastebins in your first post (I tested it):

$(eval r = (a) => a[Math.floor(Math.random() * a.length - 1)]; s = (s) => s.split(`;`); i = s(`$(urlfetch json https://pastebin.com/raw/i7a191cy)`); j = s(`$(urlfetch json https://pastebin.com/raw/RrjYAA91)`); k = s(`$(urlfetch json https://pastebin.com/raw/xriBtHdy)`); `$(user) is ${r(i)} ${r(j)} ${r(k)}!`;)

Man I feel like a dingus because that’s something I definitely knew when I made this thing a few months back.

The really confusing part though is that it did originally work in spite of that double quote being there. So something was definitely different originally but it’s been long enough that I have no idea what it was.

This is massively appreciated. Thank you so much for taking time to work on this for me.

1 Like

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