blob: 97ead7b64ab7cbc62c0677be0cd0658de358fd8e [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))
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
pineafan4edb7762022-06-26 19:21:04 +010020const callback = async (interaction: CommandInteraction): Promise<any> => {
21 const { renderUser } = client.logger
pineafan8b4b17f2022-02-27 20:42:52 +000022 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010023 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000024 .setEmoji("PUNISH.KICK.RED")
25 .setTitle("Kick")
26 .setDescription(keyValueList({
pineafan4edb7762022-06-26 19:21:04 +010027 "user": renderUser(interaction.options.getUser("user")),
pineafan8b4b17f2022-02-27 20:42:52 +000028 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
29 })
30 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`
31 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
32 .setColor("Danger")
pineafan377794f2022-04-18 19:01:01 +010033 .send()
34 if (confirmation.success) {
pineafan8b4b17f2022-02-27 20:42:52 +000035 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000036 let dm;
pineafan4edb7762022-06-26 19:21:04 +010037 let config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000038 try {
39 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000040 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010041 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000042 .setEmoji("PUNISH.KICK.RED")
43 .setTitle("Kicked")
44 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000045 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000046 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010047 ],
48 components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton()
49 .setStyle("LINK")
50 .setLabel(config.moderation.kick.text)
51 .setURL(config.moderation.kick.link)
52 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000053 })
54 dmd = true
55 }
56 } catch {}
57 try {
58 (interaction.options.getMember("user") as GuildMember).kick(interaction.options.getString("reason") ?? "No reason provided.")
pineafane625d782022-05-09 18:04:32 +010059 let member = (interaction.options.getMember("user") as GuildMember)
60 let reason = interaction.options.getString("reason") ?? null
pineafan4edb7762022-06-26 19:21:04 +010061 try { await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason) } catch {}
pineafane625d782022-05-09 18:04:32 +010062 // @ts-ignore
pineafan4edb7762022-06-26 19:21:04 +010063 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
pineafane625d782022-05-09 18:04:32 +010064 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 }
pineafan4edb7762022-06-26 19:21:04 +010088 log(data);
pineafan8b4b17f2022-02-27 20:42:52 +000089 } catch {
pineafan4edb7762022-06-26 19:21:04 +010090 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000091 .setEmoji("PUNISH.KICK.RED")
92 .setTitle(`Kick`)
93 .setDescription("Something went wrong and the user was not kicked")
94 .setStatus("Danger")
95 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000096 if (dmd) await dm.delete()
97 return
pineafan8b4b17f2022-02-27 20:42:52 +000098 }
pineafan5d1908e2022-02-28 21:34:47 +000099 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan4edb7762022-06-26 19:21:04 +0100100 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000101 .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: []})
pineafan8b4b17f2022-02-27 20:42:52 +0000106 } else {
pineafan4edb7762022-06-26 19:21:04 +0100107 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000108 .setEmoji("PUNISH.KICK.GREEN")
109 .setTitle(`Kick`)
110 .setDescription("No changes were made")
111 .setStatus("Success")
112 ], components: []})
113 }
pineafan4f164f32022-02-26 22:07:12 +0000114}
115
116const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000117 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
pineafan8b4b17f2022-02-27 20:42:52 +0000124 // Check if Nucleus can kick the member
pineafan5d1908e2022-02-28 21:34:47 +0000125 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000126 // Check if Nucleus has permission to kick
pineafan4edb7762022-06-26 19:21:04 +0100127 if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the Kick members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000128 // Do not allow kicking Nucleus
pineafan663dc472022-05-10 18:13:47 +0100129 if (member.id == interaction.guild.me.id) throw "I cannot kick myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000130 // Allow the owner to kick anyone
pineafan663dc472022-05-10 18:13:47 +0100131 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000132 // Check if the user has kick_members permission
pineafan4edb7762022-06-26 19:21:04 +0100133 if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the Kick members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000134 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000135 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000136 // Allow kick
137 return true
pineafan4f164f32022-02-26 22:07:12 +0000138}
139
pineafan8b4b17f2022-02-27 20:42:52 +0000140export { command, callback, check };