Different responses for different users

Hi, I’m trying to make a custom greeting on my twich channel, and the code I have is this.

$(eval u=’$(urlfetch json https://pastebin.com/raw/nW88mUvJ))

inside the pastebin I have this code

if(u === ‘user1’){‘hello user1’}
else if(u === ‘user2’){‘hello user2’}
else if(u === ‘user3’){‘hello user3’}
else {’¡hello @$user! :wave:’}

The code works fine, but I don’t know how to replace the username if it doesn’t match any of the above.

I tried to use .replace(/$user/g, $(user)), but it gives me an error.

$(eval u=’$(urlfetch json https://pastebin.com/raw/nW88mUvJ).replace(/$user/g, $(user)))

please, could you help me? Thank you.

Hiya, not sure what you mean, you want to remove the $(user) at the start? Why is it even there, why not include it in your if statements output, and leave it out at the else.

Hey @GokuSan!

This may help:

Actually, I have some free time, so let’s fix this.

When using a Pastebin you want to declare your Nightbot variables outside of it, I know your code works, but it’s best practice so if one day you need to use more than one variable, you won’t face an issue.

So here’s how to write your command:

$(eval u = '$(user)'.toLowerCase(); $(urlfetch json https://pastebin.com/raw/XXXXXXXX))

And then inside of your Pastebin have the following code:

switch (u) {
  case 'user1':
    'hello user1';
  case 'user2':
    'hello user2';
  case 'user3':
    'hello user3';
    `¡hello ${u}! 👋`;

Write every username (like user1) in lowercase.
I advise you to use a switch in your case to replace the if/else mess, but that’s basically the same thing.

To have the username in the message if it doesn’t match any of the case,
I used template literals: `text ${variable} text`,
but I could have done it like so as well: 'text '+ variable +' text'.

Thank you very much, it works wonderfully and you have helped me understand a little more how to create commands.

1 Like

thanks for your help, but emily already answered my question.

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