Steam
Steam
DIAdiscord.js - Imagine an app
Created by Steam on 9/20/2023 in #djs-questions
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."
);
7 replies