blob: 793a63025c15d48c6aed342c9b3f97060139c155 [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))
pineafan73a7c4a2022-07-24 10:38:04 +010015 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are kicked | Default: Yes").setRequired(false)
pineafan8b4b17f2022-02-27 20:42:52 +000016 .addChoices([["Yes", "yes"], ["No", "no"]])
17 )
pineafan4f164f32022-02-26 22:07:12 +000018
pineafan4edb7762022-06-26 19:21:04 +010019const callback = async (interaction: CommandInteraction): Promise<any> => {
20 const { renderUser } = client.logger
pineafan8b4b17f2022-02-27 20:42:52 +000021 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010022 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 }
pineafan377794f2022-04-18 19:01:01 +010041 if (confirmation.success) {
pineafan8b4b17f2022-02-27 20:42:52 +000042 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000043 let dm;
pineafan4edb7762022-06-26 19:21:04 +010044 let config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000045 try {
46 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000047 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010048 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000049 .setEmoji("PUNISH.KICK.RED")
50 .setTitle("Kicked")
51 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010052 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000053 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010054 ],
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 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000060 })
61 dmd = true
62 }
63 } catch {}
64 try {
pineafan73a7c4a2022-07-24 10:38:04 +010065 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.")
pineafane625d782022-05-09 18:04:32 +010066 let member = (interaction.options.getMember("user") as GuildMember)
pineafan4edb7762022-06-26 19:21:04 +010067 try { await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason) } catch {}
pineafane625d782022-05-09 18:04:32 +010068 // @ts-ignore
pineafan4edb7762022-06-26 19:21:04 +010069 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
pineafane625d782022-05-09 18:04:32 +010070 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: {
pineafanda6e5342022-07-03 10:03:16 +010080 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010081 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 }
pineafan4edb7762022-06-26 19:21:04 +010094 log(data);
pineafan8b4b17f2022-02-27 20:42:52 +000095 } catch {
pineafan4edb7762022-06-26 19:21:04 +010096 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000097 .setEmoji("PUNISH.KICK.RED")
98 .setTitle(`Kick`)
99 .setDescription("Something went wrong and the user was not kicked")
100 .setStatus("Danger")
101 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +0000102 if (dmd) await dm.delete()
103 return
pineafan8b4b17f2022-02-27 20:42:52 +0000104 }
pineafan5d1908e2022-02-28 21:34:47 +0000105 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan4edb7762022-06-26 19:21:04 +0100106 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000107 .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: []})
pineafan8b4b17f2022-02-27 20:42:52 +0000112 } else {
pineafan4edb7762022-06-26 19:21:04 +0100113 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000114 .setEmoji("PUNISH.KICK.GREEN")
115 .setTitle(`Kick`)
116 .setDescription("No changes were made")
117 .setStatus("Success")
118 ], components: []})
119 }
pineafan4f164f32022-02-26 22:07:12 +0000120}
121
122const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000123 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
pineafan8b4b17f2022-02-27 20:42:52 +0000130 // Check if Nucleus can kick the member
pineafan5d1908e2022-02-28 21:34:47 +0000131 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000132 // Check if Nucleus has permission to kick
pineafan4edb7762022-06-26 19:21:04 +0100133 if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the Kick members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000134 // Do not allow kicking Nucleus
pineafan663dc472022-05-10 18:13:47 +0100135 if (member.id == interaction.guild.me.id) throw "I cannot kick myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000136 // Allow the owner to kick anyone
pineafan663dc472022-05-10 18:13:47 +0100137 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000138 // Check if the user has kick_members permission
pineafan4edb7762022-06-26 19:21:04 +0100139 if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the Kick members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000140 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000141 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000142 // Allow kick
143 return true
pineafan4f164f32022-02-26 22:07:12 +0000144}
145
pineafan8b4b17f2022-02-27 20:42:52 +0000146export { command, callback, check };