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