Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Check if first argument is a mention

I'm coding a discord bot and I'm trying to make a kick command right now. I managed to find how to check if there's any mention in the command message with message.mentions.members.first() but I couldn't find anything to check if a specific argument is a mention.

Code I have so far:

module.exports = {
    name: "kick", 
    category: "moderation",
    permissions: ["KICK_MEMBERS"], 
    devOnly: false, 
    run: async ({client, message, args}) => {
        if (args[0]){
            if(message.mentions.members.first())
                message.reply("yes ping thing")
            else message.reply("``" + args[0] + "`` isn't a mention. Please mention someone to kick.")
        }
        else
            message.reply("Please specify who you want to kick: g!kick @user123")
    }
}

I looked at the DJS guide but couldn't find how.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

MessageMentions has a USERS_PATTERN property that contains the regular expression that matches the user mentions (like <@!813230572179619871>). You can use it with String#match, or RegExp#test() to check if your argument matches the pattern.

Here is an example using String#match:

// make sure to import MessageMentions
const { MessageMentions } = require('discord.js')

module.exports = {
  name: 'kick',
  category: 'moderation',
  permissions: ['KICK_MEMBERS'],
  devOnly: false,
  run: async ({ client, message, args }) => {
    if (!args[0])
      return message.reply('Please specify who you want to kick: `g!kick @user123`')

    // returns null if args[0] is not a mention, an array otherwise
    let isMention = args[0].match(MessageMentions.USERS_PATTERN)

    if (!isMention)
      return message.reply(`First argument (_\`${args[0]}\`_) needs to be a member: \`g!kick @user123\``)

    // kick the member
    let member = message.mentions.members.first()
    if (!member.kickable)
      return message.reply(`You can't kick ${member}`)

    try {
      await member.kick()
      message.reply('yes ping thing')
    } catch (err) {
      console.log(err)
      message.reply('There was an error')
    }
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!