blob: c4f18671d9c050d55fb121ab90e62b32496cf340 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import { LinkWarningFooter } from "../../utils/defaults.js";
2import {
3 CommandInteraction,
4 GuildMember,
5 ActionRowBuilder,
6 ButtonBuilder,
7 ButtonStyle,
8 SlashCommandSubcommandBuilder
9} from "discord.js";
pineafan62ce1922022-08-25 20:34:45 +010010// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +010011import humanizeDuration from "humanize-duration";
PineaFana00db1b2023-01-02 15:32:54 +000012import type Discord from "discord.js";
pineafan8b4b17f2022-02-27 20:42:52 +000013import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +010014import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +000015import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +010016import client from "../../utils/client.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -050017import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +000018
19const command = (builder: SlashCommandSubcommandBuilder) =>
20 builder
pineafan63fc5e22022-08-04 22:04:10 +010021 .setName("kick")
22 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010023 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000024
pineafan3a02ea32022-08-11 21:35:04 +010025const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000026 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010027 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000028 // TODO:[Modals] Replace this with a modal
PineaFana00db1b2023-01-02 15:32:54 +000029 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010030 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010031 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010032 let timedOut = false;
PineaFana00db1b2023-01-02 15:32:54 +000033 let success = false;
34 do {
pineafan73a7c4a2022-07-24 10:38:04 +010035 confirmation = await new confirmationMessage(interaction)
36 .setEmoji("PUNISH.KICK.RED")
37 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010038 .setDescription(
39 keyValueList({
PineaFana00db1b2023-01-02 15:32:54 +000040 user: renderUser(interaction.options.getUser("user")!),
Skyler Greyda16adf2023-03-05 10:22:12 +000041 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
42 }) + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010043 )
pineafan73a7c4a2022-07-24 10:38:04 +010044 .setColor("Danger")
PineaFana00db1b2023-01-02 15:32:54 +000045 .addCustomBoolean(
46 "notify",
47 "Notify user",
48 false,
49 null,
50 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000051 null,
PineaFana00db1b2023-01-02 15:32:54 +000052 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
53 notify
54 )
pineafan73a7c4a2022-07-24 10:38:04 +010055 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000056 .setFailedMessage("No changes were made", "Success", "PUNISH.KICK.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010057 .send(reason !== null);
58 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010059 if (confirmation.cancelled) timedOut = true;
PineaFana00db1b2023-01-02 15:32:54 +000060 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010061 else if (confirmation.newReason) reason = confirmation.newReason;
62 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010063 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010064 }
Skyler Greyda16adf2023-03-05 10:22:12 +000065 } while (!timedOut && !success);
PineaFan1dee28f2023-01-16 22:09:07 +000066 if (timedOut || !confirmation.success) return;
PineaFana00db1b2023-01-02 15:32:54 +000067 let dmSent = false;
68 let dmMessage;
69 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Greyad002172022-08-16 18:48:26 +010070 try {
71 if (notify) {
Skyler Greyda16adf2023-03-05 10:22:12 +000072 if (reason) {
73 reason = reason
74 .split("\n")
75 .map((line) => "> " + line)
76 .join("\n");
77 }
PineaFana00db1b2023-01-02 15:32:54 +000078 const messageData: {
79 embeds: EmojiEmbed[];
80 components: ActionRowBuilder<ButtonBuilder>[];
81 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010085 .setTitle("Kicked")
86 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000087 `You have been kicked from ${interaction.guild.name}` +
PineaFan538d3752023-01-12 21:48:23 +000088 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010089 )
Skyler Grey75ea9172022-08-06 10:22:23 +010090 .setStatus("Danger")
91 ],
PineaFana00db1b2023-01-02 15:32:54 +000092 components: []
93 };
94 if (config.moderation.kick.text && config.moderation.kick.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +000095 messageData.embeds[0]!.setFooter(LinkWarningFooter);
96 messageData.components.push(
97 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
98 new ButtonBuilder()
PineaFana00db1b2023-01-02 15:32:54 +000099 .setStyle(ButtonStyle.Link)
100 .setLabel(config.moderation.kick.text)
Skyler Greyda16adf2023-03-05 10:22:12 +0000101 .setURL(
102 config.moderation.kick.link.replaceAll(
103 "{id}",
104 (interaction.options.getMember("user") as GuildMember).id
105 )
106 )
107 )
108 );
PineaFana00db1b2023-01-02 15:32:54 +0000109 }
110 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
111 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000112 }
Skyler Greyad002172022-08-16 18:48:26 +0100113 } catch {
PineaFana00db1b2023-01-02 15:32:54 +0000114 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000115 }
Skyler Greyad002172022-08-16 18:48:26 +0100116 try {
PineaFana00db1b2023-01-02 15:32:54 +0000117 (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided");
Skyler Greyad002172022-08-16 18:48:26 +0100118 const member = interaction.options.getMember("user") as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000119 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100120 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +0000121 const timeInServer = member.joinedTimestamp
122 ? entry(
123 (Date.now() - member.joinedTimestamp).toString(),
124 humanizeDuration(Date.now() - member.joinedTimestamp, {
125 round: true
126 })
127 )
128 : entry(null, "*Unknown*");
Skyler Greyad002172022-08-16 18:48:26 +0100129 const data = {
130 meta: {
131 type: "memberKick",
132 displayName: "Member Kicked",
133 calculateType: "guildMemberPunish",
134 color: NucleusColors.red,
135 emoji: "PUNISH.KICK.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500136 timestamp: Date.now()
Skyler Greyad002172022-08-16 18:48:26 +0100137 },
138 list: {
139 memberId: entry(member.id, `\`${member.id}\``),
140 name: entry(member.id, renderUser(member.user)),
Skyler Greyda16adf2023-03-05 10:22:12 +0000141 joined: undefined as unknown | typeof entry,
TheCodedProf6ec331b2023-02-20 12:13:06 -0500142 kicked: entry(Date.now().toString(), renderDelta(Date.now())),
Skyler Greyad002172022-08-16 18:48:26 +0100143 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
144 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100145 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100146 serverMemberCount: member.guild.memberCount
147 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500148 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000149 end:
150 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
151 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500152 },
Skyler Greyad002172022-08-16 18:48:26 +0100153 hidden: {
154 guild: member.guild.id
155 }
156 };
PineaFana00db1b2023-01-02 15:32:54 +0000157 if (member.joinedTimestamp) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000158 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp));
PineaFana00db1b2023-01-02 15:32:54 +0000159 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000160 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100161 log(data);
162 } catch {
163 await interaction.editReply({
164 embeds: [
165 new EmojiEmbed()
166 .setEmoji("PUNISH.KICK.RED")
167 .setTitle("Kick")
168 .setDescription("Something went wrong and the user was not kicked")
169 .setStatus("Danger")
170 ],
171 components: []
172 });
PineaFana00db1b2023-01-02 15:32:54 +0000173 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100174 return;
175 }
PineaFana00db1b2023-01-02 15:32:54 +0000176 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100177 await interaction.editReply({
178 embeds: [
179 new EmojiEmbed()
180 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
181 .setTitle("Kick")
182 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
183 .setStatus(failed ? "Warning" : "Success")
184 ],
185 components: []
186 });
pineafan63fc5e22022-08-04 22:04:10 +0100187};
pineafan4f164f32022-02-26 22:07:12 +0000188
TheCodedProff86ba092023-01-27 17:10:07 -0500189const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000190 if (!interaction.guild) return;
TheCodedProff86ba092023-01-27 17:10:07 -0500191
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500193 // Check if the user has kick_members permission
194 if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission";
195 if (partial) return true;
196
PineaFana00db1b2023-01-02 15:32:54 +0000197 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100198 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100199 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
200 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
201 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
TheCodedProff86ba092023-01-27 17:10:07 -0500202 // Check if Nucleus has permission to kick
203 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
204 // Allow the owner to kick anyone
205 if (member.id === interaction.guild.ownerId) return true;
pineafanc1c18792022-08-03 21:41:36 +0100206 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000207 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000208 // Check if Nucleus can kick the member
TheCodedProf0941da42023-02-18 20:28:04 -0500209 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000210 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000211 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000212 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500213 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000214 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100215 return true;
216};
pineafan4f164f32022-02-26 22:07:12 +0000217
Skyler Grey75ea9172022-08-06 10:22:23 +0100218export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500219export const metadata = {
220 longDescription: "Removes a member from the server. They will be able to rejoin if they have an invite link.",
Skyler Greyda16adf2023-03-05 10:22:12 +0000221 premiumOnly: true
222};