blob: 9ed4bb3348c44a11a2d2e73c07261872d1987fc1 [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) {
PineaFana6e37932023-09-25 22:09:08 +010077 let formattedReason: string | null = null;
Skyler Greyda16adf2023-03-05 10:22:12 +000078 if (reason) {
PineaFana6e37932023-09-25 22:09:08 +010079 formattedReason = reason
Skyler Greyda16adf2023-03-05 10:22:12 +000080 .split("\n")
81 .map((line) => "> " + line)
82 .join("\n");
83 }
PineaFana00db1b2023-01-02 15:32:54 +000084 const messageData: {
85 embeds: EmojiEmbed[];
86 components: ActionRowBuilder<ButtonBuilder>[];
87 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010088 embeds: [
89 new EmojiEmbed()
90 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010091 .setTitle("Kicked")
92 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000093 `You have been kicked from ${interaction.guild.name}` +
PineaFana6e37932023-09-25 22:09:08 +010094 (formattedReason ? ` for:\n${formattedReason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010095 )
Skyler Grey75ea9172022-08-06 10:22:23 +010096 .setStatus("Danger")
97 ],
PineaFana00db1b2023-01-02 15:32:54 +000098 components: []
99 };
100 if (config.moderation.kick.text && config.moderation.kick.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000101 messageData.embeds[0]!.setFooter(LinkWarningFooter);
102 messageData.components.push(
103 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
104 new ButtonBuilder()
PineaFana00db1b2023-01-02 15:32:54 +0000105 .setStyle(ButtonStyle.Link)
106 .setLabel(config.moderation.kick.text)
pineafan1e462ab2023-03-07 21:34:06 +0000107 .setURL(config.moderation.kick.link.replaceAll("{id}", member.id))
Skyler Greyda16adf2023-03-05 10:22:12 +0000108 )
109 );
PineaFana00db1b2023-01-02 15:32:54 +0000110 }
pineafan6de4da52023-03-07 20:43:44 +0000111 dmMessage = await member.send(messageData);
PineaFana00db1b2023-01-02 15:32:54 +0000112 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000113 }
Skyler Greyad002172022-08-16 18:48:26 +0100114 } catch {
PineaFana00db1b2023-01-02 15:32:54 +0000115 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000116 }
Skyler Greyad002172022-08-16 18:48:26 +0100117 try {
Skyler Greyf4f21c42023-03-08 14:36:29 +0000118 await member.kick(reason || "No reason provided");
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 Greyf4f21c42023-03-08 14:36:29 +0000161 await log(data);
Skyler Greyad002172022-08-16 18:48:26 +0100162 } 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
pineafan6de4da52023-03-07 20:43:44 +0000189const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
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!;
pineafan6de4da52023-03-07 20:43:44 +0000198 let apply: GuildMember;
199 if (interaction.isButton()) {
200 apply = target!;
201 } else {
202 apply = interaction.options.getMember("user") as GuildMember;
203 }
pineafan62ce1922022-08-25 20:34:45 +0100204 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
205 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
206 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
TheCodedProff86ba092023-01-27 17:10:07 -0500207 // Check if Nucleus has permission to kick
208 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
209 // Allow the owner to kick anyone
210 if (member.id === interaction.guild.ownerId) return true;
pineafanc1c18792022-08-03 21:41:36 +0100211 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000212 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000213 // Check if Nucleus can kick the member
TheCodedProf0941da42023-02-18 20:28:04 -0500214 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000215 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000216 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000217 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500218 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000219 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100220 return true;
221};
pineafan4f164f32022-02-26 22:07:12 +0000222
Skyler Grey75ea9172022-08-06 10:22:23 +0100223export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500224export const metadata = {
225 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 +0000226 premiumOnly: true
227};