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.