blob: 6743ebfc2f14e8322bde74275c433f917f30da05 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LinkWarningFooter } from '../../utils/defaults.js';
TheCodedProff86ba092023-01-27 17:10:07 -05002import { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandSubcommandBuilder } from "discord.js";
pineafan62ce1922022-08-25 20:34:45 +01003// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import humanizeDuration from "humanize-duration";
PineaFana00db1b2023-01-02 15:32:54 +00005import type Discord from "discord.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01007import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00008import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01009import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +000010
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("kick")
14 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010015 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000016
pineafan3a02ea32022-08-11 21:35:04 +010017const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000018 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010019 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000020 // TODO:[Modals] Replace this with a modal
PineaFana00db1b2023-01-02 15:32:54 +000021 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010022 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010023 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010024 let timedOut = false;
PineaFana00db1b2023-01-02 15:32:54 +000025 let success = false;
26 do {
pineafan73a7c4a2022-07-24 10:38:04 +010027 confirmation = await new confirmationMessage(interaction)
28 .setEmoji("PUNISH.KICK.RED")
29 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010030 .setDescription(
31 keyValueList({
PineaFana00db1b2023-01-02 15:32:54 +000032 user: renderUser(interaction.options.getUser("user")!),
pineafan62ce1922022-08-25 20:34:45 +010033 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010034 }) +
Skyler Grey11236ba2022-08-08 21:13:33 +010035 `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010036 )
pineafan73a7c4a2022-07-24 10:38:04 +010037 .setColor("Danger")
PineaFana00db1b2023-01-02 15:32:54 +000038 .addCustomBoolean(
39 "notify",
40 "Notify user",
41 false,
42 null,
43 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000044 null,
PineaFana00db1b2023-01-02 15:32:54 +000045 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
46 notify
47 )
pineafan73a7c4a2022-07-24 10:38:04 +010048 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000049 .setFailedMessage("No changes were made", "Success", "PUNISH.KICK.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010050 .send(reason !== null);
51 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010052 if (confirmation.cancelled) timedOut = true;
PineaFana00db1b2023-01-02 15:32:54 +000053 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010054 else if (confirmation.newReason) reason = confirmation.newReason;
55 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010056 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010057 }
PineaFana00db1b2023-01-02 15:32:54 +000058 } while (!timedOut && !success)
PineaFan1dee28f2023-01-16 22:09:07 +000059 if (timedOut || !confirmation.success) return;
PineaFana00db1b2023-01-02 15:32:54 +000060 let dmSent = false;
61 let dmMessage;
62 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Greyad002172022-08-16 18:48:26 +010063 try {
64 if (notify) {
PineaFan538d3752023-01-12 21:48:23 +000065 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
PineaFana00db1b2023-01-02 15:32:54 +000066 const messageData: {
67 embeds: EmojiEmbed[];
68 components: ActionRowBuilder<ButtonBuilder>[];
69 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 embeds: [
71 new EmojiEmbed()
72 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010073 .setTitle("Kicked")
74 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000075 `You have been kicked from ${interaction.guild.name}` +
PineaFan538d3752023-01-12 21:48:23 +000076 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010077 )
Skyler Grey75ea9172022-08-06 10:22:23 +010078 .setStatus("Danger")
79 ],
PineaFana00db1b2023-01-02 15:32:54 +000080 components: []
81 };
82 if (config.moderation.kick.text && config.moderation.kick.link) {
83 messageData.embeds[0]!.setFooter(LinkWarningFooter)
84 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
85 .addComponents(new ButtonBuilder()
86 .setStyle(ButtonStyle.Link)
87 .setLabel(config.moderation.kick.text)
PineaFan9b2ac4d2023-01-18 14:41:07 +000088 .setURL(config.moderation.kick.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
PineaFana00db1b2023-01-02 15:32:54 +000089 )
90 )
91 }
92 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
93 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +000094 }
Skyler Greyad002172022-08-16 18:48:26 +010095 } catch {
PineaFana00db1b2023-01-02 15:32:54 +000096 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +000097 }
Skyler Greyad002172022-08-16 18:48:26 +010098 try {
PineaFana00db1b2023-01-02 15:32:54 +000099 (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided");
Skyler Greyad002172022-08-16 18:48:26 +0100100 const member = interaction.options.getMember("user") as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000101 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100102 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan62ce1922022-08-25 20:34:45 +0100103 const timeInServer = member.joinedTimestamp ? entry(
PineaFana00db1b2023-01-02 15:32:54 +0000104 (new Date().getTime() - member.joinedTimestamp).toString(),
pineafan62ce1922022-08-25 20:34:45 +0100105 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
106 round: true
107 })
108 ) : entry(null, "*Unknown*")
Skyler Greyad002172022-08-16 18:48:26 +0100109 const data = {
110 meta: {
111 type: "memberKick",
112 displayName: "Member Kicked",
113 calculateType: "guildMemberPunish",
114 color: NucleusColors.red,
115 emoji: "PUNISH.KICK.RED",
116 timestamp: new Date().getTime()
117 },
118 list: {
119 memberId: entry(member.id, `\`${member.id}\``),
120 name: entry(member.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000121 joined: undefined as (unknown | typeof entry),
122 kicked: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
Skyler Greyad002172022-08-16 18:48:26 +0100123 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
124 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100125 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100126 serverMemberCount: member.guild.memberCount
127 },
128 hidden: {
129 guild: member.guild.id
130 }
131 };
PineaFana00db1b2023-01-02 15:32:54 +0000132 if (member.joinedTimestamp) {
133 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp))
134 }
135 await client.database.history.create(
136 "kick",
137 interaction.guild.id,
138 member.user,
139 interaction.user,
140 reason
141 )
Skyler Greyad002172022-08-16 18:48:26 +0100142 log(data);
143 } catch {
144 await interaction.editReply({
145 embeds: [
146 new EmojiEmbed()
147 .setEmoji("PUNISH.KICK.RED")
148 .setTitle("Kick")
149 .setDescription("Something went wrong and the user was not kicked")
150 .setStatus("Danger")
151 ],
152 components: []
153 });
PineaFana00db1b2023-01-02 15:32:54 +0000154 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100155 return;
156 }
PineaFana00db1b2023-01-02 15:32:54 +0000157 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100158 await interaction.editReply({
159 embeds: [
160 new EmojiEmbed()
161 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
162 .setTitle("Kick")
163 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
164 .setStatus(failed ? "Warning" : "Success")
165 ],
166 components: []
167 });
pineafan63fc5e22022-08-04 22:04:10 +0100168};
pineafan4f164f32022-02-26 22:07:12 +0000169
TheCodedProff86ba092023-01-27 17:10:07 -0500170const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000171 if (!interaction.guild) return;
TheCodedProff86ba092023-01-27 17:10:07 -0500172
Skyler Grey75ea9172022-08-06 10:22:23 +0100173 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500174 // Check if the user has kick_members permission
175 if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission";
176 if (partial) return true;
177
PineaFana00db1b2023-01-02 15:32:54 +0000178 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100179 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100180 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
181 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
182 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
TheCodedProff86ba092023-01-27 17:10:07 -0500183 // Check if Nucleus has permission to kick
184 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
185 // Allow the owner to kick anyone
186 if (member.id === interaction.guild.ownerId) return true;
pineafanc1c18792022-08-03 21:41:36 +0100187 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000188 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000189 // Check if Nucleus can kick the member
PineaFan0d06edc2023-01-17 22:10:31 +0000190 if (!(mePos > applyPos)) return "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000191 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000192 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000193 // Check if the user is below on the role list
PineaFan0d06edc2023-01-17 22:10:31 +0000194 if (!(memberPos > applyPos)) return "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000195 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100196 return true;
197};
pineafan4f164f32022-02-26 22:07:12 +0000198
Skyler Grey75ea9172022-08-06 10:22:23 +0100199export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500200export const metadata = {
201 longDescription: "Removes a member from the server. They will be able to rejoin if they have an invite link.",
202 premiumOnly: true,
203}