Different responses for different users

Asked May 17, 2022·4 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.

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

$(user)';
if(u === 'user1'){'hello user1'}
else if(u === 'user2'){'hello user2'}
else if(u === 'user3'){'hello user3'}
else {'¡hello @$user! 👋'}

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.

4 replies

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 @Rapid Horse!

This may help:
/t/use-nightbot-variables-in-pastebin/1534636064776392835#m1534636077959352381

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';
break;
case 'user2':
'hello user2';
break;
case 'user3':
'hello user3';
break;
default:
`¡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.

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