How To Check Mentions On Discord

# Parsing mention arguments

In a previous affiliate, you learned how to build commands with user input; you also learned how to use
mentions
as user input. However, using
message.mentions
can pb to a few problems.
For instance, yous practise not know which mention belongs to which argument. Or if you are splitting the message’due south content by spaces to get the args, The mentions will nevertheless take upwardly space in your args array, messing upwards the rest of your args parsing if y’all are not conscientious.

Say y’all are writing a bot for moderating your server. You will want a boot or a ban command, which allows you to mention the person you are trying to ban. But what happens if you try to use the control like this?


08/14/2021

!ban

@Offender

Because they were rude to

@Victim
.

You might expect it to ban @Offender considering that is who you mentioned beginning. However, the Discord API does non send the mentions in the society they appear; They are sorted past their ID instead.

If the @Victim happens to have joined Discord earlier @Offender and has a smaller ID, they might get banned instead.
Or maybe someone misuses a command, the bot might withal accept it, but it volition create an unexpected upshot.
Say someone accidentally used the ban command like this:



User




08/fourteen/2021

!ban Because they were rude to

@Victim
.

The bot will withal ban someone, but information technology will be the @Victim once again.
message.mentions.users
nonetheless contains a mention, which the bot will use. But in reality, you would want your bot to be able to tell the user they misused the command.

# How Discord mentions piece of work

Discord uses a special syntax to embed mentions in a message. For user mentions, information technology is the user’s ID with
<@
at the outset and
>
at the end, like this:
<@86890631690977280>. If they have a nickname, at that place volition also exist a
!
later the
@.
Role mentions and aqueduct mentions work similarly. Role mentions await like
<@&134362454976102401>
and channel mentions like
<#222197033908436994>.

That ways when yous receive a message from the Discord API, and it contains mentions, the message’southward content volition contain that special syntax.
If you send



User




08/14/2021

I recollect we should add together

@GoodPerson

to the

@Modernistic

role.

then the
message.content
for that message will await something like this

# Implementation

And then, how do you employ this new information for your bot?
Most of your code will not change; however, instead of using
bulletin.mentions
to detect the mentioned users, you volition accept to do it manually.
This may audio scary at first, but you will see it is pretty simple once you see the code.

Say y’all already have a simple control handler like this:

At present y’all tin can quickly test the waters by upgrading the avatar command from last time. This is what we have so far. It is pretty uncomplicated; it will show the avatar of who used the command.

But how practice yous get the correct user at present? Well, this requires a few elementary steps.
Putting it into a part will make it hands reusable. We will use the name
getUserFromMention
here.

As you lot can come across, it is a relatively straightforward function. It substantially only works itself through the structure of the mention bit by chip:

  1. Bank check if the mention starts with the
    <@
    and ends with a
    >
    and so remove those.
  2. If the user has a nickname and their mention contains a
    !, remove that as well.
  3. But the ID should be left now, so use that to fetch the user from the

    client.users


    client.users.cache

    Collection. Whenever it encounters an error with the mention (i.e., invalid structure), information technology merely returns
    undefined
    to bespeak the mention is invalid.

Now yous take a keen part you tin can use to convert a raw mention into a proper user object. Plugging it into the command volition give you this:

And hither, we plug the new function into the command.
If the user-supplied an argument, it should be the user mention, so it just gets passed correct into the part.

And that is it! Simple, isn’t it? Start up your bot and run across if it works.

AnotherUser



AnotherUser




08/14/2021

!avatar

@User

Guide Bot

So now, instead of using
message.mentions, y’all can apply your new, fantastic function. This volition allow y’all to add proper checks for all your args so that you can tell when a command is and isn’t used correctly.

But this does non mark the end of the folio. If you feel adventurous, you can read on and learn how to employ Regular Expressions to easily convert a mention into a user object in just 2 lines.

# Ban command

You now know how to parse user mentions for a uncomplicated control like the avatar command. However, the avatar command does non benefit from information technology as much equally the intro’south example.

When writing a ban command where a mention might appear in the reason, manual parsing mentions is a lot more important. You lot can see an example of how to exercise it as follows:

Now if you ship a command like the following you tin ever be sure it will use the mention at the very forepart to figure out who to ban, and will properly validate the mention:



User




08/fourteen/2021

!ban

@Offender

because they were rude to

@Victim
.

# Using Regular Expressions

Previously you acquire how to employ rudimentary cord-related functions to turn the special mention syntax Discord uses into a proper discord.js User object. But using Regular Expressions (aka “RegEx” or “RegExp”), yous tin condense all that logic into a single line! Crazy, right?

If you have never worked with Regular Expressions earlier, this might seem daunting. Simply in fact, you already have used regular expressions. Call up
withoutPrefix.split(/ +/);? This piddling
/ +/
is a Regular Expression. The
/
on either side tell JavaScript where the Regular Expression begins and where information technology ends; the stuff in between is its content.

The RegEx you volition use for user mentions will expect similar this:
/^<@!?(\d+)>$/. Here is how the RegEx works:

  1. The
    ^
    at the beginning and the
    $
    at the end means the mention has to accept up the entire string.
  2. Yous accept the typical
    <@
    and
    >
    at the outset and end.
  3. The
    ?
    later on the
    !
    indicates that the
    !
    is optional.
  4. \d+
    ways the RegEx will look for multiple digits, which will be the ID.
  5. The parentheses around the
    \d+
    create a capture group, which allows you to go the ID out of the mention.

Using the
.match()
method on strings, yous can get the capture group’s values, i.east., the mention’s ID.

Alert

discord.js has congenital-in patterns
(opens new window)

for matching mentions, however every bit of version 11.4 they exercise not contain whatever groups and thus aren’t useful for actually getting the ID out of the mention.

Updating your
getUserFromMention
office to apply RegEx gives yous this:

See? That is
much
shorter and non that complicated. If you rerun your bot now, everything should still work the same.

# Resulting code

If yous desire to compare your code to the code we’ve synthetic so far, you tin can review it over on the GitHub repository here

(opens new window)
.

Source: https://v12.discordjs.guide/miscellaneous/parsing-mention-arguments.html