blob: 23785c3851f48b07f707d4f94691c3164e0686d5 [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +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")
13 .addUserOption(option => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafanbd02b4a2022-08-05 22:01:38 +010015const callback = async (interaction: CommandInteraction): Promise<void | 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;
pineafan73a7c4a2022-07-24 10:38:04 +010021 while (true) {
22 confirmation = await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.KICK.RED")
24 .setTitle("Kick")
25 .setDescription(keyValueList({
26 "user": renderUser(interaction.options.getUser("user")),
27 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
28 })
pineafan63fc5e22022-08-04 22:04:10 +010029 + `The user **will${notify ? "" : " not"}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010030 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
31 .setColor("Danger")
32 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010033 .send(reason !== null);
34 reason = reason ?? "";
35 if (confirmation.cancelled) return;
36 if (confirmation.success) break;
37 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010038 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010039 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010040 }
pineafan73a7c4a2022-07-24 10:38:04 +010041 }
pineafan377794f2022-04-18 19:01:01 +010042 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010043 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000044 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010045 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000046 try {
pineafan02ba0232022-07-24 22:16:15 +010047 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000048 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010049 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000050 .setEmoji("PUNISH.KICK.RED")
51 .setTitle("Kicked")
52 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010053 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000054 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010055 ],
56 components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton()
57 .setStyle("LINK")
58 .setLabel(config.moderation.kick.text)
59 .setURL(config.moderation.kick.link)
60 ] : [])]
pineafan63fc5e22022-08-04 22:04:10 +010061 });
62 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000063 }
pineafan63fc5e22022-08-04 22:04:10 +010064 } catch { dmd = false; }
pineafan8b4b17f2022-02-27 20:42:52 +000065 try {
pineafan63fc5e22022-08-04 22:04:10 +010066 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.");
67 const member = (interaction.options.getMember("user") as GuildMember);
68 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
69 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
70 const data = {
pineafane625d782022-05-09 18:04:32 +010071 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010072 type: "memberKick",
73 displayName: "Member Kicked",
74 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010075 color: NucleusColors.red,
76 emoji: "PUNISH.KICK.RED",
77 timestamp: new Date().getTime()
78 },
79 list: {
pineafanda6e5342022-07-03 10:03:16 +010080 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010081 name: entry(member.id, renderUser(member.user)),
82 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
83 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
84 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
85 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
86 timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedTimestamp, { round: true })),
87 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
pineafan63fc5e22022-08-04 22:04:10 +010088 serverMemberCount: member.guild.memberCount
pineafane625d782022-05-09 18:04:32 +010089 },
90 hidden: {
91 guild: member.guild.id
92 }
pineafan63fc5e22022-08-04 22:04:10 +010093 };
pineafan4edb7762022-06-26 19:21:04 +010094 log(data);
pineafan8b4b17f2022-02-27 20:42:52 +000095 } catch {
pineafan4edb7762022-06-26 19:21:04 +010096 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000097 .setEmoji("PUNISH.KICK.RED")
pineafan63fc5e22022-08-04 22:04:10 +010098 .setTitle("Kick")
pineafan8b4b17f2022-02-27 20:42:52 +000099 .setDescription("Something went wrong and the user was not kicked")
100 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +0100101 ], components: []});
102 if (dmd) await dm.delete();
103 return;
pineafan8b4b17f2022-02-27 20:42:52 +0000104 }
pineafan63fc5e22022-08-04 22:04:10 +0100105 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +0100106 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000107 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +0100108 .setTitle("Kick")
pineafan5d1908e2022-02-28 21:34:47 +0000109 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
110 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +0100111 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +0000112 } else {
pineafan4edb7762022-06-26 19:21:04 +0100113 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000114 .setEmoji("PUNISH.KICK.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100115 .setTitle("Kick")
pineafan8b4b17f2022-02-27 20:42:52 +0000116 .setDescription("No changes were made")
117 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100118 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +0000119 }
pineafan63fc5e22022-08-04 22:04:10 +0100120};
pineafan4f164f32022-02-26 22:07:12 +0000121
pineafanbd02b4a2022-08-05 22:01:38 +0100122const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +0100123 const member = (interaction.member as GuildMember);
124 const me = (interaction.guild.me as GuildMember);
125 const apply = (interaction.options.getMember("user") as GuildMember);
126 if (member === null || me === null || apply === null) throw "That member is not in the server";
127 const memberPos = member.roles ? member.roles.highest.position : 0;
128 const mePos = me.roles ? me.roles.highest.position : 0;
129 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100130 // Do not allow kicking the owner
pineafan63fc5e22022-08-04 22:04:10 +0100131 if (member.id === interaction.guild.ownerId) throw "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000132 // Check if Nucleus can kick the member
pineafan63fc5e22022-08-04 22:04:10 +0100133 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000134 // Check if Nucleus has permission to kick
pineafane23c4ec2022-07-27 21:56:27 +0100135 if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000136 // Do not allow kicking Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100137 if (member.id === interaction.guild.me.id) throw "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000138 // Allow the owner to kick anyone
pineafan63fc5e22022-08-04 22:04:10 +0100139 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000140 // Check if the user has kick_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100141 if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000142 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100143 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000144 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100145 return true;
146};
pineafan4f164f32022-02-26 22:07:12 +0000147
pineafan8b4b17f2022-02-27 20:42:52 +0000148export { command, callback, check };