pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 2 | import humanizeDuration from 'humanize-duration'; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 4 | import { WrappedCheck } from "jshaiku"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 5 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 7 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 8 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("kick") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | .setDescription("Kicks a user from the server") |
| 14 | .addUserOption(option => option.setName("user").setDescription("The user to kick").setRequired(true)) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 15 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are kicked | Default: Yes").setRequired(false) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 16 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 17 | ) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 19 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 22 | let reason = null; |
| 23 | let confirmation |
| 24 | while (true) { |
| 25 | confirmation = await new confirmationMessage(interaction) |
| 26 | .setEmoji("PUNISH.KICK.RED") |
| 27 | .setTitle("Kick") |
| 28 | .setDescription(keyValueList({ |
| 29 | "user": renderUser(interaction.options.getUser("user")), |
| 30 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 31 | }) |
| 32 | + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n` |
| 33 | + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 34 | .setColor("Danger") |
| 35 | .addReasonButton(reason ?? "") |
| 36 | .send(reason !== null) |
| 37 | reason = reason ?? "" |
| 38 | if (confirmation.newReason === undefined) break |
| 39 | reason = confirmation.newReason |
| 40 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 41 | if (confirmation.success) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 42 | let dmd = false |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 43 | let dm; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 44 | let config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 45 | try { |
| 46 | if (interaction.options.getString("notify") != "no") { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 47 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 48 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 49 | .setEmoji("PUNISH.KICK.RED") |
| 50 | .setTitle("Kicked") |
| 51 | .setDescription(`You have been kicked in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 52 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 53 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 54 | ], |
| 55 | components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton() |
| 56 | .setStyle("LINK") |
| 57 | .setLabel(config.moderation.kick.text) |
| 58 | .setURL(config.moderation.kick.link) |
| 59 | ] : [])] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 60 | }) |
| 61 | dmd = true |
| 62 | } |
| 63 | } catch {} |
| 64 | try { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame^] | 65 | (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 66 | let member = (interaction.options.getMember("user") as GuildMember) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 67 | try { await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason) } catch {} |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 68 | // @ts-ignore |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 69 | const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 70 | let data = { |
| 71 | meta: { |
| 72 | type: 'memberKick', |
| 73 | displayName: 'Member Kicked', |
| 74 | calculateType: 'guildMemberPunish', |
| 75 | color: NucleusColors.red, |
| 76 | emoji: "PUNISH.KICK.RED", |
| 77 | timestamp: new Date().getTime() |
| 78 | }, |
| 79 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 80 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 81 | name: entry(member.id, renderUser(member.user)), |
| 82 | joined: entry(member.joinedAt, renderDelta(member.joinedAt)), |
| 83 | kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 84 | kickedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 85 | reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"), |
| 86 | timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedTimestamp, { round: true })), |
| 87 | accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)), |
| 88 | serverMemberCount: member.guild.memberCount, |
| 89 | }, |
| 90 | hidden: { |
| 91 | guild: member.guild.id |
| 92 | } |
| 93 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 94 | log(data); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 95 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 96 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 97 | .setEmoji("PUNISH.KICK.RED") |
| 98 | .setTitle(`Kick`) |
| 99 | .setDescription("Something went wrong and the user was not kicked") |
| 100 | .setStatus("Danger") |
| 101 | ], components: []}) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 102 | if (dmd) await dm.delete() |
| 103 | return |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 104 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 105 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 106 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 107 | .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`) |
| 108 | .setTitle(`Kick`) |
| 109 | .setDescription("The member was kicked" + (failed ? ", but could not be notified" : "")) |
| 110 | .setStatus(failed ? "Warning" : "Success") |
| 111 | ], components: []}) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 112 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 113 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 114 | .setEmoji("PUNISH.KICK.GREEN") |
| 115 | .setTitle(`Kick`) |
| 116 | .setDescription("No changes were made") |
| 117 | .setStatus("Success") |
| 118 | ], components: []}) |
| 119 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 123 | let member = (interaction.member as GuildMember) |
| 124 | let me = (interaction.guild.me as GuildMember) |
| 125 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 126 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 127 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 128 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 129 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 130 | // Check if Nucleus can kick the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 131 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 132 | // Check if Nucleus has permission to kick |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 133 | if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the Kick members permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 134 | // Do not allow kicking Nucleus |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 135 | if (member.id == interaction.guild.me.id) throw "I cannot kick myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 136 | // Allow the owner to kick anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 137 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 138 | // Check if the user has kick_members permission |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 139 | if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the Kick members permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 140 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 141 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 142 | // Allow kick |
| 143 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 146 | export { command, callback, check }; |