blob: 88ba531e31192330d6563e4e547a8631fd404d27 [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
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";
8import addPlural from "../../utils/plurals.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("softban")
13 .setDescription("Kicks a user and deletes their messages")
14 .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true))
15 .addIntegerOption(option => option.setName("delete").setDescription("The days of messages to delete | Default: 0").setMinValue(0).setMaxValue(7).setRequired(false));
pineafan4f164f32022-02-26 22:07:12 +000016
pineafan4edb7762022-06-26 19:21:04 +010017const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan63fc5e22022-08-04 22:04:10 +010018 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000019 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010020 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010021 let notify = true;
pineafan73a7c4a2022-07-24 10:38:04 +010022 let confirmation;
23 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +010024 const confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010025 .setEmoji("PUNISH.BAN.RED")
26 .setTitle("Softban")
27 .setDescription(keyValueList({
28 "user": renderUser(interaction.options.getUser("user")),
29 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
30 })
pineafan63fc5e22022-08-04 22:04:10 +010031 + `The user **will${notify ? "" : " not"}** be notified\n`
pineafan73a7c4a2022-07-24 10:38:04 +010032 + `${addPlural(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
33 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
34 .setColor("Danger")
pineafan02ba0232022-07-24 22:16:15 +010035 .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify)
pineafan73a7c4a2022-07-24 10:38:04 +010036 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010037 .send(reason !== null);
38 reason = reason ?? "";
39 if (confirmation.cancelled) return;
40 if (confirmation.success) break;
41 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010042 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 }
pineafan377794f2022-04-18 19:01:01 +010046 if (confirmation.success) {
47 let dmd = false;
pineafan63fc5e22022-08-04 22:04:10 +010048 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000049 try {
pineafan02ba0232022-07-24 22:16:15 +010050 if (notify) {
pineafan8b4b17f2022-02-27 20:42:52 +000051 await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010052 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000053 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000054 .setTitle("Softbanned")
55 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010056 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000057 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010058 ],
59 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
60 .setStyle("LINK")
61 .setLabel(config.moderation.ban.text)
62 .setURL(config.moderation.ban.link)
63 ] : [])]
pineafan63fc5e22022-08-04 22:04:10 +010064 });
65 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000066 }
pineafan63fc5e22022-08-04 22:04:10 +010067 } catch { dmd = false;}
68 const member = (interaction.options.getMember("user") as GuildMember);
pineafan8b4b17f2022-02-27 20:42:52 +000069 try {
pineafan4edb7762022-06-26 19:21:04 +010070 await member.ban({
pineafan8b4b17f2022-02-27 20:42:52 +000071 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafan73a7c4a2022-07-24 10:38:04 +010072 reason: reason
pineafan5d1908e2022-02-28 21:34:47 +000073 });
pineafan4edb7762022-06-26 19:21:04 +010074 await interaction.guild.members.unban(member, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000075 } catch {
pineafan4edb7762022-06-26 19:21:04 +010076 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000077 .setEmoji("PUNISH.BAN.RED")
pineafan63fc5e22022-08-04 22:04:10 +010078 .setTitle("Softban")
pineafan8b4b17f2022-02-27 20:42:52 +000079 .setDescription("Something went wrong and the user was not softbanned")
80 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010081 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +000082 }
pineafan63fc5e22022-08-04 22:04:10 +010083 await client.database.history.create("softban", interaction.guild.id, member.user, reason);
84 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +010085 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000086 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +010087 .setTitle("Softban")
pineafan5d1908e2022-02-28 21:34:47 +000088 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
89 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +010090 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +000091 } else {
pineafan4edb7762022-06-26 19:21:04 +010092 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000093 .setEmoji("PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010094 .setTitle("Softban")
pineafan8b4b17f2022-02-27 20:42:52 +000095 .setDescription("No changes were made")
96 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010097 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +000098 }
pineafan63fc5e22022-08-04 22:04:10 +010099};
pineafan4f164f32022-02-26 22:07:12 +0000100
101const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100102 const member = (interaction.member as GuildMember);
103 const me = (interaction.guild.me as GuildMember);
104 const apply = (interaction.options.getMember("user") as GuildMember);
105 if (member === null || me === null || apply === null) throw "That member is not in the server";
106 const memberPos = member.roles ? member.roles.highest.position : 0;
107 const mePos = me.roles ? me.roles.highest.position : 0;
108 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100109 // Do not allow softbanning the owner
pineafan63fc5e22022-08-04 22:04:10 +0100110 if (member.id === interaction.guild.ownerId) throw "You cannot softban the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000111 // Check if Nucleus can ban the member
pineafan63fc5e22022-08-04 22:04:10 +0100112 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000113 // Check if Nucleus has permission to ban
pineafane23c4ec2022-07-27 21:56:27 +0100114 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000115 // Do not allow softbanning Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100116 if (member.id === me.id) throw "I cannot softban myself";
pineafanc1c18792022-08-03 21:41:36 +0100117 // Allow the owner to softban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100118 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000119 // Check if the user has ban_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100120 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000121 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100122 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000123 // Allow softban
pineafan63fc5e22022-08-04 22:04:10 +0100124 return true;
125};
pineafan4f164f32022-02-26 22:07:12 +0000126
pineafan8b4b17f2022-02-27 20:42:52 +0000127export { command, callback, check };