Using $(user) and $(touser) inside of a urlfetch

I have my doubts that this is possible, but I figured I’d check with all of you first. I was working on making a !free command for a jail in our stream and I found out you can use raw pastebin files with urlfetch to make long lists of outcomes. I have plans to make a long JSON list of outcomes but I tested it first with a short list and nightbot interprets the $(user) and $(touser) as part of the string. I probably should have expected this but alas. Is there any way to differentiate those variables from the rest of the string?

Here is the command I tried out

$(eval a="$(urlfetch".split(";"); b="$(urlfetch".split(";"); c=a.concat(b); c[Math.floor(Math.random() * (c.length-1))]:wink:

First of all, I assume you’re using multiple Pastebins to avoid the 400-character limit/error.
Instead of doing that you can combine them into one Pastebin, then use $(urlfetch json instead.

You’re right in that variables in urlfetches aren’t replaced correctly, but you’re using an eval and can do it yourself by using the .replace() JavaScript functions. You should tweak them to use other “variables” though instead, such as $user and $touser instead.

Here’s a full example following my own suggestions (keep in mind that the Pastebin link is also changed, so make sure to modify that before you use it):
$(eval const results = `$(urlfetch json`.split(';'); const text = results[Math.floor(Math.random() * (results.length-1))]; text.replace(/\$user/g, `$(user)`).replace(/\$touser/g, `$(touser)`);)

I spaced it out so I can explain it better with JavaScript comments. It’s the same eval, just line-by-line explanations:

    /* Fetch results, split by semicolons as before. Keep in mind the Pastebin link is a bit different */
    const results = `$(urlfetch json`.split(';');
    /* Pick a random result */
    const text = results[Math.floor(Math.random() * (results.length-1))];
    /* Replace instances of $user and $touser with the correct names. `/\$user/g` is used to replace all instances of `$user` */
    text.replace(/\$user/g, `$(user)`).replace(/\$touser/g, `$(touser)`);
1 Like

This works perfectly! I can’t thank you enough! I did know about the JSON part, I just hadn’t learned how to write it yet and wanted to test it out first, but it seems easy enough. Thanks so much!

Hello! You can pass the command of $ (user and $ (touser without pastebin

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