blob: 2feb5d7b8f762bf3ca4c45e4b9b12cb8e5a18940 [file] [log] [blame]
PineaFana00db1b2023-01-02 15:32:54 +00001import { LinkWarningFooter } from './../../utils/defaultEmbeds';
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",
45 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
46 notify
47 )
pineafan73a7c4a2022-07-24 10:38:04 +010048 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010049 .send(reason !== null);
50 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010051 if (confirmation.cancelled) timedOut = true;
PineaFana00db1b2023-01-02 15:32:54 +000052 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010053 else if (confirmation.newReason) reason = confirmation.newReason;
54 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010055 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010056 }
PineaFana00db1b2023-01-02 15:32:54 +000057 } while (!timedOut && !success)
Skyler Greyad002172022-08-16 18:48:26 +010058 if (timedOut) return;
PineaFana00db1b2023-01-02 15:32:54 +000059 if (!confirmation.success) {
60 await interaction.editReply({
61 embeds: [
62 new EmojiEmbed()
63 .setEmoji("PUNISH.KICK.GREEN")
64 .setTitle("Kick")
65 .setDescription("No changes were made")
66 .setStatus("Success")
67 ],
68 components: []
69 });
70 return;
71 }
72 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) {
PineaFana00db1b2023-01-02 15:32:54 +000077 const messageData: {
78 embeds: EmojiEmbed[];
79 components: ActionRowBuilder<ButtonBuilder>[];
80 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 embeds: [
82 new EmojiEmbed()
83 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010084 .setTitle("Kicked")
85 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000086 `You have been kicked from ${interaction.guild.name}` +
87 (reason ? ` for:\n> ${reason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010088 )
Skyler Grey75ea9172022-08-06 10:22:23 +010089 .setStatus("Danger")
90 ],
PineaFana00db1b2023-01-02 15:32:54 +000091 components: []
92 };
93 if (config.moderation.kick.text && config.moderation.kick.link) {
94 messageData.embeds[0]!.setFooter(LinkWarningFooter)
95 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
96 .addComponents(new ButtonBuilder()
97 .setStyle(ButtonStyle.Link)
98 .setLabel(config.moderation.kick.text)
99 .setURL(config.moderation.kick.link)
100 )
101 )
102 }
103 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
104 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000105 }
Skyler Greyad002172022-08-16 18:48:26 +0100106 } catch {
PineaFana00db1b2023-01-02 15:32:54 +0000107 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000108 }
Skyler Greyad002172022-08-16 18:48:26 +0100109 try {
PineaFana00db1b2023-01-02 15:32:54 +0000110 (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided");
Skyler Greyad002172022-08-16 18:48:26 +0100111 const member = interaction.options.getMember("user") as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000112 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100113 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan62ce1922022-08-25 20:34:45 +0100114 const timeInServer = member.joinedTimestamp ? entry(
PineaFana00db1b2023-01-02 15:32:54 +0000115 (new Date().getTime() - member.joinedTimestamp).toString(),
pineafan62ce1922022-08-25 20:34:45 +0100116 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
117 round: true
118 })
119 ) : entry(null, "*Unknown*")
Skyler Greyad002172022-08-16 18:48:26 +0100120 const data = {
121 meta: {
122 type: "memberKick",
123 displayName: "Member Kicked",
124 calculateType: "guildMemberPunish",
125 color: NucleusColors.red,
126 emoji: "PUNISH.KICK.RED",
127 timestamp: new Date().getTime()
128 },
129 list: {
130 memberId: entry(member.id, `\`${member.id}\``),
131 name: entry(member.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000132 joined: undefined as (unknown | typeof entry),
133 kicked: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
Skyler Greyad002172022-08-16 18:48:26 +0100134 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
135 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100136 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100137 serverMemberCount: member.guild.memberCount
138 },
139 hidden: {
140 guild: member.guild.id
141 }
142 };
PineaFana00db1b2023-01-02 15:32:54 +0000143 if (member.joinedTimestamp) {
144 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp))
145 }
146 await client.database.history.create(
147 "kick",
148 interaction.guild.id,
149 member.user,
150 interaction.user,
151 reason
152 )
Skyler Greyad002172022-08-16 18:48:26 +0100153 log(data);
154 } catch {
155 await interaction.editReply({
156 embeds: [
157 new EmojiEmbed()
158 .setEmoji("PUNISH.KICK.RED")
159 .setTitle("Kick")
160 .setDescription("Something went wrong and the user was not kicked")
161 .setStatus("Danger")
162 ],
163 components: []
164 });
PineaFana00db1b2023-01-02 15:32:54 +0000165 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100166 return;
167 }
PineaFana00db1b2023-01-02 15:32:54 +0000168 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100169 await interaction.editReply({
170 embeds: [
171 new EmojiEmbed()
172 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
173 .setTitle("Kick")
174 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
175 .setStatus(failed ? "Warning" : "Success")
176 ],
177 components: []
178 });
pineafan63fc5e22022-08-04 22:04:10 +0100179};
pineafan4f164f32022-02-26 22:07:12 +0000180
pineafanbd02b4a2022-08-05 22:01:38 +0100181const check = (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000182 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000184 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100186 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
187 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
188 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100189 // Do not allow kicking the owner
PineaFana00db1b2023-01-02 15:32:54 +0000190 if (member.id === interaction.guild.ownerId) throw new Error("You cannot kick the owner of the server");
pineafan8b4b17f2022-02-27 20:42:52 +0000191 // Check if Nucleus can kick the member
pineafan3a02ea32022-08-11 21:35:04 +0100192 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000193 // Check if Nucleus has permission to kick
PineaFana00db1b2023-01-02 15:32:54 +0000194 if (!me.permissions.has("KickMembers")) throw new Error("I do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000195 // Do not allow kicking Nucleus
PineaFana00db1b2023-01-02 15:32:54 +0000196 if (member.id === interaction.guild.members.me!.id) throw new Error("I cannot kick myself");
pineafan8b4b17f2022-02-27 20:42:52 +0000197 // Allow the owner to kick anyone
PineaFana00db1b2023-01-02 15:32:54 +0000198 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000199 // Check if the user has kick_members permission
PineaFana00db1b2023-01-02 15:32:54 +0000200 if (!member.permissions.has("KickMembers")) throw new Error("You do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000201 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100202 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000203 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100204 return true;
205};
pineafan4f164f32022-02-26 22:07:12 +0000206
Skyler Grey75ea9172022-08-06 10:22:23 +0100207export { command, callback, check };