Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } 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; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 29 | let cancelled = false; |
| 30 | let timedOut = false; |
| 31 | while (!timedOut && !cancelled) { |
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")), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +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( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 42 | interaction.options.getInteger("delete") ? interaction.options.getInteger("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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 47 | .setColor("Danger") |
| 48 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | .send(reason !== null); |
| 50 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 51 | if (confirmation.cancelled) timedOut = true; |
| 52 | else if (confirmation.success) cancelled = true; |
| 53 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 54 | else if (confirmation.components) notify = confirmation.components.notify.active; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 55 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 56 | if (timedOut) return; |
| 57 | let dmd = false; |
| 58 | let dm; |
| 59 | const config = await client.database.guilds.read(interaction.guild.id); |
| 60 | try { |
| 61 | if (notify) { |
| 62 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | embeds: [ |
| 64 | new EmojiEmbed() |
| 65 | .setEmoji("PUNISH.BAN.RED") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 66 | .setTitle("Banned") |
| 67 | .setDescription( |
| 68 | `You have been banned in ${interaction.guild.name}` + (reason ? ` for:\n> ${reason}` : ".") |
| 69 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | .setStatus("Danger") |
| 71 | ], |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 72 | components: [ |
| 73 | new MessageActionRow().addComponents( |
| 74 | config.moderation.ban.text |
| 75 | ? [ |
| 76 | new MessageButton() |
| 77 | .setStyle("LINK") |
| 78 | .setLabel(config.moderation.ban.text) |
| 79 | .setURL(config.moderation.ban.link) |
| 80 | ] |
| 81 | : [] |
| 82 | ) |
| 83 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | }); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 85 | dmd = true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 86 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 87 | } catch { |
| 88 | dmd = false; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 89 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 90 | try { |
| 91 | const member = interaction.options.getMember("user") as GuildMember; |
| 92 | member.ban({ |
| 93 | days: Number(interaction.options.getNumber("delete") ?? 0), |
| 94 | reason: reason ?? "No reason provided" |
| 95 | }); |
| 96 | await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason); |
| 97 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 98 | const data = { |
| 99 | meta: { |
| 100 | type: "memberBan", |
| 101 | displayName: "Member Banned", |
| 102 | calculateType: "guildMemberPunish", |
| 103 | color: NucleusColors.red, |
| 104 | emoji: "PUNISH.BAN.RED", |
| 105 | timestamp: new Date().getTime() |
| 106 | }, |
| 107 | list: { |
| 108 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 109 | name: entry(member.user.id, renderUser(member.user)), |
| 110 | banned: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 111 | bannedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 112 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
| 113 | accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)), |
| 114 | serverMemberCount: interaction.guild.memberCount |
| 115 | }, |
| 116 | hidden: { |
| 117 | guild: interaction.guild.id |
| 118 | } |
| 119 | }; |
| 120 | log(data); |
| 121 | } catch { |
| 122 | await interaction.editReply({ |
| 123 | embeds: [ |
| 124 | new EmojiEmbed() |
| 125 | .setEmoji("PUNISH.BAN.RED") |
| 126 | .setTitle("Ban") |
| 127 | .setDescription("Something went wrong and the user was not banned") |
| 128 | .setStatus("Danger") |
| 129 | ], |
| 130 | components: [] |
| 131 | }); |
| 132 | if (dmd) await dm.delete(); |
| 133 | return; |
| 134 | } |
| 135 | const failed = !dmd && notify; |
| 136 | await interaction.editReply({ |
| 137 | embeds: [ |
| 138 | new EmojiEmbed() |
| 139 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 140 | .setTitle("Ban") |
| 141 | .setDescription("The member was banned" + (failed ? ", but could not be notified" : "")) |
| 142 | .setStatus(failed ? "Warning" : "Success") |
| 143 | ], |
| 144 | components: [] |
| 145 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 146 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 147 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 148 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | const member = interaction.member as GuildMember; |
| 150 | const me = interaction.guild.me!; |
| 151 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 152 | if (member === null || me === null || apply === null) throw new Error("That member is not in the server"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | const memberPos = member.roles ? member.roles.highest.position : 0; |
| 154 | const mePos = me.roles ? me.roles.highest.position : 0; |
| 155 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 156 | // Do not allow banning the owner |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 157 | 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] | 158 | // Check if Nucleus can ban the member |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 159 | 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] | 160 | // Check if Nucleus has permission to ban |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 161 | 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] | 162 | // Do not allow banning Nucleus |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 163 | 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] | 164 | // Allow the owner to ban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 166 | // Check if the user has ban_members permission |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 167 | 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] | 168 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 169 | 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] | 170 | // Allow ban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 171 | return true; |
| 172 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 173 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 174 | export { command, callback, check }; |