blob: 34a1571c1b1580f2f0e36b3ee8898168943764f1 [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
pineafan8b4b17f2022-02-27 20:42:52 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan377794f2022-04-18 19:01:01 +01005import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan377794f2022-04-18 19:01:01 +01007import readConfig from '../../utils/readConfig.js'
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
11 .setName("kick")
pineafan8b4b17f2022-02-27 20:42:52 +000012 .setDescription("Kicks a user from the server")
13 .addUserOption(option => option.setName("user").setDescription("The user to kick").setRequired(true))
14 .addStringOption(option => option.setName("reason").setDescription("The reason for the kick").setRequired(false))
15 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are kicked | Default yes").setRequired(false)
16 .addChoices([["Yes", "yes"], ["No", "no"]])
17 )
pineafan4f164f32022-02-26 22:07:12 +000018
pineafan8b4b17f2022-02-27 20:42:52 +000019const callback = async (interaction: CommandInteraction) => {
20 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010021 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000022 .setEmoji("PUNISH.KICK.RED")
23 .setTitle("Kick")
24 .setDescription(keyValueList({
25 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
26 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
27 })
28 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`
29 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
30 .setColor("Danger")
31// pluralize("day", interaction.options.getInteger("delete"))
32// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
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;
pineafan377794f2022-04-18 19:01:01 +010037 let config = await readConfig(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({
pineafan377794f2022-04-18 19:01:01 +010041 embeds: [new generateEmojiEmbed()
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.")
pineafan8b4b17f2022-02-27 20:42:52 +000059 } catch {
pineafan377794f2022-04-18 19:01:01 +010060 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000061 .setEmoji("PUNISH.KICK.RED")
62 .setTitle(`Kick`)
63 .setDescription("Something went wrong and the user was not kicked")
64 .setStatus("Danger")
65 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000066 if (dmd) await dm.delete()
67 return
pineafan8b4b17f2022-02-27 20:42:52 +000068 }
pineafan5d1908e2022-02-28 21:34:47 +000069 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +010070 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000071 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
72 .setTitle(`Kick`)
73 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
74 .setStatus(failed ? "Warning" : "Success")
75 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000076 } else {
pineafan377794f2022-04-18 19:01:01 +010077 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000078 .setEmoji("PUNISH.KICK.GREEN")
79 .setTitle(`Kick`)
80 .setDescription("No changes were made")
81 .setStatus("Success")
82 ], components: []})
83 }
pineafan4f164f32022-02-26 22:07:12 +000084}
85
86const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000087 let member = (interaction.member as GuildMember)
88 let me = (interaction.guild.me as GuildMember)
89 let apply = (interaction.options.getMember("user") as GuildMember)
90 if (member == null || me == null || apply == null) throw "That member is not in the server"
91 let memberPos = member.roles ? member.roles.highest.position : 0
92 let mePos = me.roles ? me.roles.highest.position : 0
93 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000094 // Check if Nucleus can kick the member
pineafan5d1908e2022-02-28 21:34:47 +000095 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +000096 // Check if Nucleus has permission to kick
97 if (! interaction.guild.me.permissions.has("KICK_MEMBERS")) throw "I do not have the `kick_members` permission";
98 // Do not allow kicking Nucleus
99 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot kick myself"
100 // Allow the owner to kick anyone
101 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
102 // Check if the user has kick_members permission
103 if (! (interaction.member as GuildMember).permissions.has("KICK_MEMBERS")) throw "You do not have the `kick_members` permission";
104 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000105 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000106 // Allow kick
107 return true
pineafan4f164f32022-02-26 22:07:12 +0000108}
109
pineafan8b4b17f2022-02-27 20:42:52 +0000110export { command, callback, check };