pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember } 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"; |
| 5 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 7 | |
| 8 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 9 | builder |
| 10 | .setName("ban") |
| 11 | .setDescription("Bans a user from the server") |
| 12 | .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true)) |
| 13 | .addStringOption(option => option.setName("reason").setDescription("The reason for the ban").setRequired(false)) |
| 14 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are banned | Default yes").setRequired(false) |
| 15 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 16 | ) |
| 17 | .addIntegerOption(option => option.setName("delete").setDescription("The days of messages to delete | Default 0").setMinValue(0).setMaxValue(7).setRequired(false)) |
| 18 | |
| 19 | const callback = async (interaction: CommandInteraction) => { |
| 20 | // TODO:[Modals] Replace this with a modal |
| 21 | if (await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 22 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 23 | .setTitle("Ban") |
| 24 | .setDescription(keyValueList({ |
| 25 | "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`, |
| 26 | "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}` |
| 27 | }) |
| 28 | + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n` |
| 29 | + `${interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0} day${interaction.options.getInteger("delete") === 1 || interaction.options.getInteger("delete") === null ? "s" : ""} of messages will be deleted\n\n` // TODO:[s addition] |
| 30 | + `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 31 | .setColor("Danger") |
| 32 | // pluralize("day", interaction.options.getInteger("delete")) |
| 33 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
| 34 | .send()) { |
| 35 | let dmd = false |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 36 | let dm; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 37 | try { |
| 38 | if (interaction.options.getString("notify") != "no") { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 39 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 40 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 41 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 42 | .setTitle("Banned") |
| 43 | .setDescription(`You have been banned in ${interaction.guild.name}` + |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame^] | 44 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : ".")) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 45 | .setStatus("Danger") |
| 46 | ] |
| 47 | }) |
| 48 | dmd = true |
| 49 | } |
| 50 | } catch {} |
| 51 | try { |
| 52 | (interaction.options.getMember("user") as GuildMember).ban({ |
| 53 | days: Number(interaction.options.getInteger("delete") ?? 0), |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 54 | reason: interaction.options.getString("reason") ?? "No reason provided" |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 55 | }) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 56 | } catch { |
| 57 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 58 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 59 | .setTitle(`Ban`) |
| 60 | .setDescription("Something went wrong and the user was not banned") |
| 61 | .setStatus("Danger") |
| 62 | ], components: []}) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 63 | if (dmd) await dm.delete() |
| 64 | return |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 65 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 66 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
| 67 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 68 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 69 | .setTitle(`Ban`) |
| 70 | .setDescription("The member was banned" + (failed ? ", but could not be notified" : "")) |
| 71 | .setStatus(failed ? "Warning" : "Success") |
| 72 | ], components: []}) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 73 | } else { |
| 74 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 75 | .setEmoji("PUNISH.BAN.GREEN") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 76 | .setTitle(`Ban`) |
| 77 | .setDescription("No changes were made") |
| 78 | .setStatus("Success") |
| 79 | ], components: []}) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 84 | let member = (interaction.member as GuildMember) |
| 85 | let me = (interaction.guild.me as GuildMember) |
| 86 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 87 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 88 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 89 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 90 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 91 | // Check if Nucleus can ban the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 92 | 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] | 93 | // Check if Nucleus has permission to ban |
| 94 | if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission"; |
| 95 | // Do not allow banning Nucleus |
| 96 | if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot ban myself" |
| 97 | // Allow the owner to ban anyone |
| 98 | if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true |
| 99 | // Check if the user has ban_members permission |
| 100 | if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission"; |
| 101 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 102 | 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] | 103 | // Allow ban |
| 104 | return true |
| 105 | } |
| 106 | |
| 107 | export { command, callback, check }; |