blob: f10563ed5c0ea7c84fd2e5072009d010cc2b22f5 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import humanizeDuration from "humanize-duration";
pineafan4f164f32022-02-26 22:07:12 +00003import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan8b4b17f2022-02-27 20:42:52 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("kick")
12 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010013 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
Skyler Grey11236ba2022-08-08 21:13:33 +010015const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010016 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000017 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010018 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010019 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010020 let confirmation;
pineafan73a7c4a2022-07-24 10:38:04 +010021 while (true) {
22 confirmation = await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.KICK.RED")
24 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010025 .setDescription(
26 keyValueList({
27 user: renderUser(interaction.options.getUser("user")),
Skyler Grey11236ba2022-08-08 21:13:33 +010028 reason: reason ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010029 }) +
30 `The user **will${notify ? "" : " not"}** be notified\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010031 `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010032 )
pineafan73a7c4a2022-07-24 10:38:04 +010033 .setColor("Danger")
34 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010035 .send(reason !== null);
36 reason = reason ?? "";
37 if (confirmation.cancelled) return;
38 if (confirmation.success) break;
39 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010040 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010041 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010042 }
pineafan73a7c4a2022-07-24 10:38:04 +010043 }
pineafan377794f2022-04-18 19:01:01 +010044 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010045 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000046 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010047 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000048 try {
pineafan02ba0232022-07-24 22:16:15 +010049 if (notify) {
Skyler Grey11236ba2022-08-08 21:13:33 +010050 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010051 embeds: [
52 new EmojiEmbed()
53 .setEmoji("PUNISH.KICK.RED")
54 .setTitle("Kicked")
55 .setDescription(
56 `You have been kicked in ${interaction.guild.name}` +
57 (reason ? ` for:\n> ${reason}` : ".")
58 )
59 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010060 ],
Skyler Grey75ea9172022-08-06 10:22:23 +010061 components: [
62 new MessageActionRow().addComponents(
63 config.moderation.kick.text
64 ? [
65 new MessageButton()
66 .setStyle("LINK")
67 .setLabel(config.moderation.kick.text)
68 .setURL(config.moderation.kick.link)
69 ]
70 : []
71 )
72 ]
pineafan63fc5e22022-08-04 22:04:10 +010073 });
74 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000075 }
Skyler Grey75ea9172022-08-06 10:22:23 +010076 } catch {
77 dmd = false;
78 }
pineafan8b4b17f2022-02-27 20:42:52 +000079 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010080 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.");
Skyler Grey75ea9172022-08-06 10:22:23 +010081 const member = interaction.options.getMember("user") as GuildMember;
Skyler Grey11236ba2022-08-08 21:13:33 +010082 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
83 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010084 const data = {
pineafane625d782022-05-09 18:04:32 +010085 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010086 type: "memberKick",
87 displayName: "Member Kicked",
88 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010089 color: NucleusColors.red,
90 emoji: "PUNISH.KICK.RED",
91 timestamp: new Date().getTime()
92 },
93 list: {
pineafanda6e5342022-07-03 10:03:16 +010094 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010095 name: entry(member.id, renderUser(member.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +010096 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
97 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
98 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
99 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 timeInServer: entry(
101 new Date().getTime() - member.joinedTimestamp,
Skyler Grey11236ba2022-08-08 21:13:33 +0100102 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
103 round: true
104 })
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100106 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
pineafan63fc5e22022-08-04 22:04:10 +0100107 serverMemberCount: member.guild.memberCount
pineafane625d782022-05-09 18:04:32 +0100108 },
109 hidden: {
110 guild: member.guild.id
111 }
pineafan63fc5e22022-08-04 22:04:10 +0100112 };
pineafan4edb7762022-06-26 19:21:04 +0100113 log(data);
pineafan8b4b17f2022-02-27 20:42:52 +0000114 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 await interaction.editReply({
116 embeds: [
117 new EmojiEmbed()
118 .setEmoji("PUNISH.KICK.RED")
119 .setTitle("Kick")
Skyler Grey11236ba2022-08-08 21:13:33 +0100120 .setDescription("Something went wrong and the user was not kicked")
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 .setStatus("Danger")
122 ],
123 components: []
124 });
pineafan63fc5e22022-08-04 22:04:10 +0100125 if (dmd) await dm.delete();
126 return;
pineafan8b4b17f2022-02-27 20:42:52 +0000127 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 const failed = !dmd && notify;
129 await interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
133 .setTitle("Kick")
Skyler Grey11236ba2022-08-08 21:13:33 +0100134 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 .setStatus(failed ? "Warning" : "Success")
136 ],
137 components: []
138 });
pineafan8b4b17f2022-02-27 20:42:52 +0000139 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 await interaction.editReply({
141 embeds: [
142 new EmojiEmbed()
143 .setEmoji("PUNISH.KICK.GREEN")
144 .setTitle("Kick")
145 .setDescription("No changes were made")
146 .setStatus("Success")
147 ],
148 components: []
149 });
pineafan8b4b17f2022-02-27 20:42:52 +0000150 }
pineafan63fc5e22022-08-04 22:04:10 +0100151};
pineafan4f164f32022-02-26 22:07:12 +0000152
pineafanbd02b4a2022-08-05 22:01:38 +0100153const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100154 const member = interaction.member as GuildMember;
155 const me = interaction.guild.me!;
156 const apply = interaction.options.getMember("user") as GuildMember;
Skyler Grey11236ba2022-08-08 21:13:33 +0100157 if (member === null || me === null || apply === null) throw "That member is not in the server";
pineafan63fc5e22022-08-04 22:04:10 +0100158 const memberPos = member.roles ? member.roles.highest.position : 0;
159 const mePos = me.roles ? me.roles.highest.position : 0;
160 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100161 // Do not allow kicking the owner
Skyler Grey11236ba2022-08-08 21:13:33 +0100162 if (member.id === interaction.guild.ownerId) throw "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000163 // Check if Nucleus can kick the member
Skyler Grey11236ba2022-08-08 21:13:33 +0100164 if (!(mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000165 // Check if Nucleus has permission to kick
Skyler Grey11236ba2022-08-08 21:13:33 +0100166 if (!me.permissions.has("KICK_MEMBERS")) throw "I do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000167 // Do not allow kicking Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100168 if (member.id === interaction.guild.me.id) throw "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000169 // Allow the owner to kick anyone
pineafan63fc5e22022-08-04 22:04:10 +0100170 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000171 // Check if the user has kick_members permission
Skyler Grey11236ba2022-08-08 21:13:33 +0100172 if (!member.permissions.has("KICK_MEMBERS")) throw "You do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000173 // Check if the user is below on the role list
Skyler Grey11236ba2022-08-08 21:13:33 +0100174 if (!(memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000175 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100176 return true;
177};
pineafan4f164f32022-02-26 22:07:12 +0000178
Skyler Grey75ea9172022-08-06 10:22:23 +0100179export { command, callback, check };