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