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) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 84 | if (reason) { |
| 85 | reason = reason |
| 86 | .split("\n") |
| 87 | .map((line) => "> " + line) |
| 88 | .join("\n"); |
| 89 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 90 | const messageData: { |
| 91 | embeds: EmojiEmbed[]; |
| 92 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 93 | } = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 94 | embeds: [ |
| 95 | new EmojiEmbed() |
| 96 | .setEmoji("PUNISH.BAN.RED") |
| 97 | .setTitle("Softban") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 98 | .setDescription( |
| 99 | `You have been softbanned from ${interaction.guild.name}` + |
| 100 | (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*") |
| 101 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | .setStatus("Danger") |
| 103 | ], |
| 104 | components: [] |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 105 | }; |
| 106 | if (config.moderation.softban.text && config.moderation.softban.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 107 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 108 | messageData.components.push( |
| 109 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 110 | new ButtonBuilder() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 111 | .setStyle(ButtonStyle.Link) |
| 112 | .setLabel(config.moderation.softban.text) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 113 | .setURL( |
| 114 | config.moderation.softban.link.replaceAll( |
| 115 | "{id}", |
| 116 | (interaction.options.getMember("user") as GuildMember).id |
| 117 | ) |
| 118 | ) |
| 119 | ) |
| 120 | ); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 121 | } |
| 122 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData); |
| 123 | dmSent = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 124 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 125 | } catch { |
| 126 | dmSent = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 127 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 128 | try { |
| 129 | const member = interaction.options.getMember("user") as GuildMember; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 130 | const days: number = (interaction.options.get("delete")?.value as number | null) ?? 0; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 131 | await member.ban({ |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 132 | deleteMessageSeconds: days * 24 * 60 * 60, |
| 133 | reason: reason ?? "*No reason provided*" |
| 134 | }); |
| 135 | await interaction.guild.members.unban(member, "Softban"); |
| 136 | await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason); |
| 137 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 138 | const data = { |
| 139 | meta: { |
| 140 | type: "memberSoftban", |
| 141 | displayName: "Member Softbanned", |
| 142 | calculateType: "guildMemberPunish", |
| 143 | color: NucleusColors.yellow, |
| 144 | emoji: "PUNISH.BAN.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 145 | timestamp: Date.now() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 146 | }, |
| 147 | list: { |
| 148 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 149 | name: entry(member.user.id, renderUser(member.user)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 150 | softbanned: entry(Date.now().toString(), renderDelta(Date.now())), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 151 | softbannedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 152 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
| 153 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
| 154 | serverMemberCount: interaction.guild.memberCount |
| 155 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 156 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 157 | end: |
| 158 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 159 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 160 | }, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 161 | hidden: { |
| 162 | guild: interaction.guild.id |
| 163 | } |
| 164 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 165 | await log(data); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 166 | } catch { |
| 167 | await interaction.editReply({ |
| 168 | embeds: [ |
| 169 | new EmojiEmbed() |
| 170 | .setEmoji("PUNISH.BAN.RED") |
| 171 | .setTitle("Softban") |
| 172 | .setDescription("Something went wrong and the user was not softbanned") |
| 173 | .setStatus("Danger") |
| 174 | ], |
| 175 | components: [] |
| 176 | }); |
| 177 | if (dmSent && dmMessage) await dmMessage.delete(); |
| 178 | return; |
| 179 | } |
| 180 | const failed = !dmSent && notify; |
| 181 | await interaction.editReply({ |
| 182 | embeds: [ |
| 183 | new EmojiEmbed() |
| 184 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 185 | .setTitle("Softban") |
| 186 | .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : "")) |
| 187 | .setStatus(failed ? "Warning" : "Success") |
| 188 | ], |
| 189 | components: [] |
| 190 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 191 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 192 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 193 | const check = async (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 194 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 195 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 196 | // Check if the user has ban_members permission |
| 197 | if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission"; |
| 198 | if (partial) return true; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 199 | const me = interaction.guild.members.me!; |
| 200 | let apply = interaction.options.getUser("user") as User | GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 201 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 202 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 203 | let applyPos = 0; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 204 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 205 | apply = (await interaction.guild.members.fetch(apply.id)) as GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 206 | applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
| 207 | } catch { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 208 | apply = apply as User; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 209 | } |
| 210 | // Do not allow banning the owner |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 211 | 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] | 212 | // Check if Nucleus can ban the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 213 | 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] | 214 | // Check if Nucleus has permission to ban |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 215 | 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] | 216 | // Do not allow banning Nucleus |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 217 | if (member.id === me.id) return "I cannot softban myself"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 218 | // Allow the owner to ban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 219 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 220 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 221 | 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] | 222 | // Allow ban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 223 | return true; |
| 224 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 225 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 226 | export { command, callback, check }; |