How to make a variable response command based on the user using it

I was wondering how I would go about making a command that changes responses depending on who uses it, I would assume that you would have to make $(user) as a variable in pastebin sorta like this as I don’t know the coding language too well for Nightbot :

u=$(user)

if u=sweetshannon then
“sweetshannon sends her love to $touser”
elseif u=hoodlumkira then
“hoodlumkira pushes $(touser) down the stairs”
else “$(user) gives $(touser) a massive hug”

I am not sure if it is at all possible with the character limitations to do with twitch, but I am hoping it is possible to do, any help with this would be great :grin: thanks in advance

Hey @hoodlumkira!

Have a look here:

1 Like

That helps heaps, thank you Emily, :grin:

Also I am curious how does the switch command work or is it basically the coding language that Nightbot uses version of an if statement? or would it have to be a different command to call things like t=(touser) or r=(randomviewercode)

1 Like

switch() is part of JavaScript, only $(variables) are Nightbot’s.

Not really, you can either replace u = '$(user)' with t = '$(touser)'; or if you plan to use both simply add one after the other: u = '$(user)'; t = '$(touser)', and then call the two variables (u and t) in the Pastebin.

1 Like

Thank you, very much Emily I will give it a try and hopefully it all works thank you very much for your help :grin:

1 Like

So I have sorta have part of the command I want to use and have ran into a problem when I call the command, it says “Unexpected identifier” regardless of who I do tag as the touser I have also tried using the user variable instead to see if it would work but it got the same result

$(eval u=`$(user)`; t=`$(touser)`; q=`$(urlfetch json https://u.nu/AFdoF)`.split(`;`); $(urlfetch json https://u.nu/VZFpR))

the q variable is meant to call a randomized quirk and use it in the last urlfetch to replace the ${q}'s, at the moment I do want to figure out this problem first but I am planning to see if I can do something like bellow unless its impossible

switch (u) {
     case 'hoodlumkira':
          switch (t) {
               case 'exampleuser":
                     "insert text";
               default:
                    `default text inserted here`;
}
}

The error appears to be the pastebins you have linked are private/under moderation as I get a 403 error trying to view them.

1 Like

Oh shoot my bad I have fixed the pastebins up and changed them to public, I am sorry about that, now it comes up with “Unexpected String” when I do use the command now

https://u.nu/AFdoF

Remove the double quotes " and the very last semi-colon ;

https://u.nu/VZFpR

Your last two cases miss the break; keyword, it’ll cause issues.

1 Like

I have fixed up both of those couple issues in the pastebins but am still encountering the same issue with Nightbot saying “Unexpected string” regardless which person I tag or even if I leave it blank

There’s a missing semicolon and colon in the last case before the default.

1 Like

I’ve gone over It again and corrected the missing the semicolon and colon, which it now posts but it when it posts for example it will post this “${u} attempts to beat ${t} using their ${q} quirk, but ${t} used all for one to steal their quirk rendering them defenseless” but wont swap out the ${u}, ${t} or ${q} with the variables that it should be calling in the Nightbot code and I dont understand why it doesn’t swap them out

You have to use ` backticks and not ’ to make a concatenation string to have variables like that.

var string = `text`;

console.log(`${string}`);
//returns 'text'

console.log('${string}');
//returns '${string}'
1 Like

Ahh so to get the ${u} back ticks have to be used other wise it will return the exact text, so I have fixed all of that, there is only one issue atm and that seems to be with the ${q} as it is trying to post the whole list without picking one single line I did try using the q[Math.floor(Math.random()*q.length) in the nightbot command but I feel thats not the solution as that returns another error "Unexpected token ‘switch’ "

Your missing an ending ] and possibly a ;

1 Like

This is the current code in nightbot even after adding the math random part its still trying to post the whole list I have even tested it using a case with nightbot with just ${q} and nightbot just says “[Response must be less than 400 characters]”

$(eval u=`$(user)`; t=`$(touser)`; q=`$(urlfetch json https://u.nu/AFdoF)`.split(`;`); q[Math.floor(Math.random()*q.length)]; $(urlfetch json https://u.nu/VZFpR))

Ahhh I think I have fixed it for now by the looks of things by using r as a variable and then changing q variable to the random math

$(eval u=`$(user)`; t=`$(touser)`; r=`$(urlfetch json https://u.nu/AFdoF)`.split(`;`); q=r[Math.floor(Math.random()*r.length)]; $(urlfetch json https://u.nu/VZFpR))

I am also wondering if there is some way of making something like this working or would I have to use a different statement other than switch to make something like this possible as at the current time with what I have tested it returns “undefined” through nightbot when going to the second (u) case

switch (u) {
    case 'user1':
           switch (t) {  
                case 'hoodlumkira':
                `statement for user1`;
                break;
                default:
                'default statement user 1';
                }
      break;
      case 'user2':
          switch (t) {  
                case 'hoodlumkira':
                `statement for user2`;
                break;
                default:
                'default statement user 2';
                }
        default:
              'default statement for switch (u)'
}

Well, a switch() is only useful when you have “a lot” of cases, when you have just two where one of them is a default I’d just use an if()/else.
Also, I’d make u and t lowercase and use lowercase usernames in the tests, so you don’t have to care about casing in the switch(), and in case it changes, just write it in lowercase everywhere.

$(eval u='$(user)'.toLowerCase(); t='$(touser)'.toLowerCase(); r='$(urlfetch json https://u.nu/AFdoF)'.split(';'); q=r[Math.floor(Math.random()*r.length)]; $(urlfetch json https://u.nu/VZFpR))
switch (u) {
  case 'mellbell83':

    switch (t) {  
      case 'hoodlumkira':
        `${u} attempts to beat ${t} using their ${q} quirk, but ${t} used all for one to steal their quirk rendering them defenseless`;
        break;
      case 'saltibunni':
        `${u} tries sneaking up on ${t} to use their ${q} quirk, but ${t} heard them and uses her blueflame quirk to trap ${u}`;
        break;
      case 'mellbell83':
        `${u} tries to sneak up on ${t} to use their ${q} but, hoodlumkira interferes using all for one and steals ${u} quirk protecting ${t} and rendering ${u} helpless`;
        break;
      case 'shannonsully13':
        `${u} tries to run up and use their ${q} to assasinate ${t} but, ${u} uses her erasure quirk to erase ${u}s quirk`;
        break;
      case 'nightbot':
        `test ${q}`;
        break;
      default:
        `${u} attempts to beat ${t} using their ${q} quirk, but ${t} used all for one to steal their quirk rendering them defenseless`;
    }
    break;

  case 'hoodlumkira':
    if (t === 'nightbot') {
      `test ${q}`;
    } else {
      'Testing';
    }
    break;

  default:
    'No response yet';
}

Most of the time you can use single quotes ', it’s only when you try to add variables to text without using + that you need to use backticks `, that’s called template literals, I explained that in the topic I linked when I first answered you.
Also it’s preferable to use spaces instead of tabs to help with readability.
Pay closer attention to the syntax of what you write, JavaScript is forgiving, but its syntax is still strict.


Finally, why this works:

r='$(urlfetch json https://u.nu/AFdoF)'.split(';'); q=r[Math.floor(Math.random()*r.length)];

but this doesn’t:

q='$(urlfetch json https://u.nu/AFdoF)'.split(';'); q[Math.floor(Math.random()*q.length)];

is just because you forget to store q[Math.floor(Math.random()*q.length)] in a variable,
no need to introduce a new variable, although it’s preferable when working on proper code, but when it comes to Nightbot we’re trying to save space and variable names as much as possible, so this would work too:

q='$(urlfetch json https://u.nu/AFdoF)'.split(';'); q=q[Math.floor(Math.random()*q.length)];
2 Likes

Thank for the help guys very much for the help, I do apologize that I did kept missing my syntax errors that is definitely something I do need to improve on,
I don’t quite understand how to use the template literals, but I’ll definitely gonna have to look into it abit more in the future
again I thank you both very much for the help you have given me

2 Likes

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