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

634
Views
JavaScript: Discord.js Error with !kick command

I have been upgrading all my previous commands to have a response with embeds instead of a normal response.

const Discord = require('discord.js')
const client = new Discord.Client({ws: {intents: Discord.Intents.ALL}});

exports.run = async (bot,message,args) => {
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0])
let reason = args.slice(1).join(' ');

let embed1 = new Discord.MessageEmbed()
    .setTitle(`You Do Not Have The Permissions For This Command!`)
    .setDescription(`<@!${message.member.user.id}> you are not an Admin`)
    .setColor('#eeff00')
    .setFooter(`Bot is maintained by  BLADEZ#7296`)
let embed2 = new Discord.MessageEmbed()
    .setTitle(`InValid User`)
    .setDescription(`<@!${message.member.user.id}> please give a valid User \n **Remember** !kick [@User] [reason]`)
    .setColor('#eeff00')
    .setFooter(`Bot is maintained by  BLADEZ#7296`)
let embed3 = new Discord.MessageEmbed()
    .setTitle(`Kick Unsuccessful`)
    .setDescription(`<@!${message.member.user.id}> you can not kick this person!`)
    .setColor('#eeff00')
    .setFooter(`Bot is maintained by  BLADEZ#7296`)
let embed4 = new Discord.MessageEmbed()
    .setTitle(`Kick Successful`)
    .setDescription(`**<@!${member.user.id}>** has been **kicked** for **${reason}** by **<@!${message.author.id}>**`)
    .setColor('#eeff00')
    .setFooter(`Bot is maintained by  BLADEZ#7296`)

    
    if(!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send(embed1)
    
    if(member) return message.channel.send(embed2)
   
    if(member.roles.highest.position > message.member.roles.highest.position) return message.channel.send (embed3)
    if(!reason) {reason = 'No reason given'}
    member.kick(reason)
    message.channel.send(embed4);
    console.log(`**${member.user.tag}** has been kicked for ${reason} by ${message.author.tag}`)
}

exports.help = {
    name: 'kick'
}

So this is the code I use which I haven't changed only added embeds.^

enter image description here

But I get this error ^. Kick still works just get an error if I don't include a user after !kick.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You try to use member.user.id, but if there is no member mentioned, member will be undefined.

You don't actually need to create all these embeds if you will only use one of them. Create them inside the if statements, when you're sure the values are available.

It's enough to only declare the embed and just update it.

exports.run = async (bot, message, args) => {
  let member =
    message.mentions.members.first() ||
    message.guild.members.cache.get(args[0]);
  let reason = args.slice(1).join(' ');

  let embed = new Discord.MessageEmbed()
    .setColor('#eeff00')
    .setFooter(`Bot is maintained by  BLADEZ#7296`);

  if (!message.member.hasPermission('MANAGE_ROLES')) {
    embed
      .setTitle(`You Do Not Have The Permissions For This Command!`)
      .setDescription(`<@!${message.member.user.id}> you are not an Admin`);

    return message.channel.send(embed);
  }

  // are you sure you want to check if member exists?
  // if (member) should be if (!member)
  if (!member) {
    embed
      .setTitle(`InValid User`)
      .setDescription(
        `<@!${message.member.user.id}> please give a valid User \n **Remember** !kick [@User] [reason]`,
      );
    return message.channel.send(embed);
  }

  if (member.roles.highest.position > message.member.roles.highest.position) {
    embed
      .setTitle(`Kick Unsuccessful`)
      .setDescription(
        `<@!${message.member.user.id}> you can not kick this person!`,
      );
    return message.channel.send(embed);
  }

  embed
    .setTitle(`Kick Successful`)
    .setDescription(
      `**<@!${member.user.id}>** has been **kicked** for **${reason}** by **<@!${message.author.id}>**`,
    );

  if (!reason) {
    reason = 'No reason given';
  }

  member.kick(reason);
  message.channel.send(embed);
  console.log(
    `**${member.user.tag}** has been kicked for ${reason} by ${message.author.tag}`,
  );
};

It seems to be working as expected:

enter image description here

over 4 years ago · Santiago Trujillo 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!