blob: d0dc9bb175828c61de12b4ff3e09788fd80639da [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";
pineafan8b4b17f2022-02-27 20:42:52 +00003import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00005import keyValueList from "../../utils/generateKeyValueList.js";
pineafan6702cef2022-06-13 17:52:37 +01006import client from "../../utils/client.js";
7import addPlural from "../../utils/plurals.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("softban")
12 .setDescription("Kicks a user and deletes their messages")
13 .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true))
14 .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 +000015
pineafanbd02b4a2022-08-05 22:01:38 +010016const callback = async (interaction: CommandInteraction): Promise<void | 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;
pineafan73a7c4a2022-07-24 10:38:04 +010021 let confirmation;
22 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +010023 const confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010024 .setEmoji("PUNISH.BAN.RED")
25 .setTitle("Softban")
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`
pineafan73a7c4a2022-07-24 10:38:04 +010031 + `${addPlural(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
32 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
33 .setColor("Danger")
pineafan02ba0232022-07-24 22:16:15 +010034 .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify)
pineafan73a7c4a2022-07-24 10:38:04 +010035 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010036 .send(reason !== null);
37 reason = reason ?? "";
38 if (confirmation.cancelled) return;
39 if (confirmation.success) break;
40 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010041 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010042 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010043 }
pineafan73a7c4a2022-07-24 10:38:04 +010044 }
pineafan377794f2022-04-18 19:01:01 +010045 if (confirmation.success) {
46 let dmd = false;
pineafan63fc5e22022-08-04 22:04:10 +010047 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000048 try {
pineafan02ba0232022-07-24 22:16:15 +010049 if (notify) {
pineafan8b4b17f2022-02-27 20:42:52 +000050 await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010051 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000052 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000053 .setTitle("Softbanned")
54 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010055 (reason ? ` for:\n> ${reason}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000056 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010057 ],
58 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
59 .setStyle("LINK")
60 .setLabel(config.moderation.ban.text)
61 .setURL(config.moderation.ban.link)
62 ] : [])]
pineafan63fc5e22022-08-04 22:04:10 +010063 });
64 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000065 }
pineafan63fc5e22022-08-04 22:04:10 +010066 } catch { dmd = false;}
67 const member = (interaction.options.getMember("user") as GuildMember);
pineafan8b4b17f2022-02-27 20:42:52 +000068 try {
pineafan4edb7762022-06-26 19:21:04 +010069 await member.ban({
pineafan8b4b17f2022-02-27 20:42:52 +000070 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafan73a7c4a2022-07-24 10:38:04 +010071 reason: reason
pineafan5d1908e2022-02-28 21:34:47 +000072 });
pineafan4edb7762022-06-26 19:21:04 +010073 await interaction.guild.members.unban(member, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000074 } catch {
pineafan4edb7762022-06-26 19:21:04 +010075 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000076 .setEmoji("PUNISH.BAN.RED")
pineafan63fc5e22022-08-04 22:04:10 +010077 .setTitle("Softban")
pineafan8b4b17f2022-02-27 20:42:52 +000078 .setDescription("Something went wrong and the user was not softbanned")
79 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010080 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +000081 }
pineafan63fc5e22022-08-04 22:04:10 +010082 await client.database.history.create("softban", interaction.guild.id, member.user, reason);
83 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +010084 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000085 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +010086 .setTitle("Softban")
pineafan5d1908e2022-02-28 21:34:47 +000087 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
88 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +010089 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +000090 } else {
pineafan4edb7762022-06-26 19:21:04 +010091 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000092 .setEmoji("PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010093 .setTitle("Softban")
pineafan8b4b17f2022-02-27 20:42:52 +000094 .setDescription("No changes were made")
95 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010096 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +000097 }
pineafan63fc5e22022-08-04 22:04:10 +010098};
pineafan4f164f32022-02-26 22:07:12 +000099
pineafanbd02b4a2022-08-05 22:01:38 +0100100const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +0100101 const member = (interaction.member as GuildMember);
102 const me = (interaction.guild.me as GuildMember);
103 const apply = (interaction.options.getMember("user") as GuildMember);
104 if (member === null || me === null || apply === null) throw "That member is not in the server";
105 const memberPos = member.roles ? member.roles.highest.position : 0;
106 const mePos = me.roles ? me.roles.highest.position : 0;
107 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100108 // Do not allow softbanning the owner
pineafan63fc5e22022-08-04 22:04:10 +0100109 if (member.id === interaction.guild.ownerId) throw "You cannot softban the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000110 // Check if Nucleus can ban the member
pineafan63fc5e22022-08-04 22:04:10 +0100111 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000112 // Check if Nucleus has permission to ban
pineafane23c4ec2022-07-27 21:56:27 +0100113 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000114 // Do not allow softbanning Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100115 if (member.id === me.id) throw "I cannot softban myself";
pineafanc1c18792022-08-03 21:41:36 +0100116 // Allow the owner to softban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100117 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000118 // Check if the user has ban_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100119 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000120 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100121 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000122 // Allow softban
pineafan63fc5e22022-08-04 22:04:10 +0100123 return true;
124};
pineafan4f164f32022-02-26 22:07:12 +0000125
pineafan8b4b17f2022-02-27 20:42:52 +0000126export { command, callback, check };