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