urlfetch error: code generation from strings disallowed for this context

Asked Dec 31, 2021·2 replies·Last activity 4 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

I am trying to do a simple pastebin url fetch for an !affirmation commands that gives random affirmations. I am getting the 'Code generation from strings disallowed for this context' every time I enter in the command. Any suggestions?

$(eval a=$(urlfetch json https://pastebin.com/JjJuPB6R);a[Math.floor(Math.random()*a.length)])

2 replies

$(eval let to='$(touser)',a=$(urlfetch json https://pastebin.com/raw/WK2Ahtac); a[Math.floor(Math.random()*a.length)])

I'll remove the file so Please copy the pastebin content to your pastebin and use that

Hey @Ruby Dodo!

It doesn't work because you're not using the raw version of the Pastebin, and it won't work for two other reasons:
1. you need to turn your Pastebin into an array, we'll use .split().
2. Nightbot can't read the variables in a Pastebin if they've not been defined first.

With that in mind, here's a fix:

$(eval t=`$(touser)`; a=`$(urlfetch json https://pastebin.com/raw/JjJuPB6R)`.split(`;`); a[Math.floor(Math.random()*a.length)])

And inside your Pastebin replace all the instances of ${touser} with ${t}.