const { Client, Interaction, ApplicationCommandOptionType, PermissionFlagsBits, SlashCommandBuilder, } = require("discord.js"); async function handleBanCommand(interaction) { if (!interaction.member.permissions.has(PermissionFlagsBits.BanMembers)) { await interaction.reply({ content: "You do not have permission to ban members.", ephemeral: true, }); return; } const targetID = interaction.options.get("user").value; const reason = interaction.options.get("reason")?.value || "No reason provided"; await interaction.deferReply(); const targetUser = await interaction.guild.members.fetch(targetID); if (!targetUser) { await interaction.editReply("That user is not in the server"); return; } if (targetUser.id === interaction.guild.ownerId) { await interaction.editReply("I can't ban the server owner"); return; } if (targetUser.id === interaction.guild.members.me) { await interaction.editReply("I cant't ban myself"); return; } const targetUserRolePostion = targetUser.roles.highest.postion; // check the user highest role const requestUserRolePostion = interaction.member.roles.highest.postion; // check the user issuing the command is higher than the target const botRolePostion = interaction.guild.members.me.roles.highest.postion; // check the bot has permissions if (targetUserRolePostion >= requestUserRolePostion) { await interaction.editReply( "You can't ban someone that has the same/higher role than you." ); return; } if (targetUserRolePostion >= botRolePostion) { await interaction.editReply( "I can't ban that user because they have the same/higher role than me." ); return; } try { await targetUser.ban({ reason }); await interaction.editReply( `User ${targetUser} was banned. Reason: ${reason}` ); } catch (error) { console.error("There was an error banning the target: ", error); } } module.exports = { data: new SlashCommandBuilder() .setName("ban") .setDescription("Bans the specified user") .addUserOption((option) => option .setName("user") .setDescription("Mention the user to ban") .setRequired(true) ) .addStringOption((option) => option .setName("reason") .setDescription("Specify the reason for banning") .setRequired(false) ) .setDefaultMemberPermissions(PermissionFlagsBits.BanMembers), async execute(interaction) { try { handleBanCommand(interaction); } catch (error) { console.error("There was an error in banning: ", error); } }, };