Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import Discord, { |
| 2 | CommandInteraction, |
| 3 | GuildMember, |
| 4 | ActionRowBuilder, |
| 5 | ButtonBuilder, |
| 6 | User, |
| 7 | ButtonStyle, |
| 8 | SlashCommandSubcommandBuilder |
| 9 | } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 10 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 11 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 12 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 13 | import addPlurals from "../../utils/plurals.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 14 | import client from "../../utils/client.js"; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 15 | import { LinkWarningFooter } from "../../utils/defaults.js"; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 16 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 17 | |
| 18 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 19 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | .setName("ban") |
| 21 | .setDescription("Bans a user from the server") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 22 | .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | .addNumberOption((option) => |
| 24 | option |
| 25 | .setName("delete") |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 26 | .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] | 27 | .setMinValue(0) |
| 28 | .setMaxValue(7) |
| 29 | .setRequired(false) |
| 30 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 31 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 32 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 33 | if (!interaction.guild) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | const { renderUser } = client.logger; |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 35 | // TODO:[Modals] Replace the command arguments 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 | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 39 | let chosen = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 40 | let timedOut = false; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 41 | do { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 42 | confirmation = await new confirmationMessage(interaction) |
| 43 | .setEmoji("PUNISH.BAN.RED") |
| 44 | .setTitle("Ban") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | .setDescription( |
| 46 | keyValueList({ |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 47 | user: renderUser(interaction.options.getUser("user")!), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | }) + |
| 50 | `The user **will${notify ? "" : " not"}** be notified\n` + |
| 51 | `${addPlurals( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 52 | (interaction.options.get("delete")?.value as number | null) ?? 0, |
| 53 | "day" |
| 54 | )} of messages will be deleted\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 55 | `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] | 56 | ) |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 57 | .addCustomBoolean( |
| 58 | "notify", |
| 59 | "Notify user", |
| 60 | false, |
| 61 | undefined, |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 62 | "The user will be sent a DM", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 63 | null, |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 64 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 65 | notify |
| 66 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 67 | .setColor("Danger") |
| 68 | .addReasonButton(reason ?? "") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 69 | .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | .send(reason !== null); |
| 71 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 72 | if (confirmation.cancelled) timedOut = true; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 73 | else if (confirmation.success !== undefined) chosen = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 74 | else if (confirmation.newReason) reason = confirmation.newReason; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 75 | else if (confirmation.components) notify = confirmation.components["notify"]!.active; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 76 | } while (!timedOut && !chosen); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 77 | if (timedOut || !confirmation.success) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 78 | reason = reason.length ? reason : null; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 79 | let dmSent = false; |
| 80 | let dmMessage; |
| 81 | const config = await client.database.guilds.read(interaction.guild.id); |
| 82 | try { |
| 83 | if (notify) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 84 | if (reason) { |
| 85 | reason = reason |
| 86 | .split("\n") |
| 87 | .map((line) => "> " + line) |
| 88 | .join("\n"); |
| 89 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 90 | const messageData: { |
| 91 | embeds: EmojiEmbed[]; |
| 92 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 93 | } = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 94 | embeds: [ |
| 95 | new EmojiEmbed() |
| 96 | .setEmoji("PUNISH.BAN.RED") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 97 | .setTitle("Banned") |
| 98 | .setDescription( |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 99 | `You have been banned from ${interaction.guild.name}` + |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 100 | (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*") |
| 101 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | .setStatus("Danger") |
| 103 | ], |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 104 | components: [] |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 105 | }; |
| 106 | if (config.moderation.ban.text && config.moderation.ban.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 107 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 108 | messageData.components.push( |
| 109 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 110 | new ButtonBuilder() |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 111 | .setStyle(ButtonStyle.Link) |
| 112 | .setLabel(config.moderation.ban.text) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 113 | .setURL( |
| 114 | config.moderation.ban.link.replaceAll( |
| 115 | "{id}", |
| 116 | (interaction.options.getMember("user") as GuildMember).id |
| 117 | ) |
| 118 | ) |
| 119 | ) |
| 120 | ); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 121 | } |
| 122 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData); |
| 123 | dmSent = true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 124 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 125 | } catch { |
| 126 | dmSent = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 127 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 128 | try { |
| 129 | const member = interaction.options.getMember("user") as GuildMember; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 130 | const days: number = (interaction.options.get("delete")?.value as number | null) ?? 0; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 131 | member.ban({ |
| 132 | deleteMessageSeconds: days * 24 * 60 * 60, |
| 133 | reason: reason ?? "*No reason provided*" |
| 134 | }); |
| 135 | await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason); |
| 136 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 137 | const data = { |
| 138 | meta: { |
| 139 | type: "memberBan", |
| 140 | displayName: "Member Banned", |
| 141 | calculateType: "guildMemberPunish", |
| 142 | color: NucleusColors.red, |
| 143 | emoji: "PUNISH.BAN.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 144 | timestamp: Date.now() |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 145 | }, |
| 146 | list: { |
| 147 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 148 | name: entry(member.user.id, renderUser(member.user)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 149 | banned: entry(Date.now().toString(), renderDelta(Date.now())), |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 150 | bannedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 151 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 152 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 153 | serverMemberCount: interaction.guild.memberCount |
| 154 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 155 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 156 | end: |
| 157 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 158 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 159 | }, |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 160 | hidden: { |
| 161 | guild: interaction.guild.id |
| 162 | } |
| 163 | }; |
| 164 | log(data); |
| 165 | } catch { |
| 166 | await interaction.editReply({ |
| 167 | embeds: [ |
| 168 | new EmojiEmbed() |
| 169 | .setEmoji("PUNISH.BAN.RED") |
| 170 | .setTitle("Ban") |
| 171 | .setDescription("Something went wrong and the user was not banned") |
| 172 | .setStatus("Danger") |
| 173 | ], |
| 174 | components: [] |
| 175 | }); |
| 176 | if (dmSent && dmMessage) await dmMessage.delete(); |
| 177 | return; |
| 178 | } |
| 179 | const failed = !dmSent && notify; |
| 180 | await interaction.editReply({ |
| 181 | embeds: [ |
| 182 | new EmojiEmbed() |
| 183 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 184 | .setTitle("Ban") |
| 185 | .setDescription("The member was banned" + (failed ? ", but could not be notified" : "")) |
| 186 | .setStatus(failed ? "Warning" : "Success") |
| 187 | ], |
| 188 | components: [] |
| 189 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 190 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 191 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 192 | const check = async (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 193 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 194 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 195 | // Check if the user has ban_members permission |
| 196 | if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 197 | if (partial) return true; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 198 | const me = interaction.guild.members.me!; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 199 | let apply = interaction.options.getUser("user") as User | GuildMember; |
| 200 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 201 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 202 | let applyPos = 0; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 203 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 204 | apply = (await interaction.guild.members.fetch(apply.id)) as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 205 | applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
| 206 | } catch { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 207 | apply = apply as User; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 208 | } |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 209 | // Do not allow banning the owner |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 210 | if (member.id === interaction.guild.ownerId) return "You cannot ban the owner of the server"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 211 | // Check if Nucleus can ban the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 212 | if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 213 | // Check if Nucleus has permission to ban |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 214 | if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 215 | // Do not allow banning Nucleus |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 216 | if (member.id === me.id) return "I cannot ban myself"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 217 | // Allow the owner to ban anyone |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 218 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 219 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 220 | if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 221 | // Allow ban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 222 | return true; |
| 223 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 224 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 225 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 226 | export const metadata = { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 227 | longDescription: |
| 228 | "Removes a member from the server - this will prevent them from rejoining until they are unbanned, and will delete a specified number of days of messages from them.", |
| 229 | premiumOnly: true |
| 230 | }; |