Trouble With A Command

I am very new to how commands work for nightbot. I want a command that sends a message with a random element. Due to the command length I have been trying to use $(urlfetch json). It’s also my first time using javascript (and I havent coded in 3 years).

Here is the Command:
Hi $(touser). Someone has requested a random gift for YOU! You have received: $(eval $(urlfetch json a = Math.floor(Math.random() * 6;if (a == 0){`Nothing!`}else if (a == 1){` - Pastebin.com))

Here is the Linked Code:

a = Math.floor(Math.random() * 6;
if (a == 0){`Nothing!`}
else if (a == 1){
`Tramatic Childhood Experience: `;
b = Math.floor(Math.random() * 5);
trauma = ["Coal for Christmas","A Broken Arm","Messed up Solo at Band Concert","Parents Fighting","A Dead Carnival Goldfish"];
trauma[b];
} else if (a==2){
`Tarot Serenade: `;
c = Math.floor(Math.random() * 22);
tarot = ["The Fool - https://bit.ly/3ytJQGY","The Magician - https://bit.ly/3N9Vn2v","The High Priestess - https://bit.ly/3M6Fw4C","The Empress - https://bit.ly/3l0Zv8Y","The Emperor - https://bit.ly/38drGyN","The Hierophant - https://bit.ly/3M3miwH","The Lovers - https://bit.ly/3KXBLx3","The Chariot - https://bit.ly/3M6HTo2","Strength - https://bit.ly/3L45eoT","The Hermit - https://bit.ly/3kVSbLP","Wheel of Fortune - https://bit.ly/3N9YqaX","Justice - https://bit.ly/3L7gAIK","The Hanged Man - https://bit.ly/3Fx77cJ","Death - https://bit.ly/3wch4JH","Temperance - https://bit.ly/3yBHrKi","The Devil - https://bit.ly/3yBHrKi","The Tower - https://bit.ly/37F1iNW","The Star - https://bit.ly/3stefBp","The Moon - https://bit.ly/3w0OuL6","The Sun - https://bit.ly/3w32OCK","Judgement - https://bit.ly/39SJbVr","The World - https://bit.ly/3M38vWQ"];
tarot[c];
} else if (a==3) {
d = Math.floor(Math.random() * 99);
d = d+2
d;
` Hip Hip Hurray(s)!`;
} else if (a==4) {
prize = ["A Brand New Car!","An all Expenses Paid Trip to Disney World!","A Cruise in the Bahammas!","One Million Dollars!"];
e = Math.floor(Math.random() * 4);
prize[e];
} else {`HUGS!!!`}

Hiya, please elaborate, what are you trying to do, what isn’t working?

After the urlfetch json in the actual command code is a pastebin link (gonna figure out how to fix that on here to alleviate confusion). None of the code works. It’s supposed to pick a random category, and then some of the categories pick random results to out put. 2 of the categories (nothing and hugs) just out put one thing. It’s supposed to add the out put to the end of the chat message. Instead i get everything before the fetch, and then an error message:

Code generation from strings disallowed for this context.

One of the outputs is a tarot card followed by a video link. Thats the bit.ly links in the code.

Hey @creedoflies!

Here’s a fix to your code:
Command code:

$(eval t = '$(touser)'; $(urlfetch json https://pastebin.com/raw/XXXXXXXX))

Pastebin code:

const base = `Hi ${t}. Someone has requested a random gift for YOU! You have received:`;
const r = Math.floor(Math.random() * 6);

const trauma = ['Coal for Christmas', 'A Broken Arm', 'Messed up Solo at Band Concert', 'Parents Fighting', 'A Dead Carnival Goldfish'];
const tarot = ['The Fool - https://bit.ly/3ytJQGY', 'The Magician - https://bit.ly/3N9Vn2v', 'The High Priestess - https://bit.ly/3M6Fw4C', 'The Empress - https://bit.ly/3l0Zv8Y', 'The Emperor - https://bit.ly/38drGyN', 'The Hierophant - https://bit.ly/3M3miwH', 'The Lovers - https://bit.ly/3KXBLx3', 'The Chariot - https://bit.ly/3M6HTo2', 'Strength - https://bit.ly/3L45eoT', 'The Hermit - https://bit.ly/3kVSbLP', 'Wheel of Fortune - https://bit.ly/3N9YqaX', 'Justice - https://bit.ly/3L7gAIK', 'The Hanged Man - https://bit.ly/3Fx77cJ', 'Death - https://bit.ly/3wch4JH', 'Temperance - https://bit.ly/3yBHrKi', 'The Devil - https://bit.ly/3yBHrKi', 'The Tower - https://bit.ly/37F1iNW', 'The Star - https://bit.ly/3stefBp', 'The Moon - https://bit.ly/3w0OuL6', 'The Sun - https://bit.ly/3w32OCK', 'Judgement - https://bit.ly/39SJbVr', 'The World - https://bit.ly/3M38vWQ'];
const prize = ['A Brand New Car!', 'An all Expenses Paid Trip to Disney World!', 'A Cruise in the Bahammas!', 'One Million Dollars!'];

switch (r) {
  case 0:
    `${base} Nothing!`;
    break;
  case 1:
    `${base} Traumatic Childhood Experience: ${trauma[Math.floor(Math.random() * trauma.length)]}`;
    break;
  case 2:
    `${base} Tarot Serenade: ${tarot[Math.floor(Math.random() * tarot.length)]}`;
    break;
  case 3:
    `${base} ${Math.floor(Math.random() * 99) + 2} Hip Hip Hurrays!`;
    break;
  case 4:
    `${base} ${prize[Math.floor(Math.random() * prize.length)]}`;
    break;
  case 5:
    `${base} HUGS!!!`;
    break;
  default:
    'There was an error.';
}

I defined the Nightbot variable t = '$(touser)' outside of the Pastebin, so I could use it inside it, see this topic.

I used a switch instead of the if/else mess to improve readability, but that’s basically the same thing.

I also improved your random number generation to select an item in an array: by using the array length you don’t have to wonder how many items there are in it, and if you expand one of them you won’t have to update the rest of the code, it’ll still work as expected, it’ll automatically accommodate for the new length.

I also reduced the amount of steps by combining lines together, so we pick the random array item on the same line of code we pick the random index.

Now regarding the issue that was breaking your code: look at your variable a initiation and see if you can spot it! :wink:

1 Like

no closed parentheses. I’m a dumbass. Thanks for the help! It works like a charm. This was my first time attempting JavaScript and it didn’t help I was overtired.

1 Like

It’s an easy omission, don’t be too hard on yourself!
To avoid such issues in the future you can use a code editor, it’ll autocomplete your code, and signal potential errors.

1 Like

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