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