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