blob: 43d03fdbeb82ce32b63ace565f69ef4c894b3911 [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
12 .setName("softban")
pineafan8b4b17f2022-02-27 20:42:52 +000013 .setDescription("Kicks a user and deletes their messages")
14 .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true))
pineafan73a7c4a2022-07-24 10:38:04 +010015 .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> => {
18 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) {
24 let confirmation = await new confirmationMessage(interaction)
25 .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 })
pineafan02ba0232022-07-24 22:16:15 +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 ?? "")
37 .send(reason !== null)
38 reason = reason ?? ""
pineafan02ba0232022-07-24 22:16:15 +010039 if (confirmation.cancelled) return
40 if (confirmation.success) break
41 if (confirmation.newReason) reason = confirmation.newReason
42 if (confirmation.components) {
43 notify = confirmation.components.notify.active
44 }
pineafan73a7c4a2022-07-24 10:38:04 +010045 }
pineafan377794f2022-04-18 19:01:01 +010046 if (confirmation.success) {
47 let dmd = false;
pineafan4edb7762022-06-26 19:21:04 +010048 let 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 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000064 })
65 dmd = true
66 }
67 } catch {}
pineafan4edb7762022-06-26 19:21:04 +010068 let 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")
78 .setTitle(`Softban`)
79 .setDescription("Something went wrong and the user was not softbanned")
80 .setStatus("Danger")
81 ], components: []})
82 }
pineafan73a7c4a2022-07-24 10:38:04 +010083 try { await client.database.history.create("softban", interaction.guild.id, member.user, reason) } catch {}
pineafane23c4ec2022-07-27 21:56:27 +010084 let 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"}`)
87 .setTitle(`Softban`)
88 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
89 .setStatus(failed ? "Warning" : "Success")
90 ], 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")
94 .setTitle(`Softban`)
95 .setDescription("No changes were made")
96 .setStatus("Success")
97 ], components: []})
98 }
pineafan4f164f32022-02-26 22:07:12 +000099}
100
101const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000102 let member = (interaction.member as GuildMember)
103 let me = (interaction.guild.me as GuildMember)
104 let apply = (interaction.options.getMember("user") as GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100105 if (member === null || me === null || apply === null) throw "That member is not in the server"
pineafan5d1908e2022-02-28 21:34:47 +0000106 let memberPos = member.roles ? member.roles.highest.position : 0
107 let mePos = me.roles ? me.roles.highest.position : 0
108 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafanc1c18792022-08-03 21:41:36 +0100109 // Do not allow softbanning the owner
110 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
pineafan5d1908e2022-02-28 21:34:47 +0000112 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
pineafane23c4ec2022-07-27 21:56:27 +0100116 if (member.id === me.id) throw "I cannot softban myself"
pineafanc1c18792022-08-03 21:41:36 +0100117 // Allow the owner to softban anyone
pineafane23c4ec2022-07-27 21:56:27 +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
pineafan5d1908e2022-02-28 21:34:47 +0000122 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000123 // Allow softban
124 return true
pineafan4f164f32022-02-26 22:07:12 +0000125}
126
pineafan8b4b17f2022-02-27 20:42:52 +0000127export { command, callback, check };