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