# Taging user after command

**URL:** <https://discuss.dev.twitch.com/t/taging-user-after-command/27472>\
**Category:** Chat and IRC\
**Created:** [August 2, 2020, 6:00pm UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472 "2020-08-02T18:00:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unstoppz](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/unstoppz/32/9850_2.png) [@unstoppz](https://discuss.dev.twitch.com/u/unstoppz)\
**Post date:** [August 2, 2020, 6:00pm UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472/1 "2020-08-02T18:00:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KaeseKuchenDEV](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/kaesekuchendev/32/9828_2.png) [@KaeseKuchenDEV](https://discuss.dev.twitch.com/u/KaeseKuchenDEV)\
**Post date:** [August 2, 2020, 7:45pm UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472/2 "2020-08-02T19:45:06Z")

</div>

Hey,

> [@unstoppz](#):
>
> client.action(target, `@${userstate.username}, whatever` );

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 😃 Feel free to ask as many questions, as you need to understand the Twitch API ^^

---

<div class="post-metadata">

**Author:** ![unstoppz](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/unstoppz/32/9850_2.png) [@unstoppz](https://discuss.dev.twitch.com/u/unstoppz)\
**Post date:** [August 2, 2020, 9:58pm UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472/3 "2020-08-02T21:58:01Z")

</div>

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 :

 ![Untitled](https://d2opxh93rbxzdn.cloudfront.net/original/2X/f/f53f91d839aaeb7eea97398a5abeeb4a22693c77.jpeg)

---

<div class="post-metadata">

**Author:** ![Dist](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://discuss.dev.twitch.com/u/Dist)\
**Post date:** [August 2, 2020, 10:46pm UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472/4 "2020-08-02T22:46:56Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![unstoppz](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/unstoppz/32/9850_2.png) [@unstoppz](https://discuss.dev.twitch.com/u/unstoppz)\
**Post date:** [August 3, 2020, 12:08am UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472/5 "2020-08-03T00:08:46Z")

</div>

oh i see thanks this resolved the issue 😀

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [September 2, 2020, 12:08am UTC](https://discuss.dev.twitch.com/t/taging-user-after-command/27472/6 "2020-09-02T00:08:49Z")

</div>

Closed on September 2, 2020, 12:08am UTC

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