Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 1 | import { |
| 2 | CommandInteraction, |
| 3 | GuildMember, |
| 4 | MessageActionRow, |
| 5 | MessageButton |
| 6 | } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 7 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 8 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 9 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 10 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 11 | import client from "../../utils/client.js"; |
| 12 | import addPlural from "../../utils/plurals.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 13 | |
| 14 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 15 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | .setName("softban") |
| 17 | .setDescription("Kicks a user and deletes their messages") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | .addUserOption((option) => |
| 19 | option |
| 20 | .setName("user") |
| 21 | .setDescription("The user to softban") |
| 22 | .setRequired(true) |
| 23 | ) |
| 24 | .addIntegerOption((option) => |
| 25 | option |
| 26 | .setName("delete") |
| 27 | .setDescription("The days of messages to delete | Default: 0") |
| 28 | .setMinValue(0) |
| 29 | .setMaxValue(7) |
| 30 | .setRequired(false) |
| 31 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 32 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | const callback = async ( |
| 34 | interaction: CommandInteraction |
| 35 | ): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | const { renderUser } = client.logger; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 37 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 38 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 39 | let notify = true; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 40 | let confirmation; |
| 41 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 43 | .setEmoji("PUNISH.BAN.RED") |
| 44 | .setTitle("Softban") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | .setDescription( |
| 46 | keyValueList({ |
| 47 | user: renderUser(interaction.options.getUser("user")), |
| 48 | reason: reason |
| 49 | ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ") |
| 50 | : "*No reason provided*" |
| 51 | }) + |
| 52 | `The user **will${notify ? "" : " not"}** be notified\n` + |
| 53 | `${addPlural( |
| 54 | interaction.options.getInteger("delete") |
| 55 | ? interaction.options.getInteger("delete") |
| 56 | : 0, |
| 57 | "day" |
| 58 | )} of messages will be deleted\n\n` + |
| 59 | `Are you sure you want to softban <@!${ |
| 60 | (interaction.options.getMember("user") as GuildMember) |
| 61 | .id |
| 62 | }>?` |
| 63 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 64 | .setColor("Danger") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 65 | .addCustomBoolean( |
| 66 | "notify", |
| 67 | "Notify user", |
| 68 | false, |
| 69 | null, |
| 70 | null, |
| 71 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 72 | notify |
| 73 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 74 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | .send(reason !== null); |
| 76 | reason = reason ?? ""; |
| 77 | if (confirmation.cancelled) return; |
| 78 | if (confirmation.success) break; |
| 79 | if (confirmation.newReason) reason = confirmation.newReason; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 80 | if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 81 | notify = confirmation.components.notify.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 82 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 83 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 84 | if (confirmation.success) { |
| 85 | let dmd = false; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 86 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 87 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 88 | if (notify) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 89 | await ( |
| 90 | interaction.options.getMember("user") as GuildMember |
| 91 | ).send({ |
| 92 | embeds: [ |
| 93 | new EmojiEmbed() |
| 94 | .setEmoji("PUNISH.BAN.RED") |
| 95 | .setTitle("Softbanned") |
| 96 | .setDescription( |
| 97 | `You have been softbanned from ${interaction.guild.name}` + |
| 98 | (reason ? ` for:\n> ${reason}` : ".") |
| 99 | ) |
| 100 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 101 | ], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | components: [ |
| 103 | new MessageActionRow().addComponents( |
| 104 | config.moderation.ban.text |
| 105 | ? [ |
| 106 | new MessageButton() |
| 107 | .setStyle("LINK") |
| 108 | .setLabel(config.moderation.ban.text) |
| 109 | .setURL(config.moderation.ban.link) |
| 110 | ] |
| 111 | : [] |
| 112 | ) |
| 113 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 114 | }); |
| 115 | dmd = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 116 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | } catch { |
| 118 | dmd = false; |
| 119 | } |
| 120 | const member = interaction.options.getMember("user") as GuildMember; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 121 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 122 | await member.ban({ |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 123 | days: Number(interaction.options.getInteger("delete") ?? 0), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 124 | reason: reason |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 125 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 126 | await interaction.guild.members.unban(member, "Softban"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 127 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | await interaction.editReply({ |
| 129 | embeds: [ |
| 130 | new EmojiEmbed() |
| 131 | .setEmoji("PUNISH.BAN.RED") |
| 132 | .setTitle("Softban") |
| 133 | .setDescription( |
| 134 | "Something went wrong and the user was not softbanned" |
| 135 | ) |
| 136 | .setStatus("Danger") |
| 137 | ], |
| 138 | components: [] |
| 139 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 140 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | await client.database.history.create( |
| 142 | "softban", |
| 143 | interaction.guild.id, |
| 144 | member.user, |
| 145 | reason |
| 146 | ); |
| 147 | const failed = !dmd && notify; |
| 148 | await interaction.editReply({ |
| 149 | embeds: [ |
| 150 | new EmojiEmbed() |
| 151 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 152 | .setTitle("Softban") |
| 153 | .setDescription( |
| 154 | "The member was softbanned" + |
| 155 | (failed ? ", but could not be notified" : "") |
| 156 | ) |
| 157 | .setStatus(failed ? "Warning" : "Success") |
| 158 | ], |
| 159 | components: [] |
| 160 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 161 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 162 | await interaction.editReply({ |
| 163 | embeds: [ |
| 164 | new EmojiEmbed() |
| 165 | .setEmoji("PUNISH.BAN.GREEN") |
| 166 | .setTitle("Softban") |
| 167 | .setDescription("No changes were made") |
| 168 | .setStatus("Success") |
| 169 | ], |
| 170 | components: [] |
| 171 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 172 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 173 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 174 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 175 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 176 | const member = interaction.member as GuildMember; |
| 177 | const me = interaction.guild.me!; |
| 178 | const apply = interaction.options.getMember("user") as GuildMember; |
| 179 | if (member === null || me === null || apply === null) |
| 180 | throw "That member is not in the server"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 181 | const memberPos = member.roles ? member.roles.highest.position : 0; |
| 182 | const mePos = me.roles ? me.roles.highest.position : 0; |
| 183 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 184 | // Do not allow softbanning the owner |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 185 | if (member.id === interaction.guild.ownerId) |
| 186 | throw "You cannot softban the owner of the server"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 187 | // Check if Nucleus can ban the member |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 188 | if (!(mePos > applyPos)) |
| 189 | throw "I do not have a role higher than that member"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 190 | // Check if Nucleus has permission to ban |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 191 | if (!me.permissions.has("BAN_MEMBERS")) |
| 192 | throw "I do not have the *Ban Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 193 | // Do not allow softbanning Nucleus |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 194 | if (member.id === me.id) throw "I cannot softban myself"; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 195 | // Allow the owner to softban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 197 | // Check if the user has ban_members permission |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 198 | if (!member.permissions.has("BAN_MEMBERS")) |
| 199 | throw "You do not have the *Ban Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 200 | // Check if the user is below on the role list |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 201 | if (!(memberPos > applyPos)) |
| 202 | throw "You do not have a role higher than that member"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 203 | // Allow softban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 204 | return true; |
| 205 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 206 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 207 | export { command, callback, check }; |