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