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) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 77 | if (reason) { |
| 78 | reason = reason |
| 79 | .split("\n") |
| 80 | .map((line) => "> " + line) |
| 81 | .join("\n"); |
| 82 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 83 | const messageData: { |
| 84 | embeds: EmojiEmbed[]; |
| 85 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 86 | } = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 87 | embeds: [ |
| 88 | new EmojiEmbed() |
| 89 | .setEmoji("PUNISH.KICK.RED") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 90 | .setTitle("Kicked") |
| 91 | .setDescription( |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 92 | `You have been kicked from ${interaction.guild.name}` + |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 93 | (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 94 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | .setStatus("Danger") |
| 96 | ], |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 97 | components: [] |
| 98 | }; |
| 99 | if (config.moderation.kick.text && config.moderation.kick.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 100 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 101 | messageData.components.push( |
| 102 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 103 | new ButtonBuilder() |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 104 | .setStyle(ButtonStyle.Link) |
| 105 | .setLabel(config.moderation.kick.text) |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 106 | .setURL(config.moderation.kick.link.replaceAll("{id}", member.id)) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 107 | ) |
| 108 | ); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 109 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 110 | dmMessage = await member.send(messageData); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 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 { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 117 | await member.kick(reason || "No reason provided"); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 118 | 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] | 119 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 120 | const timeInServer = member.joinedTimestamp |
| 121 | ? entry( |
| 122 | (Date.now() - member.joinedTimestamp).toString(), |
| 123 | humanizeDuration(Date.now() - member.joinedTimestamp, { |
| 124 | round: true |
| 125 | }) |
| 126 | ) |
| 127 | : entry(null, "*Unknown*"); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 128 | const data = { |
| 129 | meta: { |
| 130 | type: "memberKick", |
| 131 | displayName: "Member Kicked", |
| 132 | calculateType: "guildMemberPunish", |
| 133 | color: NucleusColors.red, |
| 134 | emoji: "PUNISH.KICK.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 135 | timestamp: Date.now() |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 136 | }, |
| 137 | list: { |
| 138 | memberId: entry(member.id, `\`${member.id}\``), |
| 139 | name: entry(member.id, renderUser(member.user)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 140 | joined: undefined as unknown | typeof entry, |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 141 | kicked: entry(Date.now().toString(), renderDelta(Date.now())), |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 142 | kickedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 143 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 144 | timeInServer: timeInServer, |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 145 | serverMemberCount: member.guild.memberCount |
| 146 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 147 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 148 | end: |
| 149 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 150 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 151 | }, |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 152 | hidden: { |
| 153 | guild: member.guild.id |
| 154 | } |
| 155 | }; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 156 | if (member.joinedTimestamp) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 157 | data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp)); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 158 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 159 | 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^] | 160 | await log(data); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 161 | } catch { |
| 162 | await interaction.editReply({ |
| 163 | embeds: [ |
| 164 | new EmojiEmbed() |
| 165 | .setEmoji("PUNISH.KICK.RED") |
| 166 | .setTitle("Kick") |
| 167 | .setDescription("Something went wrong and the user was not kicked") |
| 168 | .setStatus("Danger") |
| 169 | ], |
| 170 | components: [] |
| 171 | }); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 172 | if (dmSent && dmMessage) await dmMessage.delete(); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 173 | return; |
| 174 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 175 | const failed = !dmSent && notify; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 176 | await interaction.editReply({ |
| 177 | embeds: [ |
| 178 | new EmojiEmbed() |
| 179 | .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`) |
| 180 | .setTitle("Kick") |
| 181 | .setDescription("The member was kicked" + (failed ? ", but could not be notified" : "")) |
| 182 | .setStatus(failed ? "Warning" : "Success") |
| 183 | ], |
| 184 | components: [] |
| 185 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 186 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 187 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 188 | const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 189 | if (!interaction.guild) return; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 190 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 191 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 192 | // Check if the user has kick_members permission |
| 193 | if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission"; |
| 194 | if (partial) return true; |
| 195 | |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 196 | const me = interaction.guild.members.me!; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 197 | let apply: GuildMember; |
| 198 | if (interaction.isButton()) { |
| 199 | apply = target!; |
| 200 | } else { |
| 201 | apply = interaction.options.getMember("user") as GuildMember; |
| 202 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 203 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 204 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 205 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 206 | // Check if Nucleus has permission to kick |
| 207 | if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission"; |
| 208 | // Allow the owner to kick anyone |
| 209 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 210 | // Do not allow kicking the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 211 | 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] | 212 | // Check if Nucleus can kick the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 213 | 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] | 214 | // Do not allow kicking Nucleus |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 215 | if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 216 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 217 | 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] | 218 | // Allow kick |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 219 | return true; |
| 220 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 221 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 222 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 223 | export const metadata = { |
| 224 | 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] | 225 | premiumOnly: true |
| 226 | }; |