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 | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 5 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
| 8 | import addPlural 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 | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 21 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 22 | const { renderUser } = client.logger |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 23 | // TODO:[Modals] Replace this with a modal |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 24 | let confirmation = await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 25 | .setEmoji("PUNISH.BAN.RED") |
| 26 | .setTitle("Softban") |
| 27 | .setDescription(keyValueList({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 28 | "user": renderUser(interaction.options.getUser("user")), |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 29 | "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}` |
| 30 | }) |
| 31 | + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n` |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 32 | + `${addPlural(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] | 33 | + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 34 | .setColor("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 35 | .send() |
| 36 | if (confirmation.success) { |
| 37 | let dmd = false; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 38 | let config = await client.database.guilds.read(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 | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 42 | embeds: [new EmojiEmbed() |
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 {} |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 58 | let member = (interaction.options.getMember("user") as GuildMember) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 59 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 60 | await member.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 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 64 | await interaction.guild.members.unban(member, "Softban"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 65 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 66 | await interaction.editReply({embeds: [new EmojiEmbed() |
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 | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 73 | try { await client.database.history.create("softban", interaction.guild.id, member.user, interaction.options.getString("reason")) } catch {} |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 74 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 75 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 76 | .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`) |
| 77 | .setTitle(`Softban`) |
| 78 | .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : "")) |
| 79 | .setStatus(failed ? "Warning" : "Success") |
| 80 | ], components: []}) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 81 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 82 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 83 | .setEmoji("PUNISH.BAN.GREEN") |
| 84 | .setTitle(`Softban`) |
| 85 | .setDescription("No changes were made") |
| 86 | .setStatus("Success") |
| 87 | ], components: []}) |
| 88 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 92 | let member = (interaction.member as GuildMember) |
| 93 | let me = (interaction.guild.me as GuildMember) |
| 94 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 95 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 96 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 97 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 98 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 99 | // Check if Nucleus can ban the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 100 | 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] | 101 | // Check if Nucleus has permission to ban |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 102 | 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] | 103 | // Do not allow softbanning Nucleus |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 104 | if (member.id == me.id) throw "I cannot softban myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 105 | // Allow the owner to ban anyone |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 106 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 107 | // Check if the user has ban_members permission |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 108 | 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] | 109 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 110 | 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] | 111 | // Allow softban |
| 112 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 115 | export { command, callback, check }; |