Taging user after command

So basicly i want when somebody uses for example : !tag command that command returns his username, than whatever msg. Im using tmi.js

function onMessageHandler (target, context, msg, self) {
  if (self) { return; } 

  const commandName = msg.trim();

  if (commandName === '!tag') {
    client.action(target, ` whatever`);
  }

that will post whatver but i seen ppl use

if (commandName === ‘!tag’) {
client.action(target, @${userstate.username}, whatever);
}

and ofc i would add userstate with arguments onmessagehandler but it would still break. Can anyone help me with this i know its stupid thing but i cant figure it out ! Thanks

Hey,

Due to the Forum styling, one can only see the “``” , when quoting it, so i was confused for a second :D. But basically this should work, as the backticks with the ${} notation to get a string value out of the code inbetween the { and }. Can you maybe specify what exactly is breaking? Like the error code or stack trace you get.

And

Can anyone help me with this i know its stupid thing but i cant figure it out

This isn’t a stupid question, as there wouldn’t be a Forum if everything would be self-explanatory :smiley: Feel free to ask as many questions, as you need to understand the Twitch API ^^

So if run it like this

function onMessageHandler (target, context, msg, self, userstate) {
if (self) { return; }

const commandName = msg.trim();

if (commandName === ‘!tag’) {
client.action(target, @${userstate.username}, whatever);
console.log(executed command);
}
}

idk why it doesnt show `` in code but its before @ starting one and after whatever ending one

and i get :

userstate is undefined and throwing an error because the tmi.js message event passes the arguments channel, userstate, message, and self, to your function. You’ve set userstate as the 5th argument of your function so the value will always be undefined.

You can look at the tmi.js docs to see what argumenta are provided by different events https://github.com/tmijs/docs/blob/gh-pages/_posts/v1.4.2/2019-03-03-Events.md#message so based on that you can see that argument you call context is what is the userstate object. You can either rename context to userstate, or where you reference userstate change that to context.

2 Likes

oh i see thanks this resolved the issue :grinning: