NightDev Community Forums

Tip: How to do shoutouts with @ in front of the user

How to do shoutouts with @ in front of the user

Background Story

Many streamers and moderators have set up a simple way to do shoutouts to announce important people in their streams. The issue that I always see is that when someone does a shoutout like this !shoutout @unarmedguitar you get something like Please give a big shoutout to @unarmedguitar. Be sure to follow them at https://twitch.tv/@unarmedguitar

Notice how that @ appears in front of my username and the link also has @ infront of it. Now you could get this result or you may get Unknown User. Either way, you should be able to use @ in front of the user and it should not affect your shoutout command.

The reason you want to use this @ reference for a user is because you can take advantage of autocomplete, which will allow you to easily complete the user you are shouting out without making any mistakes with their name. That is another common issue you can have when trying to type someones name.

The code

This is a simple command I have created to address this issue. This code is not heavily tested and is by no means perfect but it does the job. This command uses add to add a new command, if you have already set up a shoutout command you would use edit instead. You may also need to change !so to your command you set up for shoutout, for example: !shoutout

!commands add !so -ul=moderator $(twitch $(eval "$(touser)".replace("@", "")) "Please give a big shoutout to {{displayName}}. Be sure to follow them at {{url}}")

Before I explain what this code does, I will show you how to test it and what it should output so you can compare the results.

!so @unarmedguitar

or you could do
!so unarmedguitar

either method is fine and this is to help those who are already accustomed to not using @ for the username.

Here is the result

Nightbot: Please give a big shoutout to unarmedguitar. Be sure to follow them at https://twitch.tv/unarmedguitar

Now this is exactly what you want to happen.

Code Explanation

Let’s get some links for pieces that will help you, so I can just explain what my command does:

Nightbot Docs - commands
Nightbot Docs - Variables - twitch
Nightbot Docs - Variables - eval
Nightbot Docs - Variables - touser
EMCAScript/Javascript - String - replace

Now that the documentation is out of the way, let’s explain the code.

$(twitch $(eval "$(touser)".replace("@", "")) "Please give a big shoutout to {{displayName}}. Be sure to follow them at {{url}}")

So first let’s fill in this with the shoutout command !so @unarmedguitar

$(eval "@unarmedguitar".replace("@", "")) results into unarmedguitar

$(twitch unarmedguitar "Please give a big shoutout to {{displayName}}. Be sure to follow them at {{url}}") results into Please give a big shoutout to unarmedguitar. Be sure to follow them at https://twitch.tv/unarmedguitar

And that is what you would expect.

Now this is not foolproof code, it can be broken, but since only moderators and above userlevels can use this code, you should trust that they will not attempt to break the code. If however, you do have issues then you should read what RokettoJanpu has added.

If anyone has anymore tips, hints, hacks or workarounds for Nightbot then I would like to hear them. Be nice to see other methods or annoyances that people have encountered.

Thank you

@unarmedguitar

I like the idea of making this post that introduces a solution to a pretty common problem while giving other users a glimpse of how you can use coding to solve various other problems. However, there’s just a couple issues with the $(eval) code I’d like to remedy.


Markdown Is Our Friend

Markdown is a markup language used to customize the look and feel of text. For example, it’s how many GitHub users write those fancy-looking introductory README files.

This forum also supports the use of Markdown. One good use of it here is to ensure that no accidental formatting issues happen when sharing snippets of code.

Here is an example of using Markdown to protect a code snippet. This is a command response that makes Nightbot output a random number between 1 and 100 to the chat:

$(eval "The random number is: " + Math.ceil(Math.random()*100))

Notice how the entire response is enclosed within a gray box and how the JavaScript syntax is highlighted? That’s because I put 3 backticks above and below as shown in the following screenshot:

markdown

This is the same command response but without those backticks:

$(eval “The random number is: ” + Math.ceil(Math.random()*100))

Notice that the quotation marks around the string The random number is: are now curly quotes. This will break the $(eval) code and result in an error. Also, you lose the nice syntax highlighting.

Running replace() on a String

The syntax for running the replace() method on a string is:

string.replace(searchvalue, newvalue)

where searchvalue is either a value or a regular expression that you want to replace in a string and newvalue is the value to change that value to.

If you use a value for searchvalue, only the first occurrence of searchvalue will be replaced.

If you use a regular expression with the global modifier for searchvalue, all occurrences of searchvalue will be replaced.


With these issues in mind, I propose the following command response. There are a few other ways the code in this response differs from the original, but I won’t go into them as deep.

!addcom -ul=mod !so $(twitch $(eval decodeURIComponent(`$(querystring)`).split(` `)[0].replace(/@/g,``)||`$(user)`) "Please give a big shoutout to {{displayName}} and be sure to follow them at {{url}} They were last seen streaming {{game}}.")

For those interested in the other differences in the code…

In place of:

"$(touser)"

I decided to use the following code. It works in the same way. Use the first word in the input. If the input doesn’t exist, output the command user’s name:

`$(query)`.split(` `)[0]||`$(user)`

I’m running a decodeURIComponent on the querystringed version of the user input to make sure the user doesn’t break the code if they input a backtick (`)

decodeURIComponent(`$(querystring)`).split(` `)[0]||`$(user)`

All that’s left is to run a replace() on the input using a regular expression:

decodeURIComponent(`$(querystring)`).split(` `)[0].replace(/@/g,``)||`$(user)`
1 Like

Thanks for the feedback.

It is my first time here and I am still learning the ropes. I wasn’t too sure what forum software was being used. Also, I do a lot from my phone and if this place allows it. I can edit my post when I am behind a computer to style it better and actually improve on the content as I just wanted to get it published sooner.

I just came up with this solution 2 days ago after wondering why it has to be an issue. It really should be solved at source and not by what I call ‘hacks’ to fix it. I am relying on documentation as I have not seen the source or know if it is available to see.

I was wondering how I do code with syntax highlighting here but from my phone the only thing that I could see that may style the code better was blockquotes.

I did not want to make it too complicated that I would get bombarded with support questions because I understand many users do not have the time to learn a language. Lucky for me, I like learning.

Thanks again for your input.

1 Like

Don’t sweat it if you’re not too familiar with this kind of text formatting. It’s your first post here! Plus, Markdown isn’t terribly difficult to pick up and it comes with some decent documentation.

I do agree that the $(twitch) variable should be able to deal with “weird” characters like @ in the user input due to how often people may include such characters when using commands. If you really feel something like this should be implemented, feel free to make a feature request on the forums, though I am not too sure such an idea will be implemented due to this workaround using $(eval) nested within $(twitch)

1 Like

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