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