|
| 1 | +import { Command } from "djs-slash-helper"; |
| 2 | +import { |
| 3 | + ApplicationCommandOptionType, |
| 4 | + ApplicationCommandType, |
| 5 | +} from "discord.js"; |
| 6 | +import { config } from "../../Config.js"; |
| 7 | +import { actualMention, fakeMention } from "../../util/users.js"; |
| 8 | +import { createTempBanModAction } from "./tempBan.js"; |
| 9 | + |
| 10 | +export const SoftBanCommand: Command<ApplicationCommandType.ChatInput> = { |
| 11 | + name: "tempban", |
| 12 | + description: "Temp Ban a baaaaad boy", |
| 13 | + type: ApplicationCommandType.ChatInput, |
| 14 | + default_permission: false, |
| 15 | + options: [ |
| 16 | + { |
| 17 | + type: ApplicationCommandOptionType.User, |
| 18 | + name: "user", |
| 19 | + description: "The user to be banned", |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + { |
| 23 | + type: ApplicationCommandOptionType.String, |
| 24 | + name: "reason", |
| 25 | + description: "The reason why the user gets banned", |
| 26 | + }, |
| 27 | + { |
| 28 | + type: ApplicationCommandOptionType.Number, |
| 29 | + name: "ban_duration_days", |
| 30 | + description: "The duration of the ban in days", |
| 31 | + required: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + type: ApplicationCommandOptionType.Boolean, |
| 35 | + name: "delete_messages", |
| 36 | + description: "Should the users messages be deleted too? Defaults to True", |
| 37 | + }, |
| 38 | + ], |
| 39 | + |
| 40 | + handle: async (interaction) => { |
| 41 | + if ( |
| 42 | + !interaction.isChatInputCommand() || |
| 43 | + !interaction.inGuild() || |
| 44 | + interaction.guild === null |
| 45 | + ) |
| 46 | + return; |
| 47 | + try { |
| 48 | + await interaction.deferReply(); |
| 49 | + const user = interaction.options.getUser("user", true); |
| 50 | + const reason = interaction.options.getString("reason", false); |
| 51 | + const days = interaction.options.getNumber("ban_duration_days", true); |
| 52 | + const deleteMessages = |
| 53 | + interaction.options.getBoolean("delete_messages", false) ?? true; |
| 54 | + try { |
| 55 | + await user.send({ |
| 56 | + content: `You got temp-banned from ${interaction.guild.name} ${reason ? `with the reason: ${reason}` : ""} for ${days} Days`, |
| 57 | + }); |
| 58 | + } catch { |
| 59 | + /* empty */ |
| 60 | + } |
| 61 | + |
| 62 | + await interaction.guild.bans.create(user, { |
| 63 | + reason: reason ?? undefined, |
| 64 | + deleteMessageSeconds: deleteMessages ? 604800 : undefined, |
| 65 | + }); |
| 66 | + const unbanTimestamp = Math.floor( |
| 67 | + (Date.now() + days * 24 * 60 * 60 * 1000) / 1000, |
| 68 | + ); |
| 69 | + await createTempBanModAction( |
| 70 | + interaction.user, |
| 71 | + user, |
| 72 | + new Date(unbanTimestamp), |
| 73 | + reason, |
| 74 | + ); |
| 75 | + |
| 76 | + const auditLogChannel = await interaction.guild.channels.fetch( |
| 77 | + config.channels.auditLog, |
| 78 | + ); |
| 79 | + if (auditLogChannel?.isTextBased()) { |
| 80 | + await auditLogChannel.send({ |
| 81 | + content: `${actualMention(interaction.user)} temp banned ${fakeMention(user)} (${user.id})\nDelete Messages: ${deleteMessages}\nDays: ${days}\nUntil: <t:${ |
| 82 | + unbanTimestamp |
| 83 | + }:R>`, |
| 84 | + allowedMentions: { |
| 85 | + parse: [], |
| 86 | + }, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + const tempBanMessage = await interaction.followUp({ |
| 91 | + content: `Temp banned ${fakeMention(user)} (${user.id})`, |
| 92 | + }); |
| 93 | + |
| 94 | + setTimeout(() => tempBanMessage.delete().catch(() => null), 5000); |
| 95 | + } catch (e) { |
| 96 | + console.error("Failed to ban user: ", e); |
| 97 | + |
| 98 | + if (interaction.replied) { |
| 99 | + await interaction.editReply("Something went wrong!"); |
| 100 | + } else { |
| 101 | + await interaction.followUp("Something went wrong!"); |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | +}; |
0 commit comments