PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 1 | import { LinkWarningFooter } from './../../utils/defaultEmbeds.js'; |
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", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 45 | null, |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 46 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 47 | notify |
| 48 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 49 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | .send(reason !== null); |
| 51 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 52 | if (confirmation.cancelled) timedOut = true; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 53 | else if (confirmation.success !== undefined) success = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 54 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 55 | else if (confirmation.components) { |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 56 | notify = confirmation.components["notify"]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 57 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 58 | } while (!timedOut && !success) |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 59 | if (timedOut) return; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 60 | if (!confirmation.success) { |
| 61 | await interaction.editReply({ |
| 62 | embeds: [ |
| 63 | new EmojiEmbed() |
| 64 | .setEmoji("PUNISH.KICK.GREEN") |
| 65 | .setTitle("Kick") |
| 66 | .setDescription("No changes were made") |
| 67 | .setStatus("Success") |
| 68 | ], |
| 69 | components: [] |
| 70 | }); |
| 71 | return; |
| 72 | } |
| 73 | let dmSent = false; |
| 74 | let dmMessage; |
| 75 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 76 | try { |
| 77 | if (notify) { |
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}` + |
| 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) { |
| 95 | messageData.embeds[0]!.setFooter(LinkWarningFooter) |
| 96 | messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>() |
| 97 | .addComponents(new ButtonBuilder() |
| 98 | .setStyle(ButtonStyle.Link) |
| 99 | .setLabel(config.moderation.kick.text) |
| 100 | .setURL(config.moderation.kick.link) |
| 101 | ) |
| 102 | ) |
| 103 | } |
| 104 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData); |
| 105 | dmSent = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 106 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 107 | } catch { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 108 | dmSent = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 109 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 110 | try { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 111 | (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided"); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 112 | const member = interaction.options.getMember("user") as GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 113 | 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] | 114 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 115 | const timeInServer = member.joinedTimestamp ? entry( |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 116 | (new Date().getTime() - member.joinedTimestamp).toString(), |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 117 | humanizeDuration(new Date().getTime() - member.joinedTimestamp, { |
| 118 | round: true |
| 119 | }) |
| 120 | ) : entry(null, "*Unknown*") |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 121 | const data = { |
| 122 | meta: { |
| 123 | type: "memberKick", |
| 124 | displayName: "Member Kicked", |
| 125 | calculateType: "guildMemberPunish", |
| 126 | color: NucleusColors.red, |
| 127 | emoji: "PUNISH.KICK.RED", |
| 128 | timestamp: new Date().getTime() |
| 129 | }, |
| 130 | list: { |
| 131 | memberId: entry(member.id, `\`${member.id}\``), |
| 132 | name: entry(member.id, renderUser(member.user)), |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 133 | joined: undefined as (unknown | typeof entry), |
| 134 | kicked: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())), |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 135 | kickedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 136 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 137 | timeInServer: timeInServer, |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 138 | serverMemberCount: member.guild.memberCount |
| 139 | }, |
| 140 | hidden: { |
| 141 | guild: member.guild.id |
| 142 | } |
| 143 | }; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 144 | if (member.joinedTimestamp) { |
| 145 | data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp)) |
| 146 | } |
| 147 | await client.database.history.create( |
| 148 | "kick", |
| 149 | interaction.guild.id, |
| 150 | member.user, |
| 151 | interaction.user, |
| 152 | reason |
| 153 | ) |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 154 | log(data); |
| 155 | } catch { |
| 156 | await interaction.editReply({ |
| 157 | embeds: [ |
| 158 | new EmojiEmbed() |
| 159 | .setEmoji("PUNISH.KICK.RED") |
| 160 | .setTitle("Kick") |
| 161 | .setDescription("Something went wrong and the user was not kicked") |
| 162 | .setStatus("Danger") |
| 163 | ], |
| 164 | components: [] |
| 165 | }); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 166 | if (dmSent && dmMessage) await dmMessage.delete(); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 167 | return; |
| 168 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 169 | const failed = !dmSent && notify; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 170 | await interaction.editReply({ |
| 171 | embeds: [ |
| 172 | new EmojiEmbed() |
| 173 | .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`) |
| 174 | .setTitle("Kick") |
| 175 | .setDescription("The member was kicked" + (failed ? ", but could not be notified" : "")) |
| 176 | .setStatus(failed ? "Warning" : "Success") |
| 177 | ], |
| 178 | components: [] |
| 179 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 180 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 181 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 182 | const check = (interaction: CommandInteraction) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 183 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 184 | const member = interaction.member as GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 185 | const me = interaction.guild.members.me!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 186 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 187 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 188 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 189 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 190 | // Do not allow kicking the owner |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 191 | 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] | 192 | // Check if Nucleus can kick the member |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 193 | 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] | 194 | // Check if Nucleus has permission to kick |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 195 | 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] | 196 | // Do not allow kicking Nucleus |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 197 | 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] | 198 | // Allow the owner to kick anyone |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 199 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 200 | // Check if the user has kick_members permission |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 201 | 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] | 202 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 203 | 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] | 204 | // Allow kick |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 205 | return true; |
| 206 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 207 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 208 | export { command, callback, check }; |