blob: 56e00e2c55c22a3bd8256138f487e0416cba0e11 [file] [log] [blame]
pineafan8b4b17f2022-02-27 20:42:52 +00001import { CommandInteraction, GuildMember } 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";
5import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
6import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
10 .setName("kick")
pineafan8b4b17f2022-02-27 20:42:52 +000011 .setDescription("Kicks a user from the server")
12 .addUserOption(option => option.setName("user").setDescription("The user to kick").setRequired(true))
13 .addStringOption(option => option.setName("reason").setDescription("The reason for the kick").setRequired(false))
14 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are kicked | Default yes").setRequired(false)
15 .addChoices([["Yes", "yes"], ["No", "no"]])
16 )
pineafan4f164f32022-02-26 22:07:12 +000017
pineafan8b4b17f2022-02-27 20:42:52 +000018const callback = async (interaction: CommandInteraction) => {
19 // TODO:[Modals] Replace this with a modal
20 if (await new confirmationMessage(interaction)
21 .setEmoji("PUNISH.KICK.RED")
22 .setTitle("Kick")
23 .setDescription(keyValueList({
24 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
25 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
26 })
27 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`
28 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
29 .setColor("Danger")
30// pluralize("day", interaction.options.getInteger("delete"))
31// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
32 .send()) {
33 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000034 let dm;
pineafan8b4b17f2022-02-27 20:42:52 +000035 try {
36 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000037 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan8b4b17f2022-02-27 20:42:52 +000038 embeds: [new EmojiEmbed()
39 .setEmoji("PUNISH.KICK.RED")
40 .setTitle("Kicked")
41 .setDescription(`You have been kicked in ${interaction.guild.name}` +
42 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided."))
43 .setStatus("Danger")
44 ]
45 })
46 dmd = true
47 }
48 } catch {}
49 try {
50 (interaction.options.getMember("user") as GuildMember).kick(interaction.options.getString("reason") ?? "No reason provided.")
pineafan8b4b17f2022-02-27 20:42:52 +000051 } catch {
52 await interaction.editReply({embeds: [new EmojiEmbed()
53 .setEmoji("PUNISH.KICK.RED")
54 .setTitle(`Kick`)
55 .setDescription("Something went wrong and the user was not kicked")
56 .setStatus("Danger")
57 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000058 if (dmd) await dm.delete()
59 return
pineafan8b4b17f2022-02-27 20:42:52 +000060 }
pineafan5d1908e2022-02-28 21:34:47 +000061 let failed = (dmd == false && interaction.options.getString("notify") != "no")
62 await interaction.editReply({embeds: [new EmojiEmbed()
63 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
64 .setTitle(`Kick`)
65 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
66 .setStatus(failed ? "Warning" : "Success")
67 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000068 } else {
69 await interaction.editReply({embeds: [new EmojiEmbed()
70 .setEmoji("PUNISH.KICK.GREEN")
71 .setTitle(`Kick`)
72 .setDescription("No changes were made")
73 .setStatus("Success")
74 ], components: []})
75 }
pineafan4f164f32022-02-26 22:07:12 +000076}
77
78const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000079 let member = (interaction.member as GuildMember)
80 let me = (interaction.guild.me as GuildMember)
81 let apply = (interaction.options.getMember("user") as GuildMember)
82 if (member == null || me == null || apply == null) throw "That member is not in the server"
83 let memberPos = member.roles ? member.roles.highest.position : 0
84 let mePos = me.roles ? me.roles.highest.position : 0
85 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000086 // Check if Nucleus can kick the member
pineafan5d1908e2022-02-28 21:34:47 +000087 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +000088 // Check if Nucleus has permission to kick
89 if (! interaction.guild.me.permissions.has("KICK_MEMBERS")) throw "I do not have the `kick_members` permission";
90 // Do not allow kicking Nucleus
91 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot kick myself"
92 // Allow the owner to kick anyone
93 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
94 // Check if the user has kick_members permission
95 if (! (interaction.member as GuildMember).permissions.has("KICK_MEMBERS")) throw "You do not have the `kick_members` permission";
96 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +000097 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +000098 // Allow kick
99 return true
pineafan4f164f32022-02-26 22:07:12 +0000100}
101
pineafan8b4b17f2022-02-27 20:42:52 +0000102export { command, callback, check };