blob: 9dc447b32a22e475d0e445d423851dc2d50a7a1d [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";
pineafan377794f2022-04-18 19:01:01 +01006import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00007import keyValueList from "../../utils/generateKeyValueList.js";
pineafan377794f2022-04-18 19:01:01 +01008import readConfig from '../../utils/readConfig.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
pineafan8b4b17f2022-02-27 20:42:52 +000020const callback = async (interaction: CommandInteraction) => {
21 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010022 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000023 .setEmoji("PUNISH.KICK.RED")
24 .setTitle("Kick")
25 .setDescription(keyValueList({
26 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
27 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
28 })
29 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`
30 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
31 .setColor("Danger")
32// pluralize("day", interaction.options.getInteger("delete"))
33// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
pineafan377794f2022-04-18 19:01:01 +010034 .send()
35 if (confirmation.success) {
pineafan8b4b17f2022-02-27 20:42:52 +000036 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000037 let dm;
pineafan377794f2022-04-18 19:01:01 +010038 let config = await readConfig(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000039 try {
40 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000041 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010042 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000043 .setEmoji("PUNISH.KICK.RED")
44 .setTitle("Kicked")
45 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000046 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000047 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010048 ],
49 components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton()
50 .setStyle("LINK")
51 .setLabel(config.moderation.kick.text)
52 .setURL(config.moderation.kick.link)
53 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000054 })
55 dmd = true
56 }
57 } catch {}
58 try {
59 (interaction.options.getMember("user") as GuildMember).kick(interaction.options.getString("reason") ?? "No reason provided.")
pineafane625d782022-05-09 18:04:32 +010060 let member = (interaction.options.getMember("user") as GuildMember)
61 let reason = interaction.options.getString("reason") ?? null
62 // @ts-ignore
63 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
64 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 }
88 log(data, member.client);
pineafan8b4b17f2022-02-27 20:42:52 +000089 } catch {
pineafan377794f2022-04-18 19:01:01 +010090 await interaction.editReply({embeds: [new generateEmojiEmbed()
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")
pineafan377794f2022-04-18 19:01:01 +0100100 await interaction.editReply({embeds: [new generateEmojiEmbed()
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 {
pineafan377794f2022-04-18 19:01:01 +0100107 await interaction.editReply({embeds: [new generateEmojiEmbed()
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
PineappleFan5fe720d2022-05-19 12:01:49 +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
pineafan663dc472022-05-10 18:13:47 +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 };