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