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