ViridianZe
ViridianZe

Reputation: 121

Discord.js check for a user mention inside a message

I have this code:

const args = message.content.split(" ");
args.shift();
var mention = message.mentions.users.first()
const subject = args[0];
        
        
if (!subject) {
  return message.say(`*you wand projects a stream of water*`)
} else if (subject === mention) {
  return message.say(`*Your wand projects a stream of water at ${mention} and they are now soaked*`)

and I am trying to get it to pick up the mention.users.first() variable called mention so people can specify to shoot the spell at someone. I can't quite figure out how to get it to be checked. I've tried a couple of things like:

} else if (subject === mention) {
} else if (subject === 'mention') {
} else if (subject === {mention}) {

I knew this one probably wouldn't work but I tried it:

} else if (subject === ${mention}) {

I can't figure out if I'm not wording it properly or if it is just the wrong way to do entirely. Any pointers or ideas would be appreciated.

Upvotes: 2

Views: 2203

Answers (1)

user13429955
user13429955

Reputation:

Well a role mention in <Message>.content would take the form of <@&ID>, it will be a sstring, discord.js already has a static property of the regex needed to validate role mentions which is: /<@&(\d{17,19})>/g, so now you just have to test if the string passes:

const regex = /<@&(\d{17,19})>/g;
if(regex.test(subject)) {

}

Also like I said you can get this regex by the static property https://discord.js.org/#/docs/main/v12/class/MessageMentions?scrollTo=s-ROLES_PATTERN

const {MessageMentions} = require("discord.js");
const regex = MessageMentions.ROLES_PATTERN;

Upvotes: 1

Related Questions