How to make a ban command slash command

I have gone to the guide, and I can't figure it out. This is my code, and when I run it, it won't show the command when i try to do it.
module.exports = {
data: new SlashCommandBuilder()
.setName("ban")
.setDescription("Ban a member from the server")
.addUserOption((option) =>
option
.setName("user")
.setDescription("The user to ban")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("reason")
.setDescription("Reason for banning the user")
.setRequired(false)
),
async execute(interaction) {
const userToBan = interaction.options.getUser("user");
const reason =
interaction.options.getString("reason") || "No reason provided";

if (!interaction.guild) {
await interaction.reply("This command can only be used in a server.");
return;
}

if (!interaction.member.permissions.has("BAN_MEMBERS")) {
await interaction.reply(
"You do not have permission to use this command."
);
return;
}

try {
await interaction.guild.members.ban(userToBan, { reason });
await interaction.reply(
`Successfully banned ${userToBan.tag} for reason: ${reason}`
);
} catch (error) {
console.error(error);
await interaction.reply(
"An error occurred while trying to ban the user."
);
module.exports = {
data: new SlashCommandBuilder()
.setName("ban")
.setDescription("Ban a member from the server")
.addUserOption((option) =>
option
.setName("user")
.setDescription("The user to ban")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("reason")
.setDescription("Reason for banning the user")
.setRequired(false)
),
async execute(interaction) {
const userToBan = interaction.options.getUser("user");
const reason =
interaction.options.getString("reason") || "No reason provided";

if (!interaction.guild) {
await interaction.reply("This command can only be used in a server.");
return;
}

if (!interaction.member.permissions.has("BAN_MEMBERS")) {
await interaction.reply(
"You do not have permission to use this command."
);
return;
}

try {
await interaction.guild.members.ban(userToBan, { reason });
await interaction.reply(
`Successfully banned ${userToBan.tag} for reason: ${reason}`
);
} catch (error) {
console.error(error);
await interaction.reply(
"An error occurred while trying to ban the user."
);
5 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
monbrey
monbrey15mo ago
You run the bot but the command doesnt show up? Have you deployed it?
Steam
SteamOP15mo ago
How do I do that? I am new to coding, so I am learning as I go.
d.js docs
d.js docs15mo ago
guide Creating Your Bot: Registering slash commands The command deployment script, to register your slash commands with Discord so they appear in the interface. read more Tag suggestion for @officialsteam: RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined - All SCREAMING_SNAKE_CASE enums have been changed to PascalCase - Intents: Intents.FLAGS.GUILD_MESSAGES -> GatewayIntentBits.GuildMessages - Permissions: Permissions.FLAGS.SEND_MESSAGES -> PermissionFlagsBits.SendMessages
ifml
ifml15mo ago
also
Want results from more Discord servers?
Add your server