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