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") |
| 41 | .setTitle("Kick") |
| 42 | .setDescription(`You have been kicked from ${interaction.guild.name}` + |
| 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 { |
| 51 | (interaction.options.getMember("user") as GuildMember).ban({ |
| 52 | days: Number(interaction.options.getInteger("delete") ?? 0), |
| 53 | reason: interaction.options.getString("reason") |
| 54 | }) // TODO: unban here |
| 55 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
| 56 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 57 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 58 | .setTitle(`Softban`) |
| 59 | .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : "")) |
| 60 | .setStatus(failed ? "Warning" : "Success") |
| 61 | ], components: []}) |
| 62 | } catch { |
| 63 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 64 | .setEmoji("PUNISH.BAN.RED") |
| 65 | .setTitle(`Softban`) |
| 66 | .setDescription("Something went wrong and the user was not softbanned") |
| 67 | .setStatus("Danger") |
| 68 | ], components: []}) |
| 69 | } |
| 70 | } else { |
| 71 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 72 | .setEmoji("PUNISH.BAN.GREEN") |
| 73 | .setTitle(`Softban`) |
| 74 | .setDescription("No changes were made") |
| 75 | .setStatus("Success") |
| 76 | ], components: []}) |
| 77 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 81 | // Check if Nucleus can ban the member |
| 82 | if (! (interaction.guild.me.roles.highest.position > (interaction.member as GuildMember).roles.highest.position)) throw "I do not have a role higher than that member" |
| 83 | // Check if Nucleus has permission to ban |
| 84 | if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission"; |
| 85 | // Do not allow softbanning Nucleus |
| 86 | if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot softban myself" |
| 87 | // Allow the owner to ban anyone |
| 88 | if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true |
| 89 | // Check if the user has ban_members permission |
| 90 | if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission"; |
| 91 | // Check if the user is below on the role list |
| 92 | if (! ((interaction.member as GuildMember).roles.highest.position > (interaction.options.getMember("user") as GuildMember).roles.highest.position)) throw "You do not have a role higher than that member" |
| 93 | // Allow softban |
| 94 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 95 | } |
| 96 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 97 | export { command, callback, check }; |