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, |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 8 | SlashCommandSubcommandBuilder, |
| 9 | ButtonInteraction |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 10 | } from "discord.js"; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 11 | // @ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | import humanizeDuration from "humanize-duration"; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 13 | import type Discord from "discord.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 14 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 15 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 16 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 17 | import client from "../../utils/client.js"; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 18 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 19 | |
| 20 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 21 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | .setName("kick") |
| 23 | .setDescription("Kicks a user from the server") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 24 | .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 25 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 26 | const callback = async (interaction: CommandInteraction | ButtonInteraction, member?: GuildMember): Promise<void> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 27 | if (!interaction.guild) return; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 28 | if (!interaction.isButton()) { |
| 29 | member = interaction.options.getMember("user") as GuildMember; |
| 30 | } |
| 31 | if (!member) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | const { renderUser } = client.logger; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 33 | // TODO:[Modals] Replace this with a modal |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 34 | let reason: string | null = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 35 | let notify = true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | let confirmation; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 37 | let timedOut = false; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 38 | let success = false; |
| 39 | do { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 40 | confirmation = await new confirmationMessage(interaction) |
| 41 | .setEmoji("PUNISH.KICK.RED") |
| 42 | .setTitle("Kick") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 43 | .setDescription( |
| 44 | keyValueList({ |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 45 | user: renderUser(member.user), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 46 | reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*" |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 47 | }) + `Are you sure you want to kick <@!${member.id}>?` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 49 | .setColor("Danger") |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 50 | .addCustomBoolean( |
| 51 | "notify", |
| 52 | "Notify user", |
| 53 | false, |
| 54 | null, |
| 55 | "The user will be sent a DM", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 56 | null, |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 57 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 58 | notify |
| 59 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 60 | .addReasonButton(reason ?? "") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 61 | .setFailedMessage("No changes were made", "Success", "PUNISH.KICK.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | .send(reason !== null); |
| 63 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 64 | if (confirmation.cancelled) timedOut = true; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 65 | else if (confirmation.success !== undefined) success = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 66 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 67 | else if (confirmation.components) { |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 68 | notify = confirmation.components["notify"]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 69 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 70 | } while (!timedOut && !success); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 71 | if (timedOut || !confirmation.success) return; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 72 | let dmSent = false; |
| 73 | let dmMessage; |
| 74 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 75 | try { |
| 76 | if (notify) { |
PineaFan | a6e3793 | 2023-09-25 22:09:08 +0100 | [diff] [blame^] | 77 | let formattedReason: string | null = null; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 78 | if (reason) { |
PineaFan | a6e3793 | 2023-09-25 22:09:08 +0100 | [diff] [blame^] | 79 | formattedReason = reason |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 80 | .split("\n") |
| 81 | .map((line) => "> " + line) |
| 82 | .join("\n"); |
| 83 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 84 | const messageData: { |
| 85 | embeds: EmojiEmbed[]; |
| 86 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 87 | } = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | embeds: [ |
| 89 | new EmojiEmbed() |
| 90 | .setEmoji("PUNISH.KICK.RED") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 91 | .setTitle("Kicked") |
| 92 | .setDescription( |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 93 | `You have been kicked from ${interaction.guild.name}` + |
PineaFan | a6e3793 | 2023-09-25 22:09:08 +0100 | [diff] [blame^] | 94 | (formattedReason ? ` for:\n${formattedReason}` : ".\n*No reason was provided.*") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 95 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | .setStatus("Danger") |
| 97 | ], |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 98 | components: [] |
| 99 | }; |
| 100 | if (config.moderation.kick.text && config.moderation.kick.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 101 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 102 | messageData.components.push( |
| 103 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 104 | new ButtonBuilder() |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 105 | .setStyle(ButtonStyle.Link) |
| 106 | .setLabel(config.moderation.kick.text) |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 107 | .setURL(config.moderation.kick.link.replaceAll("{id}", member.id)) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 108 | ) |
| 109 | ); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 110 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 111 | dmMessage = await member.send(messageData); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 112 | dmSent = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 113 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 114 | } catch { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 115 | dmSent = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 116 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 117 | try { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 118 | await member.kick(reason || "No reason provided"); |
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 | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 161 | await log(data); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 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 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 189 | const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => { |
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!; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 198 | let apply: GuildMember; |
| 199 | if (interaction.isButton()) { |
| 200 | apply = target!; |
| 201 | } else { |
| 202 | apply = interaction.options.getMember("user") as GuildMember; |
| 203 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 204 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 205 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 206 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 207 | // Check if Nucleus has permission to kick |
| 208 | if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission"; |
| 209 | // Allow the owner to kick anyone |
| 210 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 211 | // Do not allow kicking the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 212 | 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] | 213 | // Check if Nucleus can kick the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 214 | 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] | 215 | // Do not allow kicking Nucleus |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 216 | if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 217 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 218 | 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] | 219 | // Allow kick |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 220 | return true; |
| 221 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 222 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 223 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 224 | export const metadata = { |
| 225 | 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] | 226 | premiumOnly: true |
| 227 | }; |