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