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