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