blob: ef58067767124641b4bd29a4f966236964ee9f4b [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import humanizeDuration from "humanize-duration";
pineafan4f164f32022-02-26 22:07:12 +00003import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan8b4b17f2022-02-27 20:42:52 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("kick")
12 .setDescription("Kicks a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010013 .addUserOption((option) => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan3a02ea32022-08-11 21:35:04 +010015const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010016 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000017 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010018 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010019 let notify = true;
pineafan63fc5e22022-08-04 22:04:10 +010020 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010021 let timedOut = false;
22 let success = false;
23 while (!timedOut && !success) {
pineafan73a7c4a2022-07-24 10:38:04 +010024 confirmation = await new confirmationMessage(interaction)
25 .setEmoji("PUNISH.KICK.RED")
26 .setTitle("Kick")
Skyler Grey75ea9172022-08-06 10:22:23 +010027 .setDescription(
28 keyValueList({
29 user: renderUser(interaction.options.getUser("user")),
Skyler Grey11236ba2022-08-08 21:13:33 +010030 reason: reason ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010031 }) +
32 `The user **will${notify ? "" : " not"}** be notified\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010033 `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010034 )
pineafan73a7c4a2022-07-24 10:38:04 +010035 .setColor("Danger")
36 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010037 .send(reason !== null);
38 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010039 if (confirmation.cancelled) timedOut = true;
40 else if (confirmation.success) success = true;
41 else if (confirmation.newReason) reason = confirmation.newReason;
42 else if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010043 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010044 }
pineafan73a7c4a2022-07-24 10:38:04 +010045 }
Skyler Greyad002172022-08-16 18:48:26 +010046 if (timedOut) return;
47 let dmd = false;
48 let dm;
49 const config = await client.database.guilds.read(interaction.guild.id);
50 try {
51 if (notify) {
52 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010053 embeds: [
54 new EmojiEmbed()
55 .setEmoji("PUNISH.KICK.RED")
Skyler Greyad002172022-08-16 18:48:26 +010056 .setTitle("Kicked")
57 .setDescription(
58 `You have been kicked in ${interaction.guild.name}` + (reason ? ` for:\n> ${reason}` : ".")
59 )
Skyler Grey75ea9172022-08-06 10:22:23 +010060 .setStatus("Danger")
61 ],
Skyler Greyad002172022-08-16 18:48:26 +010062 components: [
63 new MessageActionRow().addComponents(
64 config.moderation.kick.text
65 ? [
66 new MessageButton()
67 .setStyle("LINK")
68 .setLabel(config.moderation.kick.text)
69 .setURL(config.moderation.kick.link)
70 ]
71 : []
72 )
73 ]
Skyler Grey75ea9172022-08-06 10:22:23 +010074 });
Skyler Greyad002172022-08-16 18:48:26 +010075 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000076 }
Skyler Greyad002172022-08-16 18:48:26 +010077 } catch {
78 dmd = false;
pineafan8b4b17f2022-02-27 20:42:52 +000079 }
Skyler Greyad002172022-08-16 18:48:26 +010080 try {
81 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.");
82 const member = interaction.options.getMember("user") as GuildMember;
83 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
84 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
85 const data = {
86 meta: {
87 type: "memberKick",
88 displayName: "Member Kicked",
89 calculateType: "guildMemberPunish",
90 color: NucleusColors.red,
91 emoji: "PUNISH.KICK.RED",
92 timestamp: new Date().getTime()
93 },
94 list: {
95 memberId: entry(member.id, `\`${member.id}\``),
96 name: entry(member.id, renderUser(member.user)),
97 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
98 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
99 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
100 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
101 timeInServer: entry(
102 new Date().getTime() - member.joinedTimestamp,
103 humanizeDuration(new Date().getTime() - member.joinedTimestamp, {
104 round: true
105 })
106 ),
107 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
108 serverMemberCount: member.guild.memberCount
109 },
110 hidden: {
111 guild: member.guild.id
112 }
113 };
114 log(data);
115 } catch {
116 await interaction.editReply({
117 embeds: [
118 new EmojiEmbed()
119 .setEmoji("PUNISH.KICK.RED")
120 .setTitle("Kick")
121 .setDescription("Something went wrong and the user was not kicked")
122 .setStatus("Danger")
123 ],
124 components: []
125 });
126 if (dmd) await dm.delete();
127 return;
128 }
129 const failed = !dmd && notify;
130 await interaction.editReply({
131 embeds: [
132 new EmojiEmbed()
133 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
134 .setTitle("Kick")
135 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
136 .setStatus(failed ? "Warning" : "Success")
137 ],
138 components: []
139 });
pineafan63fc5e22022-08-04 22:04:10 +0100140};
pineafan4f164f32022-02-26 22:07:12 +0000141
pineafanbd02b4a2022-08-05 22:01:38 +0100142const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 const member = interaction.member as GuildMember;
144 const me = interaction.guild.me!;
145 const apply = interaction.options.getMember("user") as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100146 if (member === null || me === null || apply === null) throw new Error("That member is not in the server");
pineafan63fc5e22022-08-04 22:04:10 +0100147 const memberPos = member.roles ? member.roles.highest.position : 0;
148 const mePos = me.roles ? me.roles.highest.position : 0;
149 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100150 // Do not allow kicking the owner
pineafan3a02ea32022-08-11 21:35:04 +0100151 if (member.id === interaction.guild.ownerId) throw new Error("You cannot kick the owner of the server");
pineafan8b4b17f2022-02-27 20:42:52 +0000152 // Check if Nucleus can kick the member
pineafan3a02ea32022-08-11 21:35:04 +0100153 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000154 // Check if Nucleus has permission to kick
pineafan3a02ea32022-08-11 21:35:04 +0100155 if (!me.permissions.has("KICK_MEMBERS")) throw new Error("I do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000156 // Do not allow kicking Nucleus
pineafan3a02ea32022-08-11 21:35:04 +0100157 if (member.id === interaction.guild.me.id) throw new Error("I cannot kick myself");
pineafan8b4b17f2022-02-27 20:42:52 +0000158 // Allow the owner to kick anyone
pineafan63fc5e22022-08-04 22:04:10 +0100159 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000160 // Check if the user has kick_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100161 if (!member.permissions.has("KICK_MEMBERS")) throw new Error("You do not have the *Kick Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000162 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100163 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000164 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100165 return true;
166};
pineafan4f164f32022-02-26 22:07:12 +0000167
Skyler Grey75ea9172022-08-06 10:22:23 +0100168export { command, callback, check };