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