const { Client, Intents, Collection } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.Guilds, Intents.FLAGS.GuildMembers, Intents.FLAGS.GuildBans, Intents.FLAGS.GuildMessages] });
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
// Register the ban slash command
await client.guilds.cache.get('YOUR_GUILD_ID').commands.create({
name: 'ban',
description: 'Bans a specified user from the server',
options: [
{
name: 'user',
type: 'USER',
description: 'The user to ban',
required: true,
},
{
name: 'reason',
type: 'STRING',
description: 'The reason for the ban',
required: false,
},
],
});
console.log('Ban slash command registered!');
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
if (commandName === 'ban') {
const user = options.getUser('user');
const reason = options.getString('reason') || 'No reason provided';
if (!interaction.guild.me.permissions.has('BAN_MEMBERS')) {
return await interaction.reply('I do not have the required permissions to ban members.');
}
if (!interaction.member.permissions.has('BAN_MEMBERS')) {
return await interaction.reply('You do not have the required permissions to ban members.');
}
const member = interaction.guild.members.cache.get(user.id);
if (!member.bannable) {
return await interaction.reply('I cannot ban this member.');
}
await member.ban({ reason });
await interaction.reply(`Successfully banned ${user.tag} for reason: ${reason}`);
}
});
client.login(token);