blob: 059bdb2c37ec218ce4d929dd2a4a9178673c71cf [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";
TheCodedProf94ff6de2023-02-22 17:47:26 -050010import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +000011
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan63fc5e22022-08-04 22:04:10 +010014 .setName("kick")
15 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010016 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000017
pineafan3a02ea32022-08-11 21:35:04 +010018const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000019 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010020 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000021 // TODO:[Modals] Replace this with a modal
PineaFana00db1b2023-01-02 15:32:54 +000022 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010023 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010024 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010025 let timedOut = false;
PineaFana00db1b2023-01-02 15:32:54 +000026 let success = false;
27 do {
pineafan73a7c4a2022-07-24 10:38:04 +010028 confirmation = await new confirmationMessage(interaction)
29 .setEmoji("PUNISH.KICK.RED")
30 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010031 .setDescription(
32 keyValueList({
PineaFana00db1b2023-01-02 15:32:54 +000033 user: renderUser(interaction.options.getUser("user")!),
pineafan62ce1922022-08-25 20:34:45 +010034 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010035 }) +
Skyler Grey11236ba2022-08-08 21:13:33 +010036 `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010037 )
pineafan73a7c4a2022-07-24 10:38:04 +010038 .setColor("Danger")
PineaFana00db1b2023-01-02 15:32:54 +000039 .addCustomBoolean(
40 "notify",
41 "Notify user",
42 false,
43 null,
44 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000045 null,
PineaFana00db1b2023-01-02 15:32:54 +000046 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
47 notify
48 )
pineafan73a7c4a2022-07-24 10:38:04 +010049 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000050 .setFailedMessage("No changes were made", "Success", "PUNISH.KICK.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010051 .send(reason !== null);
52 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010053 if (confirmation.cancelled) timedOut = true;
PineaFana00db1b2023-01-02 15:32:54 +000054 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010055 else if (confirmation.newReason) reason = confirmation.newReason;
56 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010057 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010058 }
PineaFana00db1b2023-01-02 15:32:54 +000059 } while (!timedOut && !success)
PineaFan1dee28f2023-01-16 22:09:07 +000060 if (timedOut || !confirmation.success) return;
PineaFana00db1b2023-01-02 15:32:54 +000061 let dmSent = false;
62 let dmMessage;
63 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Greyad002172022-08-16 18:48:26 +010064 try {
65 if (notify) {
PineaFan538d3752023-01-12 21:48:23 +000066 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
PineaFana00db1b2023-01-02 15:32:54 +000067 const messageData: {
68 embeds: EmojiEmbed[];
69 components: ActionRowBuilder<ButtonBuilder>[];
70 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010071 embeds: [
72 new EmojiEmbed()
73 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010074 .setTitle("Kicked")
75 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000076 `You have been kicked from ${interaction.guild.name}` +
PineaFan538d3752023-01-12 21:48:23 +000077 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010078 )
Skyler Grey75ea9172022-08-06 10:22:23 +010079 .setStatus("Danger")
80 ],
PineaFana00db1b2023-01-02 15:32:54 +000081 components: []
82 };
83 if (config.moderation.kick.text && config.moderation.kick.link) {
84 messageData.embeds[0]!.setFooter(LinkWarningFooter)
85 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
86 .addComponents(new ButtonBuilder()
87 .setStyle(ButtonStyle.Link)
88 .setLabel(config.moderation.kick.text)
PineaFan9b2ac4d2023-01-18 14:41:07 +000089 .setURL(config.moderation.kick.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
PineaFana00db1b2023-01-02 15:32:54 +000090 )
91 )
92 }
93 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
94 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +000095 }
Skyler Greyad002172022-08-16 18:48:26 +010096 } catch {
PineaFana00db1b2023-01-02 15:32:54 +000097 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +000098 }
Skyler Greyad002172022-08-16 18:48:26 +010099 try {
PineaFana00db1b2023-01-02 15:32:54 +0000100 (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided");
Skyler Greyad002172022-08-16 18:48:26 +0100101 const member = interaction.options.getMember("user") as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000102 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100103 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan62ce1922022-08-25 20:34:45 +0100104 const timeInServer = member.joinedTimestamp ? entry(
TheCodedProf6ec331b2023-02-20 12:13:06 -0500105 (Date.now() - member.joinedTimestamp).toString(),
106 humanizeDuration(Date.now() - member.joinedTimestamp, {
pineafan62ce1922022-08-25 20:34:45 +0100107 round: true
108 })
109 ) : entry(null, "*Unknown*")
Skyler Greyad002172022-08-16 18:48:26 +0100110 const data = {
111 meta: {
112 type: "memberKick",
113 displayName: "Member Kicked",
114 calculateType: "guildMemberPunish",
115 color: NucleusColors.red,
116 emoji: "PUNISH.KICK.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500117 timestamp: Date.now()
Skyler Greyad002172022-08-16 18:48:26 +0100118 },
119 list: {
120 memberId: entry(member.id, `\`${member.id}\``),
121 name: entry(member.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000122 joined: undefined as (unknown | typeof entry),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500123 kicked: entry(Date.now().toString(), renderDelta(Date.now())),
Skyler Greyad002172022-08-16 18:48:26 +0100124 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
125 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100126 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100127 serverMemberCount: member.guild.memberCount
128 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500129 separate: {
130 end: getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + ` The user was ${notify ? "" : "not "}notified`
131 },
Skyler Greyad002172022-08-16 18:48:26 +0100132 hidden: {
133 guild: member.guild.id
134 }
135 };
PineaFana00db1b2023-01-02 15:32:54 +0000136 if (member.joinedTimestamp) {
137 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp))
138 }
139 await client.database.history.create(
140 "kick",
141 interaction.guild.id,
142 member.user,
143 interaction.user,
144 reason
145 )
Skyler Greyad002172022-08-16 18:48:26 +0100146 log(data);
147 } catch {
148 await interaction.editReply({
149 embeds: [
150 new EmojiEmbed()
151 .setEmoji("PUNISH.KICK.RED")
152 .setTitle("Kick")
153 .setDescription("Something went wrong and the user was not kicked")
154 .setStatus("Danger")
155 ],
156 components: []
157 });
PineaFana00db1b2023-01-02 15:32:54 +0000158 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100159 return;
160 }
PineaFana00db1b2023-01-02 15:32:54 +0000161 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100162 await interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
166 .setTitle("Kick")
167 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
168 .setStatus(failed ? "Warning" : "Success")
169 ],
170 components: []
171 });
pineafan63fc5e22022-08-04 22:04:10 +0100172};
pineafan4f164f32022-02-26 22:07:12 +0000173
TheCodedProff86ba092023-01-27 17:10:07 -0500174const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000175 if (!interaction.guild) return;
TheCodedProff86ba092023-01-27 17:10:07 -0500176
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500178 // Check if the user has kick_members permission
179 if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission";
180 if (partial) return true;
181
PineaFana00db1b2023-01-02 15:32:54 +0000182 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100184 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
185 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
186 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
TheCodedProff86ba092023-01-27 17:10:07 -0500187 // Check if Nucleus has permission to kick
188 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
189 // Allow the owner to kick anyone
190 if (member.id === interaction.guild.ownerId) return true;
pineafanc1c18792022-08-03 21:41:36 +0100191 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000192 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000193 // Check if Nucleus can kick the member
TheCodedProf0941da42023-02-18 20:28:04 -0500194 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000195 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000196 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000197 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500198 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000199 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100200 return true;
201};
pineafan4f164f32022-02-26 22:07:12 +0000202
Skyler Grey75ea9172022-08-06 10:22:23 +0100203export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500204export const metadata = {
205 longDescription: "Removes a member from the server. They will be able to rejoin if they have an invite link.",
206 premiumOnly: true,
207}