blob: 380bcc9563f00659274f87d039a970813e9735b9 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LinkWarningFooter } from '../../utils/defaults.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 ?? "")
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(
PineaFana00db1b2023-01-02 15:32:54 +0000105 (new Date().getTime() - member.joinedTimestamp).toString(),
pineafan62ce1922022-08-25 20:34:45 +0100106 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
107 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",
117 timestamp: new Date().getTime()
118 },
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),
123 kicked: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
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 },
129 hidden: {
130 guild: member.guild.id
131 }
132 };
PineaFana00db1b2023-01-02 15:32:54 +0000133 if (member.joinedTimestamp) {
134 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp))
135 }
136 await client.database.history.create(
137 "kick",
138 interaction.guild.id,
139 member.user,
140 interaction.user,
141 reason
142 )
Skyler Greyad002172022-08-16 18:48:26 +0100143 log(data);
144 } catch {
145 await interaction.editReply({
146 embeds: [
147 new EmojiEmbed()
148 .setEmoji("PUNISH.KICK.RED")
149 .setTitle("Kick")
150 .setDescription("Something went wrong and the user was not kicked")
151 .setStatus("Danger")
152 ],
153 components: []
154 });
PineaFana00db1b2023-01-02 15:32:54 +0000155 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100156 return;
157 }
PineaFana00db1b2023-01-02 15:32:54 +0000158 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100159 await interaction.editReply({
160 embeds: [
161 new EmojiEmbed()
162 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
163 .setTitle("Kick")
164 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
165 .setStatus(failed ? "Warning" : "Success")
166 ],
167 components: []
168 });
pineafan63fc5e22022-08-04 22:04:10 +0100169};
pineafan4f164f32022-02-26 22:07:12 +0000170
pineafanbd02b4a2022-08-05 22:01:38 +0100171const check = (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000172 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100173 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000174 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100175 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100176 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
177 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
178 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100179 // Do not allow kicking the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000180 if (member.id === interaction.guild.ownerId) return "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000181 // Check if Nucleus can kick the member
PineaFan0d06edc2023-01-17 22:10:31 +0000182 if (!(mePos > applyPos)) return "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000183 // Check if Nucleus has permission to kick
PineaFan0d06edc2023-01-17 22:10:31 +0000184 if (!me.permissions.has("KickMembers")) return "I do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000185 // Do not allow kicking Nucleus
PineaFan0d06edc2023-01-17 22:10:31 +0000186 if (member.id === interaction.guild.members.me!.id) return "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000187 // Allow the owner to kick anyone
PineaFana00db1b2023-01-02 15:32:54 +0000188 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000189 // Check if the user has kick_members permission
PineaFan0d06edc2023-01-17 22:10:31 +0000190 if (!member.permissions.has("KickMembers")) return "You do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000191 // Check if the user is below on the role list
PineaFan0d06edc2023-01-17 22:10:31 +0000192 if (!(memberPos > applyPos)) return "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000193 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100194 return true;
195};
pineafan4f164f32022-02-26 22:07:12 +0000196
Skyler Grey75ea9172022-08-06 10:22:23 +0100197export { command, callback, check };