veedoni
veedoni

Reputation: 35

Discord.js checking for mention in args

Hey so I am making a discord bot and when I use this to check for mention: message.mentions.members.first(); it will look for mention in whole message user has sent. I am trying to work this out because if user send message ?ban bla bla bla @user it will work. I want to check for mention only in args[0]. Is that possible? I am using discord v12. Thanks!

Upvotes: 1

Views: 783

Answers (2)

yes sir
yes sir

Reputation: 230

Mentions

USERS_PATTERN uses RegEx for checking if string mentions about a User. If string matches with the pattern it returns true, otherwise returns false.

const { MessageMentions: { USERS_PATTERN } } = require('discord.js');

...
  if (!args[0].match(USERS_PATTERN)) return message.channel.send("Please mention a user");
...

Upvotes: 1

gavin
gavin

Reputation: 1354

this is what i found to work...

if(args[0].slice(2).slice(0, -1) !== message.mentions.users.first()?.id) {
  return message.reply("Please start with a user...")
}

the args[0].slice(2).slice(0, -1) if a mention... will be the id of the first mention... and if the mention is the first arg, it will also be the first mention. So what I did was took ID of the first mention and compared it to the sliced args[0] to see if they match, else it will return telling them to please start with a user... Make sure to keep the ? in message.mentions.users.first()?.id just in the case of no mention in the message, it will not cause an error to the process and will also return the please start with a user message.

Upvotes: 1

Related Questions