blob: dd71892fddac94af1571d97370277caf1d824fc0 [file] [log] [blame]
PineaFana34d04b2023-01-03 22:05:42 +00001import { LinkWarningFooter } from './../../utils/defaultEmbeds.js';
TheCodedProf21c08592022-09-13 14:14:43 -04002import { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
pineafan62ce1922022-08-25 20:34:45 +01003// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import humanizeDuration from "humanize-duration";
pineafan62ce1922022-08-25 20:34:45 +01005import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
PineaFana00db1b2023-01-02 15:32:54 +00006import type Discord from "discord.js";
pineafan8b4b17f2022-02-27 20:42:52 +00007import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01008import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00009import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +010010import client from "../../utils/client.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 ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010050 .send(reason !== null);
51 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010052 if (confirmation.cancelled) timedOut = true;
PineaFana00db1b2023-01-02 15:32:54 +000053 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010054 else if (confirmation.newReason) reason = confirmation.newReason;
55 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010056 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010057 }
PineaFana00db1b2023-01-02 15:32:54 +000058 } while (!timedOut && !success)
Skyler Greyad002172022-08-16 18:48:26 +010059 if (timedOut) return;
PineaFana00db1b2023-01-02 15:32:54 +000060 if (!confirmation.success) {
61 await interaction.editReply({
62 embeds: [
63 new EmojiEmbed()
64 .setEmoji("PUNISH.KICK.GREEN")
65 .setTitle("Kick")
66 .setDescription("No changes were made")
67 .setStatus("Success")
68 ],
69 components: []
70 });
71 return;
72 }
73 let dmSent = false;
74 let dmMessage;
75 const config = await client.database.guilds.read(interaction.guild.id);
Skyler Greyad002172022-08-16 18:48:26 +010076 try {
77 if (notify) {
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}` +
88 (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) {
95 messageData.embeds[0]!.setFooter(LinkWarningFooter)
96 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
97 .addComponents(new ButtonBuilder()
98 .setStyle(ButtonStyle.Link)
99 .setLabel(config.moderation.kick.text)
100 .setURL(config.moderation.kick.link)
101 )
102 )
103 }
104 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
105 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000106 }
Skyler Greyad002172022-08-16 18:48:26 +0100107 } catch {
PineaFana00db1b2023-01-02 15:32:54 +0000108 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000109 }
Skyler Greyad002172022-08-16 18:48:26 +0100110 try {
PineaFana00db1b2023-01-02 15:32:54 +0000111 (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided");
Skyler Greyad002172022-08-16 18:48:26 +0100112 const member = interaction.options.getMember("user") as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000113 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100114 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan62ce1922022-08-25 20:34:45 +0100115 const timeInServer = member.joinedTimestamp ? entry(
PineaFana00db1b2023-01-02 15:32:54 +0000116 (new Date().getTime() - member.joinedTimestamp).toString(),
pineafan62ce1922022-08-25 20:34:45 +0100117 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
118 round: true
119 })
120 ) : entry(null, "*Unknown*")
Skyler Greyad002172022-08-16 18:48:26 +0100121 const data = {
122 meta: {
123 type: "memberKick",
124 displayName: "Member Kicked",
125 calculateType: "guildMemberPunish",
126 color: NucleusColors.red,
127 emoji: "PUNISH.KICK.RED",
128 timestamp: new Date().getTime()
129 },
130 list: {
131 memberId: entry(member.id, `\`${member.id}\``),
132 name: entry(member.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000133 joined: undefined as (unknown | typeof entry),
134 kicked: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
Skyler Greyad002172022-08-16 18:48:26 +0100135 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
136 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100137 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100138 serverMemberCount: member.guild.memberCount
139 },
140 hidden: {
141 guild: member.guild.id
142 }
143 };
PineaFana00db1b2023-01-02 15:32:54 +0000144 if (member.joinedTimestamp) {
145 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp))
146 }
147 await client.database.history.create(
148 "kick",
149 interaction.guild.id,
150 member.user,
151 interaction.user,
152 reason
153 )
Skyler Greyad002172022-08-16 18:48:26 +0100154 log(data);
155 } catch {
156 await interaction.editReply({
157 embeds: [
158 new EmojiEmbed()
159 .setEmoji("PUNISH.KICK.RED")
160 .setTitle("Kick")
161 .setDescription("Something went wrong and the user was not kicked")
162 .setStatus("Danger")
163 ],
164 components: []
165 });
PineaFana00db1b2023-01-02 15:32:54 +0000166 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100167 return;
168 }
PineaFana00db1b2023-01-02 15:32:54 +0000169 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100170 await interaction.editReply({
171 embeds: [
172 new EmojiEmbed()
173 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
174 .setTitle("Kick")
175 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
176 .setStatus(failed ? "Warning" : "Success")
177 ],
178 components: []
179 });
pineafan63fc5e22022-08-04 22:04:10 +0100180};
pineafan4f164f32022-02-26 22:07:12 +0000181
pineafanbd02b4a2022-08-05 22:01:38 +0100182const check = (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000183 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000185 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100187 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
188 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
189 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100190 // Do not allow kicking the owner
PineaFana00db1b2023-01-02 15:32:54 +0000191 if (member.id === interaction.guild.ownerId) throw new Error("You cannot kick the owner of the server");
pineafan8b4b17f2022-02-27 20:42:52 +0000192 // Check if Nucleus can kick the member
pineafan3a02ea32022-08-11 21:35:04 +0100193 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000194 // Check if Nucleus has permission to kick
PineaFana00db1b2023-01-02 15:32:54 +0000195 if (!me.permissions.has("KickMembers")) throw new Error("I do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000196 // Do not allow kicking Nucleus
PineaFana00db1b2023-01-02 15:32:54 +0000197 if (member.id === interaction.guild.members.me!.id) throw new Error("I cannot kick myself");
pineafan8b4b17f2022-02-27 20:42:52 +0000198 // Allow the owner to kick anyone
PineaFana00db1b2023-01-02 15:32:54 +0000199 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000200 // Check if the user has kick_members permission
PineaFana00db1b2023-01-02 15:32:54 +0000201 if (!member.permissions.has("KickMembers")) throw new Error("You do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000202 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100203 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000204 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100205 return true;
206};
pineafan4f164f32022-02-26 22:07:12 +0000207
Skyler Grey75ea9172022-08-06 10:22:23 +0100208export { command, callback, check };