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}`)]});
},
};
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
}