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 |
| 34 | try { |
| 35 | if (interaction.options.getString("notify") != "no") { |
| 36 | await (interaction.options.getMember("user") as GuildMember).send({ |
| 37 | embeds: [new EmojiEmbed() |
| 38 | .setEmoji("PUNISH.KICK.RED") |
| 39 | .setTitle("Kicked") |
| 40 | .setDescription(`You have been kicked in ${interaction.guild.name}` + |
| 41 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided.")) |
| 42 | .setStatus("Danger") |
| 43 | ] |
| 44 | }) |
| 45 | dmd = true |
| 46 | } |
| 47 | } catch {} |
| 48 | try { |
| 49 | (interaction.options.getMember("user") as GuildMember).kick(interaction.options.getString("reason") ?? "No reason provided.") |
| 50 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
| 51 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 52 | .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`) |
| 53 | .setTitle(`Kick`) |
| 54 | .setDescription("The member was kicked" + (failed ? ", but could not be notified" : "")) |
| 55 | .setStatus(failed ? "Warning" : "Success") |
| 56 | ], components: []}) |
| 57 | } catch { |
| 58 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 59 | .setEmoji("PUNISH.KICK.RED") |
| 60 | .setTitle(`Kick`) |
| 61 | .setDescription("Something went wrong and the user was not kicked") |
| 62 | .setStatus("Danger") |
| 63 | ], components: []}) |
| 64 | } |
| 65 | } else { |
| 66 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 67 | .setEmoji("PUNISH.KICK.GREEN") |
| 68 | .setTitle(`Kick`) |
| 69 | .setDescription("No changes were made") |
| 70 | .setStatus("Success") |
| 71 | ], components: []}) |
| 72 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 76 | // Check if Nucleus can kick the member |
| 77 | if (! (interaction.guild.me.roles.highest.position > (interaction.member as GuildMember).roles.highest.position)) throw "I do not have a role higher than that member" |
| 78 | // Check if Nucleus has permission to kick |
| 79 | if (! interaction.guild.me.permissions.has("KICK_MEMBERS")) throw "I do not have the `kick_members` permission"; |
| 80 | // Do not allow kicking Nucleus |
| 81 | if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot kick myself" |
| 82 | // Allow the owner to kick anyone |
| 83 | if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true |
| 84 | // Check if the user has kick_members permission |
| 85 | if (! (interaction.member as GuildMember).permissions.has("KICK_MEMBERS")) throw "You do not have the `kick_members` permission"; |
| 86 | // Check if the user is below on the role list |
| 87 | if (! ((interaction.member as GuildMember).roles.highest.position > (interaction.options.getMember("user") as GuildMember).roles.highest.position)) throw "You do not have a role higher than that member" |
| 88 | // Allow kick |
| 89 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 92 | export { command, callback, check }; |