Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import Discord, { |
| 2 | CommandInteraction, |
| 3 | GuildMember, |
| 4 | ActionRowBuilder, |
| 5 | ButtonBuilder, |
| 6 | User, |
| 7 | ButtonStyle |
| 8 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 9 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 10 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 11 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 12 | import keyValueList from "../../utils/generateKeyValueList.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 13 | import addPlurals from "../../utils/plurals.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 14 | import client from "../../utils/client.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 15 | import { LinkWarningFooter } from "../../utils/defaults.js"; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 16 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 17 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 19 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | .setName("softban") |
| 21 | .setDescription("Kicks a user and deletes their messages") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 22 | .addUserOption((option) => option.setName("user").setDescription("The user to softban").setRequired(true)) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 23 | .addNumberOption((option) => |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | option |
| 25 | .setName("delete") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 26 | .setDescription("Delete this number of days of messages from the user | Default: 0") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | .setMinValue(0) |
| 28 | .setMaxValue(7) |
| 29 | .setRequired(false) |
| 30 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 31 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 32 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
| 33 | if (!interaction.guild) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | const { renderUser } = client.logger; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 35 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 36 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 37 | let notify = true; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 38 | let confirmation; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 39 | let chosen = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 40 | let timedOut = false; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 41 | do { |
| 42 | 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({ |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 47 | user: renderUser(interaction.options.getUser("user")!), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | }) + |
| 50 | `The user **will${notify ? "" : " not"}** be notified\n` + |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 51 | `${addPlurals( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 52 | (interaction.options.get("delete")?.value as number | null) ?? 0, |
| 53 | "day" |
| 54 | )} of messages will be deleted\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 55 | `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] | 56 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 57 | .addCustomBoolean( |
| 58 | "notify", |
| 59 | "Notify user", |
| 60 | false, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 61 | undefined, |
| 62 | "The user will be sent a DM", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 63 | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 65 | notify |
| 66 | ) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 67 | .setColor("Danger") |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 68 | .addReasonButton(reason ?? "") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 69 | .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | .send(reason !== null); |
| 71 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 72 | if (confirmation.cancelled) timedOut = true; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 73 | else if (confirmation.success !== undefined) chosen = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 74 | else if (confirmation.newReason) reason = confirmation.newReason; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 75 | else if (confirmation.components) notify = confirmation.components["notify"]!.active; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 76 | } while (!timedOut && !chosen); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 77 | if (timedOut || !confirmation.success) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 78 | reason = reason.length ? reason : null; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 79 | let dmSent = false; |
| 80 | let dmMessage; |
| 81 | const config = await client.database.guilds.read(interaction.guild.id); |
| 82 | try { |
| 83 | if (notify) { |
PineaFan | a6e3793 | 2023-09-25 22:09:08 +0100 | [diff] [blame^] | 84 | let formattedReason: string | null = null; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 85 | if (reason) { |
PineaFan | a6e3793 | 2023-09-25 22:09:08 +0100 | [diff] [blame^] | 86 | formattedReason = reason |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 87 | .split("\n") |
| 88 | .map((line) => "> " + line) |
| 89 | .join("\n"); |
| 90 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 91 | const messageData: { |
| 92 | embeds: EmojiEmbed[]; |
| 93 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 94 | } = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | embeds: [ |
| 96 | new EmojiEmbed() |
| 97 | .setEmoji("PUNISH.BAN.RED") |
| 98 | .setTitle("Softban") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 99 | .setDescription( |
| 100 | `You have been softbanned from ${interaction.guild.name}` + |
PineaFan | a6e3793 | 2023-09-25 22:09:08 +0100 | [diff] [blame^] | 101 | (formattedReason ? ` for:\n${formattedReason}` : ".\n*No reason was provided.*") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 102 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | .setStatus("Danger") |
| 104 | ], |
| 105 | components: [] |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 106 | }; |
| 107 | if (config.moderation.softban.text && config.moderation.softban.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 108 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 109 | messageData.components.push( |
| 110 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 111 | new ButtonBuilder() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 112 | .setStyle(ButtonStyle.Link) |
| 113 | .setLabel(config.moderation.softban.text) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 114 | .setURL( |
| 115 | config.moderation.softban.link.replaceAll( |
| 116 | "{id}", |
| 117 | (interaction.options.getMember("user") as GuildMember).id |
| 118 | ) |
| 119 | ) |
| 120 | ) |
| 121 | ); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 122 | } |
| 123 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData); |
| 124 | dmSent = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 125 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 126 | } catch { |
| 127 | dmSent = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 128 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 129 | try { |
| 130 | const member = interaction.options.getMember("user") as GuildMember; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 131 | const days: number = (interaction.options.get("delete")?.value as number | null) ?? 0; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 132 | await member.ban({ |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 133 | deleteMessageSeconds: days * 24 * 60 * 60, |
| 134 | reason: reason ?? "*No reason provided*" |
| 135 | }); |
| 136 | await interaction.guild.members.unban(member, "Softban"); |
| 137 | await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason); |
| 138 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 139 | const data = { |
| 140 | meta: { |
| 141 | type: "memberSoftban", |
| 142 | displayName: "Member Softbanned", |
| 143 | calculateType: "guildMemberPunish", |
| 144 | color: NucleusColors.yellow, |
| 145 | emoji: "PUNISH.BAN.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 146 | timestamp: Date.now() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 147 | }, |
| 148 | list: { |
| 149 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 150 | name: entry(member.user.id, renderUser(member.user)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 151 | softbanned: entry(Date.now().toString(), renderDelta(Date.now())), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 152 | softbannedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 153 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
| 154 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
| 155 | serverMemberCount: interaction.guild.memberCount |
| 156 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 157 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 158 | end: |
| 159 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 160 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 161 | }, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 162 | hidden: { |
| 163 | guild: interaction.guild.id |
| 164 | } |
| 165 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 166 | await log(data); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 167 | } catch { |
| 168 | await interaction.editReply({ |
| 169 | embeds: [ |
| 170 | new EmojiEmbed() |
| 171 | .setEmoji("PUNISH.BAN.RED") |
| 172 | .setTitle("Softban") |
| 173 | .setDescription("Something went wrong and the user was not softbanned") |
| 174 | .setStatus("Danger") |
| 175 | ], |
| 176 | components: [] |
| 177 | }); |
| 178 | if (dmSent && dmMessage) await dmMessage.delete(); |
| 179 | return; |
| 180 | } |
| 181 | const failed = !dmSent && notify; |
| 182 | await interaction.editReply({ |
| 183 | embeds: [ |
| 184 | new EmojiEmbed() |
| 185 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 186 | .setTitle("Softban") |
| 187 | .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : "")) |
| 188 | .setStatus(failed ? "Warning" : "Success") |
| 189 | ], |
| 190 | components: [] |
| 191 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 192 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 193 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 194 | const check = async (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 195 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 196 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 197 | // Check if the user has ban_members permission |
| 198 | if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission"; |
| 199 | if (partial) return true; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 200 | const me = interaction.guild.members.me!; |
| 201 | let apply = interaction.options.getUser("user") as User | GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 202 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 203 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 204 | let applyPos = 0; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 205 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 206 | apply = (await interaction.guild.members.fetch(apply.id)) as GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 207 | applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
| 208 | } catch { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 209 | apply = apply as User; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 210 | } |
| 211 | // Do not allow banning the owner |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 212 | if (member.id === interaction.guild.ownerId) return "You cannot softban the owner of the server"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 213 | // Check if Nucleus can ban the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 214 | if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 215 | // Check if Nucleus has permission to ban |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 216 | if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 217 | // Do not allow banning Nucleus |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 218 | if (member.id === me.id) return "I cannot softban myself"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 219 | // Allow the owner to ban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 220 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 221 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 222 | if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 223 | // Allow ban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 224 | return true; |
| 225 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 226 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 227 | export { command, callback, check }; |