blob: b9b1b2a0733a027c3ae880cf1939b91d0a939b08 [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";
4import { WrappedCheck } from "jshaiku";
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")
14 .addUserOption(option => option.setName("user").setDescription("The user to kick").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000015
pineafan4edb7762022-06-26 19:21:04 +010016const callback = async (interaction: CommandInteraction): Promise<any> => {
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;
pineafan73a7c4a2022-07-24 10:38:04 +010022 while (true) {
23 confirmation = await new confirmationMessage(interaction)
24 .setEmoji("PUNISH.KICK.RED")
25 .setTitle("Kick")
26 .setDescription(keyValueList({
27 "user": renderUser(interaction.options.getUser("user")),
28 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
29 })
pineafan63fc5e22022-08-04 22:04:10 +010030 + `The user **will${notify ? "" : " not"}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010031 + `Are you sure you want to kick <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
32 .setColor("Danger")
33 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010034 .send(reason !== null);
35 reason = reason ?? "";
36 if (confirmation.cancelled) return;
37 if (confirmation.success) break;
38 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010039 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010040 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010041 }
pineafan73a7c4a2022-07-24 10:38:04 +010042 }
pineafan377794f2022-04-18 19:01:01 +010043 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010044 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000045 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010046 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000047 try {
pineafan02ba0232022-07-24 22:16:15 +010048 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000049 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010050 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000051 .setEmoji("PUNISH.KICK.RED")
52 .setTitle("Kicked")
53 .setDescription(`You have been kicked in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010054 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000055 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010056 ],
57 components: [new MessageActionRow().addComponents(config.moderation.kick.text ? [new MessageButton()
58 .setStyle("LINK")
59 .setLabel(config.moderation.kick.text)
60 .setURL(config.moderation.kick.link)
61 ] : [])]
pineafan63fc5e22022-08-04 22:04:10 +010062 });
63 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000064 }
pineafan63fc5e22022-08-04 22:04:10 +010065 } catch { dmd = false; }
pineafan8b4b17f2022-02-27 20:42:52 +000066 try {
pineafan63fc5e22022-08-04 22:04:10 +010067 (interaction.options.getMember("user") as GuildMember).kick(reason ?? "No reason provided.");
68 const member = (interaction.options.getMember("user") as GuildMember);
69 await client.database.history.create("kick", interaction.guild.id, member.user, interaction.user, reason);
70 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
71 const data = {
pineafane625d782022-05-09 18:04:32 +010072 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010073 type: "memberKick",
74 displayName: "Member Kicked",
75 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010076 color: NucleusColors.red,
77 emoji: "PUNISH.KICK.RED",
78 timestamp: new Date().getTime()
79 },
80 list: {
pineafanda6e5342022-07-03 10:03:16 +010081 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010082 name: entry(member.id, renderUser(member.user)),
83 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
84 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
85 kickedBy: entry(interaction.user.id, renderUser(interaction.user)),
86 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
87 timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedTimestamp, { round: true })),
88 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
pineafan63fc5e22022-08-04 22:04:10 +010089 serverMemberCount: member.guild.memberCount
pineafane625d782022-05-09 18:04:32 +010090 },
91 hidden: {
92 guild: member.guild.id
93 }
pineafan63fc5e22022-08-04 22:04:10 +010094 };
pineafan4edb7762022-06-26 19:21:04 +010095 log(data);
pineafan8b4b17f2022-02-27 20:42:52 +000096 } catch {
pineafan4edb7762022-06-26 19:21:04 +010097 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000098 .setEmoji("PUNISH.KICK.RED")
pineafan63fc5e22022-08-04 22:04:10 +010099 .setTitle("Kick")
pineafan8b4b17f2022-02-27 20:42:52 +0000100 .setDescription("Something went wrong and the user was not kicked")
101 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +0100102 ], components: []});
103 if (dmd) await dm.delete();
104 return;
pineafan8b4b17f2022-02-27 20:42:52 +0000105 }
pineafan63fc5e22022-08-04 22:04:10 +0100106 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +0100107 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000108 .setEmoji(`PUNISH.KICK.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +0100109 .setTitle("Kick")
pineafan5d1908e2022-02-28 21:34:47 +0000110 .setDescription("The member was kicked" + (failed ? ", but could not be notified" : ""))
111 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +0100112 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +0000113 } else {
pineafan4edb7762022-06-26 19:21:04 +0100114 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000115 .setEmoji("PUNISH.KICK.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100116 .setTitle("Kick")
pineafan8b4b17f2022-02-27 20:42:52 +0000117 .setDescription("No changes were made")
118 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100119 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +0000120 }
pineafan63fc5e22022-08-04 22:04:10 +0100121};
pineafan4f164f32022-02-26 22:07:12 +0000122
123const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100124 const member = (interaction.member as GuildMember);
125 const me = (interaction.guild.me as GuildMember);
126 const apply = (interaction.options.getMember("user") as GuildMember);
127 if (member === null || me === null || apply === null) throw "That member is not in the server";
128 const memberPos = member.roles ? member.roles.highest.position : 0;
129 const mePos = me.roles ? me.roles.highest.position : 0;
130 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100131 // Do not allow kicking the owner
pineafan63fc5e22022-08-04 22:04:10 +0100132 if (member.id === interaction.guild.ownerId) throw "You cannot kick the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000133 // Check if Nucleus can kick the member
pineafan63fc5e22022-08-04 22:04:10 +0100134 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000135 // Check if Nucleus has permission to kick
pineafane23c4ec2022-07-27 21:56:27 +0100136 if (! me.permissions.has("KICK_MEMBERS")) throw "I do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000137 // Do not allow kicking Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100138 if (member.id === interaction.guild.me.id) throw "I cannot kick myself";
pineafan8b4b17f2022-02-27 20:42:52 +0000139 // Allow the owner to kick anyone
pineafan63fc5e22022-08-04 22:04:10 +0100140 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000141 // Check if the user has kick_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100142 if (! member.permissions.has("KICK_MEMBERS")) throw "You do not have the *Kick Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000143 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100144 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000145 // Allow kick
pineafan63fc5e22022-08-04 22:04:10 +0100146 return true;
147};
pineafan4f164f32022-02-26 22:07:12 +0000148
pineafan8b4b17f2022-02-27 20:42:52 +0000149export { command, callback, check };