blob: f6052f8bbd293bebad811a1119e713a9a68d5e2b [file] [log] [blame]
TheCodedProf21c08592022-09-13 14:14:43 -04001import { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
pineafan62ce1922022-08-25 20:34:45 +01002// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import humanizeDuration from "humanize-duration";
pineafan62ce1922022-08-25 20:34:45 +01004import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan8b4b17f2022-02-27 20:42:52 +00005import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01006import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00007import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01008import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("kick")
13 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010014 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000015
pineafan3a02ea32022-08-11 21:35:04 +010016const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010017 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000018 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010019 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010020 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010021 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010022 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010023 let chosen = false;
24 while (!timedOut && !chosen) {
pineafan73a7c4a2022-07-24 10:38:04 +010025 confirmation = await new confirmationMessage(interaction)
26 .setEmoji("PUNISH.KICK.RED")
27 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010028 .setDescription(
29 keyValueList({
30 user: renderUser(interaction.options.getUser("user")),
pineafan62ce1922022-08-25 20:34:45 +010031 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010032 }) +
33 `The user **will${notify ? "" : " not"}** be notified\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010034 `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010035 )
pineafan73a7c4a2022-07-24 10:38:04 +010036 .setColor("Danger")
37 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010038 .send(reason !== null);
39 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010040 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010041 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010042 else if (confirmation.newReason) reason = confirmation.newReason;
43 else if (confirmation.components) {
pineafan62ce1922022-08-25 20:34:45 +010044 notify = confirmation.components["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010045 }
pineafan73a7c4a2022-07-24 10:38:04 +010046 }
Skyler Greyad002172022-08-16 18:48:26 +010047 if (timedOut) return;
48 let dmd = false;
49 let dm;
pineafan62ce1922022-08-25 20:34:45 +010050 const config = await client.database.guilds.read(interaction.guild!.id);
Skyler Greyad002172022-08-16 18:48:26 +010051 try {
52 if (notify) {
53 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010054 embeds: [
55 new EmojiEmbed()
56 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010057 .setTitle("Kicked")
58 .setDescription(
pineafan62ce1922022-08-25 20:34:45 +010059 `You have been kicked in ${interaction.guild!.name}` + (reason ? ` for:\n> ${reason}` : ".")
Skyler Greyad002172022-08-16 18:48:26 +010060 )
Skyler Grey75ea9172022-08-06 10:22:23 +010061 .setStatus("Danger")
62 ],
Skyler Greyad002172022-08-16 18:48:26 +010063 components: [
TheCodedProf21c08592022-09-13 14:14:43 -040064 new ActionRowBuilder().addComponents(
Skyler Greyad002172022-08-16 18:48:26 +010065 config.moderation.kick.text
66 ? [
TheCodedProf21c08592022-09-13 14:14:43 -040067 new ButtonBuilder()
68 .setStyle(ButtonStyle.Link)
Skyler Greyad002172022-08-16 18:48:26 +010069 .setLabel(config.moderation.kick.text)
70 .setURL(config.moderation.kick.link)
71 ]
72 : []
73 )
74 ]
Skyler Grey75ea9172022-08-06 10:22:23 +010075 });
Skyler Greyad002172022-08-16 18:48:26 +010076 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000077 }
Skyler Greyad002172022-08-16 18:48:26 +010078 } catch {
79 dmd = false;
pineafan8b4b17f2022-02-27 20:42:52 +000080 }
Skyler Greyad002172022-08-16 18:48:26 +010081 try {
82 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.");
83 const member = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +010084 await client.database.history.create("kick", interaction.guild!.id, member.user, interaction.user, reason);
Skyler Greyad002172022-08-16 18:48:26 +010085 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan62ce1922022-08-25 20:34:45 +010086 const timeInServer = member.joinedTimestamp ? entry(
87 new Date().getTime() - member.joinedTimestamp,
88 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
89 round: true
90 })
91 ) : entry(null, "*Unknown*")
Skyler Greyad002172022-08-16 18:48:26 +010092 const data = {
93 meta: {
94 type: "memberKick",
95 displayName: "Member Kicked",
96 calculateType: "guildMemberPunish",
97 color: NucleusColors.red,
98 emoji: "PUNISH.KICK.RED",
99 timestamp: new Date().getTime()
100 },
101 list: {
102 memberId: entry(member.id, `\`${member.id}\``),
103 name: entry(member.id, renderUser(member.user)),
104 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
105 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
106 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
107 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
pineafan62ce1922022-08-25 20:34:45 +0100108 timeInServer: timeInServer,
Skyler Greyad002172022-08-16 18:48:26 +0100109 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
110 serverMemberCount: member.guild.memberCount
111 },
112 hidden: {
113 guild: member.guild.id
114 }
115 };
116 log(data);
117 } catch {
118 await interaction.editReply({
119 embeds: [
120 new EmojiEmbed()
121 .setEmoji("PUNISH.KICK.RED")
122 .setTitle("Kick")
123 .setDescription("Something went wrong and the user was not kicked")
124 .setStatus("Danger")
125 ],
126 components: []
127 });
pineafan62ce1922022-08-25 20:34:45 +0100128 if (dmd && dm) await dm.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100129 return;
130 }
131 const failed = !dmd && notify;
132 await interaction.editReply({
133 embeds: [
134 new EmojiEmbed()
135 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
136 .setTitle("Kick")
137 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
138 .setStatus(failed ? "Warning" : "Success")
139 ],
140 components: []
141 });
pineafan63fc5e22022-08-04 22:04:10 +0100142};
pineafan4f164f32022-02-26 22:07:12 +0000143
pineafanbd02b4a2022-08-05 22:01:38 +0100144const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 const member = interaction.member as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100146 const me = interaction.guild!.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100148 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
149 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
150 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100151 // Do not allow kicking the owner
pineafan62ce1922022-08-25 20:34:45 +0100152 if (member.id === interaction.guild!.ownerId) throw new Error("You cannot kick the owner of the server");
pineafan8b4b17f2022-02-27 20:42:52 +0000153 // Check if Nucleus can kick the member
pineafan3a02ea32022-08-11 21:35:04 +0100154 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000155 // Check if Nucleus has permission to kick
pineafan3a02ea32022-08-11 21:35:04 +0100156 if (!me.permissions.has("KICK_MEMBERS")) throw new Error("I do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000157 // Do not allow kicking Nucleus
pineafan62ce1922022-08-25 20:34:45 +0100158 if (member.id === interaction.guild!.me!.id) throw new Error("I cannot kick myself");
pineafan8b4b17f2022-02-27 20:42:52 +0000159 // Allow the owner to kick anyone
pineafan62ce1922022-08-25 20:34:45 +0100160 if (member.id === interaction.guild!.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000161 // Check if the user has kick_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100162 if (!member.permissions.has("KICK_MEMBERS")) throw new Error("You do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000163 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100164 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000165 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100166 return true;
167};
pineafan4f164f32022-02-26 22:07:12 +0000168
Skyler Grey75ea9172022-08-06 10:22:23 +0100169export { command, callback, check };