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