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

84
Views
How can I change this kick command fully to get a reply when you try to kick a member that is not in the server?

So I have this kick command I have coded and it is working perfectly fine. My question is, how can I change the command so that I get a reply saying as an example "You can't kick this user since they are not in this server" or so.

Here is my code:

const { Client, CommandInteraction, MessageEmbed } = require("discord.js");

module.exports = {
    name: "kick",
    description: "Kick a member",
    userPermission: ["KICK_MEMBERS"],
    options: [
        {
            name: "target",
            description: "target to kick",
            type: "USER",
            required: true
        },
        {
            name: "reason",
            description: "reason for this kick",
            type: "STRING",
            required: false,
        }
    ],
    /**
     * 
     * @param {Client} client 
     * @param {CommandInteraction} interaction 
     * @param {String} args 
     */
    execute: async(interaction, client, args) => {
        const { options, member, guild } = interaction;

        const target = options.getMember("target");
        const reason = options.getString("reason") || "No reason provided";

        if(!target.roles.highest.position >= member.roles.highest.position) return interaction.reply({content: "You can't take action on this user as their role is higher than yours!",
    });

    await target.send({embeds: [new MessageEmbed().setColor("AQUA").setTitle("๐Ÿšช Kicked").setAuthor(target.user.tag, target.user.avatarURL({dynamic: true, size: 512})).setDescription(`You have been kicked from **${guild.name}**!\nReason: \`${reason}\``).setFooter(`ID: ${target.user.id}`)]});

    target.kick(reason);

    interaction.reply({embeds: [new MessageEmbed().setColor("GREEN").setTitle("๐Ÿšช Kick").setAuthor(target.user.tag, target.user.avatarURL({dynamic: true, size: 512})).setDescription(`**${target.user.tag}** has successfully been kicked from **${guild.name}**!\n**Reason:** \`${reason}\``).setFooter(`ID: ${target.user.id}`)]});
    },
};
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Because this is a slash command and requires a USER here:

name: "target",
description: "target to kick",
type: "USER",
required: true

Then there can never exist a possibility of this command running without the user being in the guild. The command would error before it was sent.

Edit:

Also your line

const target = options.getMember("target");

Should be

const target = options.getUser("target");

You would need to get the member using the below:

const targetMember = guild.members.cache.find(member => member.id === target)

Then every line below that where target is used change to targetMember

Edit answering question in comments:

To kick/ban someone not in the guild

const targetMember = guild.members.cache.find(member => member.id === target)

if (targetMember = undefined) {
// code to kick/ban
} else {
// normal code
}
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!