blob: 4ef78c81cfe19e2f8bb667ad8fbea5cd6172ec9f [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,
pineafan6de4da52023-03-07 20:43:44 +00008 SlashCommandSubcommandBuilder,
9 ButtonInteraction
Skyler Greyda16adf2023-03-05 10:22:12 +000010} from "discord.js";
pineafan62ce1922022-08-25 20:34:45 +010011// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +010012import humanizeDuration from "humanize-duration";
PineaFana00db1b2023-01-02 15:32:54 +000013import type Discord from "discord.js";
pineafan8b4b17f2022-02-27 20:42:52 +000014import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +010015import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +000016import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +010017import client from "../../utils/client.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -050018import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +000019
20const command = (builder: SlashCommandSubcommandBuilder) =>
21 builder
pineafan63fc5e22022-08-04 22:04:10 +010022 .setName("kick")
23 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010024 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000025
pineafan6de4da52023-03-07 20:43:44 +000026const callback = async (interaction: CommandInteraction | ButtonInteraction, member?: GuildMember): Promise<void> => {
PineaFana00db1b2023-01-02 15:32:54 +000027 if (!interaction.guild) return;
pineafan6de4da52023-03-07 20:43:44 +000028 if (!interaction.isButton()) {
29 member = interaction.options.getMember("user") as GuildMember;
30 }
31 if (!member) return;
pineafan63fc5e22022-08-04 22:04:10 +010032 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000033 // TODO:[Modals] Replace this with a modal
PineaFana00db1b2023-01-02 15:32:54 +000034 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010035 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010036 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010037 let timedOut = false;
PineaFana00db1b2023-01-02 15:32:54 +000038 let success = false;
39 do {
pineafan73a7c4a2022-07-24 10:38:04 +010040 confirmation = await new confirmationMessage(interaction)
41 .setEmoji("PUNISH.KICK.RED")
42 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010043 .setDescription(
44 keyValueList({
pineafan6de4da52023-03-07 20:43:44 +000045 user: renderUser(member.user),
Skyler Greyda16adf2023-03-05 10:22:12 +000046 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
pineafan6de4da52023-03-07 20:43:44 +000047 }) + `Are you sure you want to kick <@!${member.id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010048 )
pineafan73a7c4a2022-07-24 10:38:04 +010049 .setColor("Danger")
PineaFana00db1b2023-01-02 15:32:54 +000050 .addCustomBoolean(
51 "notify",
52 "Notify user",
53 false,
54 null,
55 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000056 null,
PineaFana00db1b2023-01-02 15:32:54 +000057 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
58 notify
59 )
pineafan73a7c4a2022-07-24 10:38:04 +010060 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000061 .setFailedMessage("No changes were made", "Success", "PUNISH.KICK.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010062 .send(reason !== null);
63 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010064 if (confirmation.cancelled) timedOut = true;
PineaFana00db1b2023-01-02 15:32:54 +000065 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010066 else if (confirmation.newReason) reason = confirmation.newReason;
67 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010068 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010069 }
Skyler Greyda16adf2023-03-05 10:22:12 +000070 } while (!timedOut && !success);
PineaFan1dee28f2023-01-16 22:09:07 +000071 if (timedOut || !confirmation.success) return;
PineaFana00db1b2023-01-02 15:32:54 +000072 let dmSent = false;
73 let dmMessage;
74 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Greyad002172022-08-16 18:48:26 +010075 try {
76 if (notify) {
Skyler Greyda16adf2023-03-05 10:22:12 +000077 if (reason) {
78 reason = reason
79 .split("\n")
80 .map((line) => "> " + line)
81 .join("\n");
82 }
PineaFana00db1b2023-01-02 15:32:54 +000083 const messageData: {
84 embeds: EmojiEmbed[];
85 components: ActionRowBuilder<ButtonBuilder>[];
86 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010087 embeds: [
88 new EmojiEmbed()
89 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010090 .setTitle("Kicked")
91 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000092 `You have been kicked from ${interaction.guild.name}` +
PineaFan538d3752023-01-12 21:48:23 +000093 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010094 )
Skyler Grey75ea9172022-08-06 10:22:23 +010095 .setStatus("Danger")
96 ],
PineaFana00db1b2023-01-02 15:32:54 +000097 components: []
98 };
99 if (config.moderation.kick.text && config.moderation.kick.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000100 messageData.embeds[0]!.setFooter(LinkWarningFooter);
101 messageData.components.push(
102 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
103 new ButtonBuilder()
PineaFana00db1b2023-01-02 15:32:54 +0000104 .setStyle(ButtonStyle.Link)
105 .setLabel(config.moderation.kick.text)
pineafan1e462ab2023-03-07 21:34:06 +0000106 .setURL(config.moderation.kick.link.replaceAll("{id}", member.id))
Skyler Greyda16adf2023-03-05 10:22:12 +0000107 )
108 );
PineaFana00db1b2023-01-02 15:32:54 +0000109 }
pineafan6de4da52023-03-07 20:43:44 +0000110 dmMessage = await member.send(messageData);
PineaFana00db1b2023-01-02 15:32:54 +0000111 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 {
pineafan6de4da52023-03-07 20:43:44 +0000117 member.kick(reason || "No reason provided");
PineaFana00db1b2023-01-02 15:32:54 +0000118 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100119 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +0000120 const timeInServer = member.joinedTimestamp
121 ? entry(
122 (Date.now() - member.joinedTimestamp).toString(),
123 humanizeDuration(Date.now() - member.joinedTimestamp, {
124 round: true
125 })
126 )
127 : entry(null, "*Unknown*");
Skyler Greyad002172022-08-16 18:48:26 +0100128 const data = {
129 meta: {
130 type: "memberKick",
131 displayName: "Member Kicked",
132 calculateType: "guildMemberPunish",
133 color: NucleusColors.red,
134 emoji: "PUNISH.KICK.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500135 timestamp: Date.now()
Skyler Greyad002172022-08-16 18:48:26 +0100136 },
137 list: {
138 memberId: entry(member.id, `\`${member.id}\``),
139 name: entry(member.id, renderUser(member.user)),
Skyler Greyda16adf2023-03-05 10:22:12 +0000140 joined: undefined as unknown | typeof entry,
TheCodedProf6ec331b2023-02-20 12:13:06 -0500141 kicked: entry(Date.now().toString(), renderDelta(Date.now())),
Skyler Greyad002172022-08-16 18:48:26 +0100142 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
143 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100144 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100145 serverMemberCount: member.guild.memberCount
146 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500147 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000148 end:
149 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
150 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500151 },
Skyler Greyad002172022-08-16 18:48:26 +0100152 hidden: {
153 guild: member.guild.id
154 }
155 };
PineaFana00db1b2023-01-02 15:32:54 +0000156 if (member.joinedTimestamp) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000157 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp));
PineaFana00db1b2023-01-02 15:32:54 +0000158 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000159 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100160 log(data);
161 } catch {
162 await interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setEmoji("PUNISH.KICK.RED")
166 .setTitle("Kick")
167 .setDescription("Something went wrong and the user was not kicked")
168 .setStatus("Danger")
169 ],
170 components: []
171 });
PineaFana00db1b2023-01-02 15:32:54 +0000172 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100173 return;
174 }
PineaFana00db1b2023-01-02 15:32:54 +0000175 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100176 await interaction.editReply({
177 embeds: [
178 new EmojiEmbed()
179 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
180 .setTitle("Kick")
181 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
182 .setStatus(failed ? "Warning" : "Success")
183 ],
184 components: []
185 });
pineafan63fc5e22022-08-04 22:04:10 +0100186};
pineafan4f164f32022-02-26 22:07:12 +0000187
pineafan6de4da52023-03-07 20:43:44 +0000188const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
PineaFana00db1b2023-01-02 15:32:54 +0000189 if (!interaction.guild) return;
TheCodedProff86ba092023-01-27 17:10:07 -0500190
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500192 // Check if the user has kick_members permission
193 if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission";
194 if (partial) return true;
195
PineaFana00db1b2023-01-02 15:32:54 +0000196 const me = interaction.guild.members.me!;
pineafan6de4da52023-03-07 20:43:44 +0000197 let apply: GuildMember;
198 if (interaction.isButton()) {
199 apply = target!;
200 } else {
201 apply = interaction.options.getMember("user") as GuildMember;
202 }
pineafan62ce1922022-08-25 20:34:45 +0100203 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
204 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
205 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
TheCodedProff86ba092023-01-27 17:10:07 -0500206 // Check if Nucleus has permission to kick
207 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
208 // Allow the owner to kick anyone
209 if (member.id === interaction.guild.ownerId) return true;
pineafanc1c18792022-08-03 21:41:36 +0100210 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000211 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000212 // Check if Nucleus can kick the member
TheCodedProf0941da42023-02-18 20:28:04 -0500213 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000214 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000215 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000216 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500217 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000218 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100219 return true;
220};
pineafan4f164f32022-02-26 22:07:12 +0000221
Skyler Grey75ea9172022-08-06 10:22:23 +0100222export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500223export const metadata = {
224 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 +0000225 premiumOnly: true
226};