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