How Discord Points Out Work

Discord uses a special syntax to embed mentions in a message. For user mentions it is the user's ID with <@ at the start and > at the end, like this: <@15790751620966987>. If they have a label there will likewise be a! after the @.

Function points out and channel mentions work similarly. Role discusses look like <@&324576511236102764> and channel mentions like <#111457313000036941>.

That is to say when you get a message from the Discord API and it consists of pointing out the message's material will include that unique syntax.

How To Work

So, how can you use this new info for your bot?

The majority of your code will not change, but instead of utilizing message.mentions to discover the pointed out users you will need to do it by hand. This might sound scary in the beginning, but once you see the code you will see it is in fact quite easy. Let's say you have an easy command handler like this one.

client.on(' message', message => );.
const command = split [0];.
const args = split.slice( 1 );.
);.

Now you can quickly check the waters by updating the avatar command from last time. It is pretty simple, and it will show the avatar of who utilized the command.

if (command === 'avatar')
const user = message.author;.

return message.channel.send('$ 's avatar: $ user.displayAvatarURL( dynamic: real) ');.

However, how do you actually get the proper user now? Cetainly, this requires a few more basic steps. Using a function will make it quickly multiple-use. We can use the name getUserFromMention here.

function getUserFromMention( reference)

As you can see it is a fairly straight forward function. It essentially just works itself through the structure of the mention bit by bit.

Inspect if the mention begins with the <@ and ends with a > and after that get rid of those. If the user has a label and their reference consists of a! remove that too.

Just the ID must be left now, so use that to fetch the user from the client.users.cache Collection. Whenever it experiences an error with the reference (i.e. void structure) it simply returns undefined to signify the reference is invalid.

Now you have an awesome function you can use whenever you want to convert a raw reference into proper user things. Plugging it into the command will give you this.

if (command === 'avatar')
if (args [0]

return message.channel.send('$ , your avatar: $ ');.

And here we simply plug the brand-new function into the command. If the user-supplied an argument it should be the user reference, so it just gets passed right into the function.

And that is it! Basic, isn't it? Start up your bot and see if it works.

So now, instead of utilizing message.mentions you can utilize your new, wonderful function. This method will enable you to add an appropriate look for all your args. And you can tell when a command was utilized correctly and when it was used incorrectly.



Leave a reply



Submit