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