|
| 1 | +import { |
| 2 | + AuditLogEvent, |
| 3 | + type ChatInputCommandInteraction, |
| 4 | + MessageFlags, |
| 5 | + SlashCommandBuilder, |
| 6 | +} from "discord.js"; |
| 7 | +import type { Command } from "../types/command"; |
| 8 | + |
| 9 | +const allowedRoleIds = [ |
| 10 | + "1268946626387378189", |
| 11 | + "1268540163068526632", |
| 12 | + "1357730279644594399", |
| 13 | + "1324344058138726481", |
| 14 | + "1349758583859970140", |
| 15 | + "917520262939938915", |
| 16 | +]; |
| 17 | + |
| 18 | +const BanReasonCommand: Command = { |
| 19 | + data: new SlashCommandBuilder() |
| 20 | + .setName("banreason") |
| 21 | + .setDescription("Show the ban reason for a user") |
| 22 | + .addUserOption((option) => |
| 23 | + option.setName("user").setDescription("User to check").setRequired(true), |
| 24 | + ), |
| 25 | + |
| 26 | + async execute(interaction: ChatInputCommandInteraction) { |
| 27 | + if (!interaction.inGuild() || !interaction.guild) { |
| 28 | + await interaction.reply({ |
| 29 | + content: "This command can only be used in a server.", |
| 30 | + flags: MessageFlags.Ephemeral, |
| 31 | + }); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const member = await interaction.guild.members.fetch(interaction.user.id); |
| 36 | + const hasPermission = allowedRoleIds.some((id) => |
| 37 | + member.roles.cache.has(id), |
| 38 | + ); |
| 39 | + |
| 40 | + if (!hasPermission) { |
| 41 | + await interaction.reply({ |
| 42 | + content: "You do not have permission to use this command.", |
| 43 | + flags: MessageFlags.Ephemeral, |
| 44 | + }); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const targetUser = interaction.options.getUser("user", true); |
| 49 | + await interaction.deferReply({ flags: MessageFlags.Ephemeral }); |
| 50 | + |
| 51 | + try { |
| 52 | + const ban = await interaction.guild.bans.fetch(targetUser.id); |
| 53 | + |
| 54 | + const auditLogs = await interaction.guild.fetchAuditLogs({ |
| 55 | + type: AuditLogEvent.MemberBanAdd, |
| 56 | + limit: 10, |
| 57 | + }); |
| 58 | + |
| 59 | + const auditEntry = auditLogs.entries.find( |
| 60 | + (entry) => entry.target?.id === targetUser.id, |
| 61 | + ); |
| 62 | + |
| 63 | + const reason = ban.reason ?? auditEntry?.reason ?? "No reason provided."; |
| 64 | + const bannedBy = auditEntry?.executor?.tag ?? "Unknown (not found)"; |
| 65 | + |
| 66 | + await interaction.editReply( |
| 67 | + `**Ban information for ${targetUser.tag}:**\n` + |
| 68 | + `• Reason: ${reason}\n` + |
| 69 | + `• Banned by: ${bannedBy}`, |
| 70 | + ); |
| 71 | + } catch (error) { |
| 72 | + console.error(error); |
| 73 | + await interaction.editReply( |
| 74 | + `${targetUser.tag} is not currently banned from this server.`, |
| 75 | + ); |
| 76 | + } |
| 77 | + }, |
| 78 | +}; |
| 79 | + |
| 80 | +export default BanReasonCommand; |
0 commit comments