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 | 8b4b17f | 2022-02-27 20:42:52 +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 | 8b4b17f | 2022-02-27 20:42:52 +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("softban") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | .setDescription("Kicks a user and deletes their messages") |
| 14 | .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true)) |
| 15 | .addStringOption(option => option.setName("reason").setDescription("The reason for the softban").setRequired(false)) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 16 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are softbanned | Default yes").setRequired(false) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 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)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 20 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 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") |
| 25 | .setTitle("Softban") |
| 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 | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 32 | + `Are you sure you want to softban <@!${(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) { |
| 38 | let dmd = false; |
| 39 | let config = await readConfig(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 40 | try { |
| 41 | if (interaction.options.getString("notify") != "no") { |
| 42 | await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 43 | embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 44 | .setEmoji("PUNISH.BAN.RED") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 45 | .setTitle("Softbanned") |
| 46 | .setDescription(`You have been softbanned from ${interaction.guild.name}` + |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 47 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : ".")) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 48 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 49 | ], |
| 50 | components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton() |
| 51 | .setStyle("LINK") |
| 52 | .setLabel(config.moderation.ban.text) |
| 53 | .setURL(config.moderation.ban.link) |
| 54 | ] : [])] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 55 | }) |
| 56 | dmd = true |
| 57 | } |
| 58 | } catch {} |
| 59 | try { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 60 | await (interaction.options.getMember("user") as GuildMember).ban({ |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 61 | days: Number(interaction.options.getInteger("delete") ?? 0), |
| 62 | reason: interaction.options.getString("reason") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 63 | }); |
| 64 | await interaction.guild.members.unban(interaction.options.getMember("user") as GuildMember, "Softban"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 65 | } catch { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 66 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 67 | .setEmoji("PUNISH.BAN.RED") |
| 68 | .setTitle(`Softban`) |
| 69 | .setDescription("Something went wrong and the user was not softbanned") |
| 70 | .setStatus("Danger") |
| 71 | ], components: []}) |
| 72 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 73 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 74 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 75 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 76 | .setTitle(`Softban`) |
| 77 | .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : "")) |
| 78 | .setStatus(failed ? "Warning" : "Success") |
| 79 | ], components: []}) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 80 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 81 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 82 | .setEmoji("PUNISH.BAN.GREEN") |
| 83 | .setTitle(`Softban`) |
| 84 | .setDescription("No changes were made") |
| 85 | .setStatus("Success") |
| 86 | ], components: []}) |
| 87 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 91 | let member = (interaction.member as GuildMember) |
| 92 | let me = (interaction.guild.me as GuildMember) |
| 93 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 94 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 95 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 96 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 97 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 98 | // Check if Nucleus can ban the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 99 | 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] | 100 | // Check if Nucleus has permission to ban |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 101 | if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 102 | // Do not allow softbanning Nucleus |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 103 | if (member.id == me.id) throw "I cannot softban myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 104 | // Allow the owner to ban anyone |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame^] | 105 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 106 | // Check if the user has ban_members permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 107 | if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 108 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 109 | 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] | 110 | // Allow softban |
| 111 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 112 | } |
| 113 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 114 | export { command, callback, check }; |