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