blob: 4d325a2f1b666a173793788025c08ba52a7e356c [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)
Skyler Greyda16adf2023-03-05 10:22:12 +0000106 .setURL(
107 config.moderation.kick.link.replaceAll(
108 "{id}",
pineafan6de4da52023-03-07 20:43:44 +0000109 member.id
Skyler Greyda16adf2023-03-05 10:22:12 +0000110 )
111 )
112 )
113 );
PineaFana00db1b2023-01-02 15:32:54 +0000114 }
pineafan6de4da52023-03-07 20:43:44 +0000115 dmMessage = await member.send(messageData);
PineaFana00db1b2023-01-02 15:32:54 +0000116 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000117 }
Skyler Greyad002172022-08-16 18:48:26 +0100118 } catch {
PineaFana00db1b2023-01-02 15:32:54 +0000119 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000120 }
Skyler Greyad002172022-08-16 18:48:26 +0100121 try {
pineafan6de4da52023-03-07 20:43:44 +0000122 member.kick(reason || "No reason provided");
PineaFana00db1b2023-01-02 15:32:54 +0000123 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100124 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +0000125 const timeInServer = member.joinedTimestamp
126 ? entry(
127 (Date.now() - member.joinedTimestamp).toString(),
128 humanizeDuration(Date.now() - member.joinedTimestamp, {
129 round: true
130 })
131 )
132 : entry(null, "*Unknown*");
Skyler Greyad002172022-08-16 18:48:26 +0100133 const data = {
134 meta: {
135 type: "memberKick",
136 displayName: "Member Kicked",
137 calculateType: "guildMemberPunish",
138 color: NucleusColors.red,
139 emoji: "PUNISH.KICK.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500140 timestamp: Date.now()
Skyler Greyad002172022-08-16 18:48:26 +0100141 },
142 list: {
143 memberId: entry(member.id, `\`${member.id}\``),
144 name: entry(member.id, renderUser(member.user)),
Skyler Greyda16adf2023-03-05 10:22:12 +0000145 joined: undefined as unknown | typeof entry,
TheCodedProf6ec331b2023-02-20 12:13:06 -0500146 kicked: entry(Date.now().toString(), renderDelta(Date.now())),
Skyler Greyad002172022-08-16 18:48:26 +0100147 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
148 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100149 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100150 serverMemberCount: member.guild.memberCount
151 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500152 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000153 end:
154 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
155 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500156 },
Skyler Greyad002172022-08-16 18:48:26 +0100157 hidden: {
158 guild: member.guild.id
159 }
160 };
PineaFana00db1b2023-01-02 15:32:54 +0000161 if (member.joinedTimestamp) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000162 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp));
PineaFana00db1b2023-01-02 15:32:54 +0000163 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000164 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100165 log(data);
166 } catch {
167 await interaction.editReply({
168 embeds: [
169 new EmojiEmbed()
170 .setEmoji("PUNISH.KICK.RED")
171 .setTitle("Kick")
172 .setDescription("Something went wrong and the user was not kicked")
173 .setStatus("Danger")
174 ],
175 components: []
176 });
PineaFana00db1b2023-01-02 15:32:54 +0000177 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100178 return;
179 }
PineaFana00db1b2023-01-02 15:32:54 +0000180 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100181 await interaction.editReply({
182 embeds: [
183 new EmojiEmbed()
184 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
185 .setTitle("Kick")
186 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
187 .setStatus(failed ? "Warning" : "Success")
188 ],
189 components: []
190 });
pineafan63fc5e22022-08-04 22:04:10 +0100191};
pineafan4f164f32022-02-26 22:07:12 +0000192
pineafan6de4da52023-03-07 20:43:44 +0000193const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
PineaFana00db1b2023-01-02 15:32:54 +0000194 if (!interaction.guild) return;
TheCodedProff86ba092023-01-27 17:10:07 -0500195
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500197 // Check if the user has kick_members permission
198 if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission";
199 if (partial) return true;
200
PineaFana00db1b2023-01-02 15:32:54 +0000201 const me = interaction.guild.members.me!;
pineafan6de4da52023-03-07 20:43:44 +0000202 let apply: GuildMember;
203 if (interaction.isButton()) {
204 apply = target!;
205 } else {
206 apply = interaction.options.getMember("user") as GuildMember;
207 }
pineafan62ce1922022-08-25 20:34:45 +0100208 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
209 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
210 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
TheCodedProff86ba092023-01-27 17:10:07 -0500211 // Check if Nucleus has permission to kick
212 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
213 // Allow the owner to kick anyone
214 if (member.id === interaction.guild.ownerId) return true;
pineafanc1c18792022-08-03 21:41:36 +0100215 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000216 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000217 // Check if Nucleus can kick the member
TheCodedProf0941da42023-02-18 20:28:04 -0500218 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000219 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000220 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000221 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500222 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000223 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100224 return true;
225};
pineafan4f164f32022-02-26 22:07:12 +0000226
Skyler Grey75ea9172022-08-06 10:22:23 +0100227export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500228export const metadata = {
229 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 +0000230 premiumOnly: true
231};