blob: 20fbc01a43713daf43e8b4d2355f83caf15d8fc1 [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafane625d782022-05-09 18:04:32 +01002import humanizeDuration from 'humanize-duration';
pineafan4f164f32022-02-26 22:07:12 +00003import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
4import { WrappedCheck } from "jshaiku";
pineafan8b4b17f2022-02-27 20:42:52 +00005import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan377794f2022-04-18 19:01:01 +01006import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00007import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01008import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("kick")
pineafan8b4b17f2022-02-27 20:42:52 +000013 .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 )
pineafan4f164f32022-02-26 22:07:12 +000019
pineafan8b4b17f2022-02-27 20:42:52 +000020const callback = async (interaction: CommandInteraction) => {
21 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010022 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000023 .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")
pineafan377794f2022-04-18 19:01:01 +010032 .send()
33 if (confirmation.success) {
pineafan8b4b17f2022-02-27 20:42:52 +000034 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000035 let dm;
pineafan6702cef2022-06-13 17:52:37 +010036 let config = await client.database.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000037 try {
38 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000039 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010040 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000041 .setEmoji("PUNISH.KICK.RED")
42 .setTitle("Kicked")
43 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000044 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000045 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010046 ],
47 components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton()
48 .setStyle("LINK")
49 .setLabel(config.moderation.kick.text)
50 .setURL(config.moderation.kick.link)
51 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000052 })
53 dmd = true
54 }
55 } catch {}
56 try {
57 (interaction.options.getMember("user") as GuildMember).kick(interaction.options.getString("reason") ?? "No reason provided.")
pineafane625d782022-05-09 18:04:32 +010058 let member = (interaction.options.getMember("user") as GuildMember)
59 let reason = interaction.options.getString("reason") ?? null
60 // @ts-ignore
61 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
62 let data = {
63 meta: {
64 type: 'memberKick',
65 displayName: 'Member Kicked',
66 calculateType: 'guildMemberPunish',
67 color: NucleusColors.red,
68 emoji: "PUNISH.KICK.RED",
69 timestamp: new Date().getTime()
70 },
71 list: {
72 id: entry(member.id, `\`${member.id}\``),
73 name: entry(member.id, renderUser(member.user)),
74 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
75 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
76 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
77 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
78 timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedTimestamp, { round: true })),
79 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
80 serverMemberCount: member.guild.memberCount,
81 },
82 hidden: {
83 guild: member.guild.id
84 }
85 }
86 log(data, member.client);
pineafan8b4b17f2022-02-27 20:42:52 +000087 } catch {
pineafan377794f2022-04-18 19:01:01 +010088 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000089 .setEmoji("PUNISH.KICK.RED")
90 .setTitle(`Kick`)
91 .setDescription("Something went wrong and the user was not kicked")
92 .setStatus("Danger")
93 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000094 if (dmd) await dm.delete()
95 return
pineafan8b4b17f2022-02-27 20:42:52 +000096 }
pineafan5d1908e2022-02-28 21:34:47 +000097 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +010098 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000099 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
100 .setTitle(`Kick`)
101 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
102 .setStatus(failed ? "Warning" : "Success")
103 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +0000104 } else {
pineafan377794f2022-04-18 19:01:01 +0100105 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000106 .setEmoji("PUNISH.KICK.GREEN")
107 .setTitle(`Kick`)
108 .setDescription("No changes were made")
109 .setStatus("Success")
110 ], components: []})
111 }
pineafan4f164f32022-02-26 22:07:12 +0000112}
113
114const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000115 let member = (interaction.member as GuildMember)
116 let me = (interaction.guild.me as GuildMember)
117 let apply = (interaction.options.getMember("user") as GuildMember)
118 if (member == null || me == null || apply == null) throw "That member is not in the server"
119 let memberPos = member.roles ? member.roles.highest.position : 0
120 let mePos = me.roles ? me.roles.highest.position : 0
121 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +0000122 // Check if Nucleus can kick the member
pineafan5d1908e2022-02-28 21:34:47 +0000123 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000124 // Check if Nucleus has permission to kick
PineappleFan5fe720d2022-05-19 12:01:49 +0100125 if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the `kick_members` permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000126 // Do not allow kicking Nucleus
pineafan663dc472022-05-10 18:13:47 +0100127 if (member.id == interaction.guild.me.id) throw "I cannot kick myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000128 // Allow the owner to kick anyone
pineafan663dc472022-05-10 18:13:47 +0100129 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000130 // Check if the user has kick_members permission
pineafan663dc472022-05-10 18:13:47 +0100131 if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the `kick_members` permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000132 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000133 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000134 // Allow kick
135 return true
pineafan4f164f32022-02-26 22:07:12 +0000136}
137
pineafan8b4b17f2022-02-27 20:42:52 +0000138export { command, callback, check };