pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 4 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 5 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
| 8 | import addPlural from "../../utils/plurals.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("softban") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | .setDescription("Kicks a user and deletes their messages") |
| 14 | .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true)) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 15 | .addIntegerOption(option => option.setName("delete").setDescription("The days of messages to delete | Default: 0").setMinValue(0).setMaxValue(7).setRequired(false)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 16 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 17 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 18 | const { renderUser } = client.logger |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 19 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 20 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 21 | let notify = true; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 22 | let confirmation; |
| 23 | while (true) { |
| 24 | let confirmation = await new confirmationMessage(interaction) |
| 25 | .setEmoji("PUNISH.BAN.RED") |
| 26 | .setTitle("Softban") |
| 27 | .setDescription(keyValueList({ |
| 28 | "user": renderUser(interaction.options.getUser("user")), |
| 29 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 30 | }) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 31 | + `The user **will${notify ? '' : ' not'}** be notified\n` |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 32 | + `${addPlural(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n` |
| 33 | + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 34 | .setColor("Danger") |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 35 | .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 36 | .addReasonButton(reason ?? "") |
| 37 | .send(reason !== null) |
| 38 | reason = reason ?? "" |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 39 | if (confirmation.cancelled) return |
| 40 | if (confirmation.success) break |
| 41 | if (confirmation.newReason) reason = confirmation.newReason |
| 42 | if (confirmation.components) { |
| 43 | notify = confirmation.components.notify.active |
| 44 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 45 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 46 | if (confirmation.success) { |
| 47 | let dmd = false; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 48 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 49 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 50 | if (notify) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 52 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 53 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 54 | .setTitle("Softbanned") |
| 55 | .setDescription(`You have been softbanned from ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 56 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 57 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 58 | ], |
| 59 | components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton() |
| 60 | .setStyle("LINK") |
| 61 | .setLabel(config.moderation.ban.text) |
| 62 | .setURL(config.moderation.ban.link) |
| 63 | ] : [])] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 64 | }) |
| 65 | dmd = true |
| 66 | } |
| 67 | } catch {} |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 68 | let member = (interaction.options.getMember("user") as GuildMember) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 69 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 70 | await member.ban({ |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 71 | days: Number(interaction.options.getInteger("delete") ?? 0), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 72 | reason: reason |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 73 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 74 | await interaction.guild.members.unban(member, "Softban"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 75 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 76 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 77 | .setEmoji("PUNISH.BAN.RED") |
| 78 | .setTitle(`Softban`) |
| 79 | .setDescription("Something went wrong and the user was not softbanned") |
| 80 | .setStatus("Danger") |
| 81 | ], components: []}) |
| 82 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 83 | try { await client.database.history.create("softban", interaction.guild.id, member.user, reason) } catch {} |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 84 | let failed = (dmd == false && notify) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 85 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 86 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 87 | .setTitle(`Softban`) |
| 88 | .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : "")) |
| 89 | .setStatus(failed ? "Warning" : "Success") |
| 90 | ], components: []}) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 91 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 92 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 93 | .setEmoji("PUNISH.BAN.GREEN") |
| 94 | .setTitle(`Softban`) |
| 95 | .setDescription("No changes were made") |
| 96 | .setStatus("Success") |
| 97 | ], components: []}) |
| 98 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 102 | let member = (interaction.member as GuildMember) |
| 103 | let me = (interaction.guild.me as GuildMember) |
| 104 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 105 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 106 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 107 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 108 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 109 | // Check if Nucleus can ban the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 110 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 111 | // Check if Nucleus has permission to ban |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 112 | if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the Ban members permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 113 | // Do not allow softbanning Nucleus |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 114 | if (member.id == me.id) throw "I cannot softban myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 115 | // Allow the owner to ban anyone |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 116 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 117 | // Check if the user has ban_members permission |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 118 | if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the Ban members permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 119 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 120 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 121 | // Allow softban |
| 122 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 125 | export { command, callback, check }; |