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