blob: ef748e8766a151a521ed830684c5db5c20441ea6 [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";
pineafan4edb7762022-06-26 19:21:04 +01006import EmojiEmbed 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))
pineafan4f164f32022-02-26 22:07:12 +000015
pineafan4edb7762022-06-26 19:21:04 +010016const callback = async (interaction: CommandInteraction): Promise<any> => {
17 const { renderUser } = client.logger
pineafan8b4b17f2022-02-27 20:42:52 +000018 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010019 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010020 let notify = true;
pineafan73a7c4a2022-07-24 10:38:04 +010021 let confirmation
22 while (true) {
23 confirmation = await new confirmationMessage(interaction)
24 .setEmoji("PUNISH.KICK.RED")
25 .setTitle("Kick")
26 .setDescription(keyValueList({
27 "user": renderUser(interaction.options.getUser("user")),
28 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
29 })
pineafan02ba0232022-07-24 22:16:15 +010030 + `The user **will${notify ? '' : ' not'}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010031 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
32 .setColor("Danger")
33 .addReasonButton(reason ?? "")
34 .send(reason !== null)
35 reason = reason ?? ""
pineafan02ba0232022-07-24 22:16:15 +010036 if (confirmation.cancelled) return
37 if (confirmation.success) break
38 if (confirmation.newReason) reason = confirmation.newReason
39 if (confirmation.components) {
40 notify = confirmation.components.notify.active
41 }
pineafan73a7c4a2022-07-24 10:38:04 +010042 }
pineafan377794f2022-04-18 19:01:01 +010043 if (confirmation.success) {
pineafan8b4b17f2022-02-27 20:42:52 +000044 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000045 let dm;
pineafan4edb7762022-06-26 19:21:04 +010046 let config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000047 try {
pineafan02ba0232022-07-24 22:16:15 +010048 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000049 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010050 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000051 .setEmoji("PUNISH.KICK.RED")
52 .setTitle("Kicked")
53 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010054 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000055 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010056 ],
57 components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton()
58 .setStyle("LINK")
59 .setLabel(config.moderation.kick.text)
60 .setURL(config.moderation.kick.link)
61 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000062 })
63 dmd = true
64 }
65 } catch {}
66 try {
pineafan73a7c4a2022-07-24 10:38:04 +010067 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.")
pineafane625d782022-05-09 18:04:32 +010068 let member = (interaction.options.getMember("user") as GuildMember)
pineafan4edb7762022-06-26 19:21:04 +010069 try { await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason) } catch {}
pineafan02ba0232022-07-24 22:16:15 +010070 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
pineafane625d782022-05-09 18:04:32 +010071 let data = {
72 meta: {
73 type: 'memberKick',
74 displayName: 'Member Kicked',
75 calculateType: 'guildMemberPunish',
76 color: NucleusColors.red,
77 emoji: "PUNISH.KICK.RED",
78 timestamp: new Date().getTime()
79 },
80 list: {
pineafanda6e5342022-07-03 10:03:16 +010081 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010082 name: entry(member.id, renderUser(member.user)),
83 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
84 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
85 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
86 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
87 timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedTimestamp, { round: true })),
88 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
89 serverMemberCount: member.guild.memberCount,
90 },
91 hidden: {
92 guild: member.guild.id
93 }
94 }
pineafan4edb7762022-06-26 19:21:04 +010095 log(data);
pineafan8b4b17f2022-02-27 20:42:52 +000096 } catch {
pineafan4edb7762022-06-26 19:21:04 +010097 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000098 .setEmoji("PUNISH.KICK.RED")
99 .setTitle(`Kick`)
100 .setDescription("Something went wrong and the user was not kicked")
101 .setStatus("Danger")
102 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +0000103 if (dmd) await dm.delete()
104 return
pineafan8b4b17f2022-02-27 20:42:52 +0000105 }
pineafane23c4ec2022-07-27 21:56:27 +0100106 let failed = (dmd === false && notify)
pineafan4edb7762022-06-26 19:21:04 +0100107 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000108 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
109 .setTitle(`Kick`)
110 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
111 .setStatus(failed ? "Warning" : "Success")
112 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +0000113 } else {
pineafan4edb7762022-06-26 19:21:04 +0100114 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000115 .setEmoji("PUNISH.KICK.GREEN")
116 .setTitle(`Kick`)
117 .setDescription("No changes were made")
118 .setStatus("Success")
119 ], components: []})
120 }
pineafan4f164f32022-02-26 22:07:12 +0000121}
122
123const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000124 let member = (interaction.member as GuildMember)
125 let me = (interaction.guild.me as GuildMember)
126 let apply = (interaction.options.getMember("user") as GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100127 if (member === null || me === null || apply === null) throw "That member is not in the server"
pineafan5d1908e2022-02-28 21:34:47 +0000128 let memberPos = member.roles ? member.roles.highest.position : 0
129 let mePos = me.roles ? me.roles.highest.position : 0
130 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafanc1c18792022-08-03 21:41:36 +0100131 // Do not allow kicking the owner
132 if (member.id === interaction.guild.ownerId) throw "You cannot kick the owner of the server"
pineafan8b4b17f2022-02-27 20:42:52 +0000133 // Check if Nucleus can kick the member
pineafan5d1908e2022-02-28 21:34:47 +0000134 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000135 // Check if Nucleus has permission to kick
pineafane23c4ec2022-07-27 21:56:27 +0100136 if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000137 // Do not allow kicking Nucleus
pineafane23c4ec2022-07-27 21:56:27 +0100138 if (member.id === interaction.guild.me.id) throw "I cannot kick myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000139 // Allow the owner to kick anyone
pineafane23c4ec2022-07-27 21:56:27 +0100140 if (member.id === interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000141 // Check if the user has kick_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100142 if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000143 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000144 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000145 // Allow kick
146 return true
pineafan4f164f32022-02-26 22:07:12 +0000147}
148
pineafan8b4b17f2022-02-27 20:42:52 +0000149export { command, callback, check };