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