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