Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { LinkWarningFooter } from "../../utils/defaults.js"; |
| 2 | import { |
| 3 | CommandInteraction, |
| 4 | GuildMember, |
| 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
| 7 | ButtonStyle, |
| 8 | SlashCommandSubcommandBuilder |
| 9 | } from "discord.js"; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 10 | // @ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | import humanizeDuration from "humanize-duration"; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 12 | import type Discord from "discord.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 14 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 15 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 16 | import client from "../../utils/client.js"; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 17 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | |
| 19 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 20 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | .setName("kick") |
| 22 | .setDescription("Kicks a user from the server") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 23 | .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 24 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 25 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 26 | if (!interaction.guild) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | const { renderUser } = client.logger; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 28 | // TODO:[Modals] Replace this with a modal |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 29 | let reason: string | null = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 30 | let notify = true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | let confirmation; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 32 | let timedOut = false; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 33 | let success = false; |
| 34 | do { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 35 | confirmation = await new confirmationMessage(interaction) |
| 36 | .setEmoji("PUNISH.KICK.RED") |
| 37 | .setTitle("Kick") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | .setDescription( |
| 39 | keyValueList({ |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 40 | user: renderUser(interaction.options.getUser("user")!), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 41 | reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*" |
| 42 | }) + `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] | 43 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 44 | .setColor("Danger") |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 45 | .addCustomBoolean( |
| 46 | "notify", |
| 47 | "Notify user", |
| 48 | false, |
| 49 | null, |
| 50 | "The user will be sent a DM", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 51 | null, |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 52 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 53 | notify |
| 54 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 55 | .addReasonButton(reason ?? "") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 56 | .setFailedMessage("No changes were made", "Success", "PUNISH.KICK.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | .send(reason !== null); |
| 58 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 59 | if (confirmation.cancelled) timedOut = true; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 60 | else if (confirmation.success !== undefined) success = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 61 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 62 | else if (confirmation.components) { |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 63 | notify = confirmation.components["notify"]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 64 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 65 | } while (!timedOut && !success); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 66 | if (timedOut || !confirmation.success) return; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 67 | let dmSent = false; |
| 68 | let dmMessage; |
| 69 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 70 | try { |
| 71 | if (notify) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 72 | if (reason) { |
| 73 | reason = reason |
| 74 | .split("\n") |
| 75 | .map((line) => "> " + line) |
| 76 | .join("\n"); |
| 77 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 78 | const messageData: { |
| 79 | embeds: EmojiEmbed[]; |
| 80 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 81 | } = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | embeds: [ |
| 83 | new EmojiEmbed() |
| 84 | .setEmoji("PUNISH.KICK.RED") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 85 | .setTitle("Kicked") |
| 86 | .setDescription( |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 87 | `You have been kicked from ${interaction.guild.name}` + |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 88 | (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 89 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | .setStatus("Danger") |
| 91 | ], |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 92 | components: [] |
| 93 | }; |
| 94 | if (config.moderation.kick.text && config.moderation.kick.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 95 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 96 | messageData.components.push( |
| 97 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 98 | new ButtonBuilder() |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 99 | .setStyle(ButtonStyle.Link) |
| 100 | .setLabel(config.moderation.kick.text) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 101 | .setURL( |
| 102 | config.moderation.kick.link.replaceAll( |
| 103 | "{id}", |
| 104 | (interaction.options.getMember("user") as GuildMember).id |
| 105 | ) |
| 106 | ) |
| 107 | ) |
| 108 | ); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 109 | } |
| 110 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData); |
| 111 | dmSent = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 112 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 113 | } catch { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 114 | dmSent = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 115 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 116 | try { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 117 | (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided"); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 118 | const member = interaction.options.getMember("user") as GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 119 | 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] | 120 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 121 | const timeInServer = member.joinedTimestamp |
| 122 | ? entry( |
| 123 | (Date.now() - member.joinedTimestamp).toString(), |
| 124 | humanizeDuration(Date.now() - member.joinedTimestamp, { |
| 125 | round: true |
| 126 | }) |
| 127 | ) |
| 128 | : entry(null, "*Unknown*"); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 129 | const data = { |
| 130 | meta: { |
| 131 | type: "memberKick", |
| 132 | displayName: "Member Kicked", |
| 133 | calculateType: "guildMemberPunish", |
| 134 | color: NucleusColors.red, |
| 135 | emoji: "PUNISH.KICK.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 136 | timestamp: Date.now() |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 137 | }, |
| 138 | list: { |
| 139 | memberId: entry(member.id, `\`${member.id}\``), |
| 140 | name: entry(member.id, renderUser(member.user)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 141 | joined: undefined as unknown | typeof entry, |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 142 | kicked: entry(Date.now().toString(), renderDelta(Date.now())), |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 143 | kickedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 144 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 145 | timeInServer: timeInServer, |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 146 | serverMemberCount: member.guild.memberCount |
| 147 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 148 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 149 | end: |
| 150 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 151 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 152 | }, |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 153 | hidden: { |
| 154 | guild: member.guild.id |
| 155 | } |
| 156 | }; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 157 | if (member.joinedTimestamp) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 158 | data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp)); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 159 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 160 | 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] | 161 | log(data); |
| 162 | } catch { |
| 163 | await interaction.editReply({ |
| 164 | embeds: [ |
| 165 | new EmojiEmbed() |
| 166 | .setEmoji("PUNISH.KICK.RED") |
| 167 | .setTitle("Kick") |
| 168 | .setDescription("Something went wrong and the user was not kicked") |
| 169 | .setStatus("Danger") |
| 170 | ], |
| 171 | components: [] |
| 172 | }); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 173 | if (dmSent && dmMessage) await dmMessage.delete(); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 174 | return; |
| 175 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 176 | const failed = !dmSent && notify; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 177 | await interaction.editReply({ |
| 178 | embeds: [ |
| 179 | new EmojiEmbed() |
| 180 | .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`) |
| 181 | .setTitle("Kick") |
| 182 | .setDescription("The member was kicked" + (failed ? ", but could not be notified" : "")) |
| 183 | .setStatus(failed ? "Warning" : "Success") |
| 184 | ], |
| 185 | components: [] |
| 186 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 187 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 188 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 189 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 190 | if (!interaction.guild) return; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 191 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 192 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 193 | // Check if the user has kick_members permission |
| 194 | if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission"; |
| 195 | if (partial) return true; |
| 196 | |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 197 | const me = interaction.guild.members.me!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 198 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 199 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 200 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 201 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 202 | // Check if Nucleus has permission to kick |
| 203 | if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission"; |
| 204 | // Allow the owner to kick anyone |
| 205 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 206 | // Do not allow kicking the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 207 | if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 208 | // Check if Nucleus can kick the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 209 | if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 210 | // Do not allow kicking Nucleus |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 211 | if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 212 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 213 | if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 214 | // Allow kick |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 215 | return true; |
| 216 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 217 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 218 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 219 | export const metadata = { |
| 220 | longDescription: "Removes a member from the server. They will be able to rejoin if they have an invite link.", |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 221 | premiumOnly: true |
| 222 | }; |