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