blob: 88b6e14950abc1b45e39f67127c72ca9c710248d [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) {
PineaFan538d3752023-01-12 21:48:23 +000078 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
PineaFana00db1b2023-01-02 15:32:54 +000079 const messageData: {
80 embeds: EmojiEmbed[];
81 components: ActionRowBuilder<ButtonBuilder>[];
82 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 embeds: [
84 new EmojiEmbed()
85 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010086 .setTitle("Kicked")
87 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000088 `You have been kicked from ${interaction.guild.name}` +
PineaFan538d3752023-01-12 21:48:23 +000089 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
Skyler Greyad002172022-08-16 18:48:26 +010090 )
Skyler Grey75ea9172022-08-06 10:22:23 +010091 .setStatus("Danger")
92 ],
PineaFana00db1b2023-01-02 15:32:54 +000093 components: []
94 };
95 if (config.moderation.kick.text && config.moderation.kick.link) {
96 messageData.embeds[0]!.setFooter(LinkWarningFooter)
97 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
98 .addComponents(new ButtonBuilder()
99 .setStyle(ButtonStyle.Link)
100 .setLabel(config.moderation.kick.text)
101 .setURL(config.moderation.kick.link)
102 )
103 )
104 }
105 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
106 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000107 }
Skyler Greyad002172022-08-16 18:48:26 +0100108 } catch {
PineaFana00db1b2023-01-02 15:32:54 +0000109 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000110 }
Skyler Greyad002172022-08-16 18:48:26 +0100111 try {
PineaFana00db1b2023-01-02 15:32:54 +0000112 (interaction.options.getMember("user") as GuildMember).kick(reason || "No reason provided");
Skyler Greyad002172022-08-16 18:48:26 +0100113 const member = interaction.options.getMember("user") as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000114 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +0100115 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan62ce1922022-08-25 20:34:45 +0100116 const timeInServer = member.joinedTimestamp ? entry(
PineaFana00db1b2023-01-02 15:32:54 +0000117 (new Date().getTime() - member.joinedTimestamp).toString(),
pineafan62ce1922022-08-25 20:34:45 +0100118 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
119 round: true
120 })
121 ) : entry(null, "*Unknown*")
Skyler Greyad002172022-08-16 18:48:26 +0100122 const data = {
123 meta: {
124 type: "memberKick",
125 displayName: "Member Kicked",
126 calculateType: "guildMemberPunish",
127 color: NucleusColors.red,
128 emoji: "PUNISH.KICK.RED",
129 timestamp: new Date().getTime()
130 },
131 list: {
132 memberId: entry(member.id, `\`${member.id}\``),
133 name: entry(member.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000134 joined: undefined as (unknown | typeof entry),
135 kicked: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
Skyler Greyad002172022-08-16 18:48:26 +0100136 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
137 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100138 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100139 serverMemberCount: member.guild.memberCount
140 },
141 hidden: {
142 guild: member.guild.id
143 }
144 };
PineaFana00db1b2023-01-02 15:32:54 +0000145 if (member.joinedTimestamp) {
146 data.list.joined = entry(member.joinedTimestamp.toString(), renderDelta(member.joinedTimestamp))
147 }
148 await client.database.history.create(
149 "kick",
150 interaction.guild.id,
151 member.user,
152 interaction.user,
153 reason
154 )
Skyler Greyad002172022-08-16 18:48:26 +0100155 log(data);
156 } catch {
157 await interaction.editReply({
158 embeds: [
159 new EmojiEmbed()
160 .setEmoji("PUNISH.KICK.RED")
161 .setTitle("Kick")
162 .setDescription("Something went wrong and the user was not kicked")
163 .setStatus("Danger")
164 ],
165 components: []
166 });
PineaFana00db1b2023-01-02 15:32:54 +0000167 if (dmSent && dmMessage) await dmMessage.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100168 return;
169 }
PineaFana00db1b2023-01-02 15:32:54 +0000170 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100171 await interaction.editReply({
172 embeds: [
173 new EmojiEmbed()
174 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
175 .setTitle("Kick")
176 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
177 .setStatus(failed ? "Warning" : "Success")
178 ],
179 components: []
180 });
pineafan63fc5e22022-08-04 22:04:10 +0100181};
pineafan4f164f32022-02-26 22:07:12 +0000182
pineafanbd02b4a2022-08-05 22:01:38 +0100183const check = (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000184 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000186 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100187 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100188 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
189 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
190 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100191 // Do not allow kicking the owner
PineaFana00db1b2023-01-02 15:32:54 +0000192 if (member.id === interaction.guild.ownerId) throw new Error("You cannot kick the owner of the server");
pineafan8b4b17f2022-02-27 20:42:52 +0000193 // Check if Nucleus can kick the member
pineafan3a02ea32022-08-11 21:35:04 +0100194 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000195 // Check if Nucleus has permission to kick
PineaFana00db1b2023-01-02 15:32:54 +0000196 if (!me.permissions.has("KickMembers")) throw new Error("I do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000197 // Do not allow kicking Nucleus
PineaFana00db1b2023-01-02 15:32:54 +0000198 if (member.id === interaction.guild.members.me!.id) throw new Error("I cannot kick myself");
pineafan8b4b17f2022-02-27 20:42:52 +0000199 // Allow the owner to kick anyone
PineaFana00db1b2023-01-02 15:32:54 +0000200 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000201 // Check if the user has kick_members permission
PineaFana00db1b2023-01-02 15:32:54 +0000202 if (!member.permissions.has("KickMembers")) throw new Error("You do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000203 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100204 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000205 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100206 return true;
207};
pineafan4f164f32022-02-26 22:07:12 +0000208
Skyler Grey75ea9172022-08-06 10:22:23 +0100209export { command, callback, check };