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