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