Random user doesn’t work in if else

Hi,
I’m trying to make a hug command that gives a specific user a special message and works for everyone else just as a normal hug command where you can either target a user or get a random one.

I managed to get the first part to work as well as the second but together they don’t work.
When I want to get a random user I get an error message saying invalid channel. It works though if I target a user. Does anyone know why it doesn’t work?

Is it because the else part is technically if else as well?
Like if there is a message use that if it’s empty use a random user. And if so how do I fix this?

This is the command I am using :slight_smile:

!addcom !hugtest $(eval if(`$(user)`.toLowerCase() == "kyram"){`Error`}else{"$(user) gives $(eval decodeURIComponent("$(querystring)")||"$(urlfetch https://2g.be/twitch/randomviewer.php?channel=$(channel))") a warm and friendly hug paliHug fruitAww"}) 

Edit: I have tried redoing it with a Pastebin but i do not seem to do it right…
This time it gives me Unexpected token ‘{’ as error message.

This is my command:
$(eval u="$(user)"; r="$(urlfetch https://2g.be/twitch/randomviewer.php?channel=$(channel))"; q="$(query)"; $(urlfetch json https://pastebin.com/raw/YQJQ2YRt);)

and this is in my Pastebin:

if (${u}.toLowerCase() == "kyram") {
    var hugMessage = "${u} Error"; 
 } else if  (${q} == "") {
     var hugMessage = "${u} gives ${r} a hug";
 } else {
     "${u} gives ${q} a hug";
 } hugMessage;

Hey @Kyra!

I’m going to use the fact that $(touser) is identical to $(user) if no one is targeted.

$(eval '$(touser)' === '$(user)' ? t = '$(urlfetch https://2g.be/twitch/randomviewer.php?channel=$(channel))' : t = '$(touser)'; '$(user)'.toLowerCase() === 'kyram' ? 'Error' : `$(user) gives ${t} a warm and friendly hug paliHug fruitAww`;)

In case you don’t understand my code, I’m using the ternary operator.


In your first code, the $(channel) variable can’t work because it’s nested too deep in the code, the variable isn’t replaced by your channel name, a fix is to write the name of your channel, for example, according to your username on this forum:

$(urlfetch https://2g.be/twitch/randomviewer.php?channel=Kyra)

But if you’re using multiple $(eval) inside a single command, you’re doing it wrong, the code can be improved, or refactorized as we say.


In your second code, you’re using template literals wrong. Here’s how to properly write your Pastebin code, while simplifying it for Nightbot:

if (u.toLowerCase() === 'kyram') {
    `${u} Error`; 
 } else if (q === '') {
     `${u} gives ${r} a hug`;
 } else {
     `${u} gives ${q} a hug`;
 }

Template literals are to be used when you want to display a variable value inside a string, in which case you need to use backticks `, you don’t use ${} when using the variables in any other way.


It’s great you gave it a try tho’, too many ask us to write code without even trying, too few try by themselves first.

@Emily Thank you so much for helping and explaining!

1 Like

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