Can't randomly select between viewers

Asked Nov 5, 2022·2 replies·Last activity 3 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'm trying to create a command that randomly decides between the viewer using the command and either a viewer they submit or a random user. My code currently looks like this:

$(eval $(query)?u=[$(user),$(touser)];:u[$(user),$(urlfetch https://2g.be/twitch/randomviewer.php?channel=kirascendant)] ;; u[Math.floor(Math.random()*u.length)]) wins!

When I use this command in chat is always returns "Unexpected identifier wins!", whether I specify a touser or not.

2 replies

Hey @Frisky Orangutan!

Here's how I'd write such command:

$(eval u='$(user)'; '$(touser)' === u ? t = '$(urlfetch https://2g.be/twitch/randomviewer.php?channel=$(channel))' : t = '$(touser)'; a = [u, t]; `${a[Math.floor(Math.random() * a.length)]} wins!`;)

You were close, your biggest mistake was the semi-colon (;) inside of your ternary operator (?:) when you put just one only at the end, here's how you should have written it to avoid errors:

'$(query)' ? u = ['$(user)', '$(touser)'] : u = ['$(user)', '$(urlfetch https://2g.be/twitch/randomviewer.php?channel=$(channel))'];
Polar Cat said:
```
$(eval u='$(user)'; '$(touser)' === u ? t = '$(urlfetch https://2g.be/twitch/randomviewer.php?channel=$(channel))' : t = '$(touser)'; a = [u, t]; ${a[Math.floor(Math.random() * a.length)]} wins!;)
```

Thanks! Semicolon and bracket placement always trips me up. Command works perfectly now!