ban.ts

1import {
2	ChatInputCommandInteraction,
3	PermissionFlagsBits,
4	SlashCommandBuilder,
5} from 'discord.js'
6import { Command } from '@/types/bot'
7
8export default {
9	data: new SlashCommandBuilder()
10		.setName('ban')
11		.setDescription('Ban a user from the server')
12		.addUserOption((option) =>
13			option
14				.setName('user')
15				.setDescription('The user to ban')
16				.setRequired(true),
17		)
18		.addStringOption((option) =>
19			option
20				.setName('reason')
21				.setDescription('Reason for the ban')
22				.setRequired(false),
23		)
24		.addNumberOption((option) =>
25			option
26				.setName('days')
27				.setDescription('Number of days of messages to delete')
28				.setMinValue(0)
29				.setMaxValue(7)
30				.setRequired(false),
31		)
32		.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers),
33
34	async execute(interaction: ChatInputCommandInteraction) {
35		const target = interaction.options.getUser('user', true)
36		const reason =
37			interaction.options.getString('reason') ?? 'No reason provided'
38		const days = interaction.options.getNumber('days') ?? 0
39
40		const member = interaction.guild?.members.cache.get(target.id)
41
42		if (!member) {
43			await interaction.reply({
44				content: 'User not found in this server!',
45				ephemeral: true,
46			})
47			return
48		}
49
50		if (!member.bannable) {
51			await interaction.reply({
52				content: 'I cannot ban this user!',
53				ephemeral: true,
54			})
55			return
56		}
57
58		await member.ban({
59			deleteMessageDays: days,
60			reason: `${reason} - Banned by ${interaction.user.tag}`,
61		})
62		await interaction.reply(`🔨 **Banned ${target.tag}**\nReason: ${reason}`)
63	},
64} as Command
65