pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { 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") |
| 13 | .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true)) |
| 14 | .addNumberOption(option => option.setName("delete").setDescription("The days of messages to delete | Default: 0").setMinValue(0).setMaxValue(7).setRequired(false)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 15 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 16 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | const { renderUser } = client.logger; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | // TODO:[Modals] Replace this with a modal |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 20 | let notify = true; |
| 21 | let confirmation; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 22 | while (true) { |
| 23 | confirmation = await new confirmationMessage(interaction) |
| 24 | .setEmoji("PUNISH.BAN.RED") |
| 25 | .setTitle("Ban") |
| 26 | .setDescription(keyValueList({ |
| 27 | "user": renderUser(interaction.options.getUser("user")), |
| 28 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 29 | }) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | + `The user **will${notify ? "" : " not"}** be notified\n` |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 31 | + `${addPlurals(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n` |
| 32 | + `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 33 | .setColor("Danger") |
| 34 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | .send(reason !== null); |
| 36 | reason = reason ?? ""; |
| 37 | if (confirmation.cancelled) return; |
| 38 | if (confirmation.success) break; |
| 39 | if (confirmation.newReason) reason = confirmation.newReason; |
| 40 | if (confirmation.components) notify = confirmation.components.notify.active; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 41 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 42 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | let dmd = false; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 44 | let dm; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 46 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 47 | if (notify) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 48 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 49 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 50 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 51 | .setTitle("Banned") |
| 52 | .setDescription(`You have been banned in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 53 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 54 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 55 | ], |
| 56 | components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton() |
| 57 | .setStyle("LINK") |
| 58 | .setLabel(config.moderation.ban.text) |
| 59 | .setURL(config.moderation.ban.link) |
| 60 | ] : [])] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | }); |
| 62 | dmd = true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 63 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | } catch { dmd = false; } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 65 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | const member = (interaction.options.getMember("user") as GuildMember); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 67 | member.ban({ |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 68 | days: Number(interaction.options.getNumber("delete") ?? 0), |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 69 | reason: reason ?? "No reason provided" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | }); |
| 71 | await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason); |
| 72 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 73 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 74 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | type: "memberBan", |
| 76 | displayName: "Member Banned", |
| 77 | calculateType: "guildMemberPunish", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 78 | color: NucleusColors.red, |
| 79 | emoji: "PUNISH.BAN.RED", |
| 80 | timestamp: new Date().getTime() |
| 81 | }, |
| 82 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 83 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 84 | name: entry(member.user.id, renderUser(member.user)), |
| 85 | banned: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 86 | bannedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 87 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
| 88 | accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | serverMemberCount: interaction.guild.memberCount |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 90 | }, |
| 91 | hidden: { |
| 92 | guild: interaction.guild.id |
| 93 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | log(data); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 96 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 97 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 98 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | .setTitle("Ban") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 100 | .setDescription("Something went wrong and the user was not banned") |
| 101 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | ], components: []}); |
| 103 | if (dmd) await dm.delete(); |
| 104 | return; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 105 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 106 | const failed = (dmd === false && notify); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 107 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 108 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | .setTitle("Ban") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 110 | .setDescription("The member was banned" + (failed ? ", but could not be notified" : "")) |
| 111 | .setStatus(failed ? "Warning" : "Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | ], components: []}); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 113 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 114 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 115 | .setEmoji("PUNISH.BAN.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 116 | .setTitle("Ban") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 117 | .setDescription("No changes were made") |
| 118 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 119 | ], components: []}); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 120 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 122 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 123 | const check = (interaction: CommandInteraction) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | const member = (interaction.member as GuildMember); |
| 125 | const me = (interaction.guild.me as GuildMember); |
| 126 | const apply = (interaction.options.getMember("user") as GuildMember); |
| 127 | if (member === null || me === null || apply === null) throw "That member is not in the server"; |
| 128 | const memberPos = member.roles ? member.roles.highest.position : 0; |
| 129 | const mePos = me.roles ? me.roles.highest.position : 0; |
| 130 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 131 | // Do not allow banning the owner |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 132 | if (member.id === interaction.guild.ownerId) throw "You cannot ban the owner of the server"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 133 | // Check if Nucleus can ban the member |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 134 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 135 | // Check if Nucleus has permission to ban |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 136 | if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 137 | // Do not allow banning Nucleus |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | if (member.id === interaction.guild.me.id) throw "I cannot ban myself"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 139 | // Allow the owner to ban anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 140 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 141 | // Check if the user has ban_members permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 142 | if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 143 | // Check if the user is below on the role list |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 145 | // Allow ban |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 146 | return true; |
| 147 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 148 | |
| 149 | export { command, callback, check }; |