pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 2 | import humanizeDuration from 'humanize-duration'; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 4 | import { WrappedCheck } from "jshaiku"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 5 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 7 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 8 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("kick") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | .setDescription("Kicks a user from the server") |
| 14 | .addUserOption(option => option.setName("user").setDescription("The user to kick").setRequired(true)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 15 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 16 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 17 | const { renderUser } = client.logger |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 18 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 20 | let notify = true; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 21 | let confirmation |
| 22 | while (true) { |
| 23 | confirmation = await new confirmationMessage(interaction) |
| 24 | .setEmoji("PUNISH.KICK.RED") |
| 25 | .setTitle("Kick") |
| 26 | .setDescription(keyValueList({ |
| 27 | "user": renderUser(interaction.options.getUser("user")), |
| 28 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 29 | }) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 30 | + `The user **will${notify ? '' : ' not'}** be notified\n\n` |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 31 | + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 32 | .setColor("Danger") |
| 33 | .addReasonButton(reason ?? "") |
| 34 | .send(reason !== null) |
| 35 | reason = reason ?? "" |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 36 | if (confirmation.cancelled) return |
| 37 | if (confirmation.success) break |
| 38 | if (confirmation.newReason) reason = confirmation.newReason |
| 39 | if (confirmation.components) { |
| 40 | notify = confirmation.components.notify.active |
| 41 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 42 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 43 | if (confirmation.success) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 44 | let dmd = false |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 45 | let dm; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 46 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 47 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 48 | if (notify) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 49 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 50 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | .setEmoji("PUNISH.KICK.RED") |
| 52 | .setTitle("Kicked") |
| 53 | .setDescription(`You have been kicked in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 54 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 55 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 56 | ], |
| 57 | components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton() |
| 58 | .setStyle("LINK") |
| 59 | .setLabel(config.moderation.kick.text) |
| 60 | .setURL(config.moderation.kick.link) |
| 61 | ] : [])] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 62 | }) |
| 63 | dmd = true |
| 64 | } |
| 65 | } catch {} |
| 66 | try { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 67 | (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 68 | let member = (interaction.options.getMember("user") as GuildMember) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 69 | try { await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason) } catch {} |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 70 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 71 | let data = { |
| 72 | meta: { |
| 73 | type: 'memberKick', |
| 74 | displayName: 'Member Kicked', |
| 75 | calculateType: 'guildMemberPunish', |
| 76 | color: NucleusColors.red, |
| 77 | emoji: "PUNISH.KICK.RED", |
| 78 | timestamp: new Date().getTime() |
| 79 | }, |
| 80 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 81 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 82 | name: entry(member.id, renderUser(member.user)), |
| 83 | joined: entry(member.joinedAt, renderDelta(member.joinedAt)), |
| 84 | kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 85 | kickedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 86 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
| 87 | timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedTimestamp, { round: true })), |
| 88 | accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)), |
| 89 | serverMemberCount: member.guild.memberCount, |
| 90 | }, |
| 91 | hidden: { |
| 92 | guild: member.guild.id |
| 93 | } |
| 94 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | log(data); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 96 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 97 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 98 | .setEmoji("PUNISH.KICK.RED") |
| 99 | .setTitle(`Kick`) |
| 100 | .setDescription("Something went wrong and the user was not kicked") |
| 101 | .setStatus("Danger") |
| 102 | ], components: []}) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 103 | if (dmd) await dm.delete() |
| 104 | return |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 105 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 106 | let failed = (dmd === false && notify) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 107 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 108 | .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`) |
| 109 | .setTitle(`Kick`) |
| 110 | .setDescription("The member was kicked" + (failed ? ", but could not be notified" : "")) |
| 111 | .setStatus(failed ? "Warning" : "Success") |
| 112 | ], components: []}) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 113 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 114 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 115 | .setEmoji("PUNISH.KICK.GREEN") |
| 116 | .setTitle(`Kick`) |
| 117 | .setDescription("No changes were made") |
| 118 | .setStatus("Success") |
| 119 | ], components: []}) |
| 120 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 124 | let member = (interaction.member as GuildMember) |
| 125 | let me = (interaction.guild.me as GuildMember) |
| 126 | let apply = (interaction.options.getMember("user") as GuildMember) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 127 | if (member === null || me === null || apply === null) throw "That member is not in the server" |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 128 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 129 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 130 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 131 | // Check if Nucleus can kick the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 132 | 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] | 133 | // Check if Nucleus has permission to kick |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 134 | if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the *Kick Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 135 | // Do not allow kicking Nucleus |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 136 | if (member.id === interaction.guild.me.id) throw "I cannot kick myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 137 | // Allow the owner to kick anyone |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 138 | if (member.id === interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 139 | // Check if the user has kick_members permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 140 | if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the *Kick Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 141 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 142 | 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] | 143 | // Allow kick |
| 144 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 145 | } |
| 146 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 147 | export { command, callback, check }; |