blob: ea4a4477f1d17cdf1ef6b68b8fdb59adc296b643 [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))
15 .addStringOption(option => option.setName("reason").setDescription("The reason for the softban").setRequired(false))
pineafan377794f2022-04-18 19:01:01 +010016 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are softbanned | Default yes").setRequired(false)
pineafan8b4b17f2022-02-27 20:42:52 +000017 .addChoices([["Yes", "yes"], ["No", "no"]])
18 )
19 .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 +000020
pineafan4edb7762022-06-26 19:21:04 +010021const callback = async (interaction: CommandInteraction): Promise<any> => {
22 const { renderUser } = client.logger
pineafan8b4b17f2022-02-27 20:42:52 +000023 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010024 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000025 .setEmoji("PUNISH.BAN.RED")
26 .setTitle("Softban")
27 .setDescription(keyValueList({
pineafan4edb7762022-06-26 19:21:04 +010028 "user": renderUser(interaction.options.getUser("user")),
pineafan8b4b17f2022-02-27 20:42:52 +000029 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
30 })
31 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
pineafan6702cef2022-06-13 17:52:37 +010032 + `${addPlural(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
pineafan8b4b17f2022-02-27 20:42:52 +000033 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
34 .setColor("Danger")
pineafan377794f2022-04-18 19:01:01 +010035 .send()
36 if (confirmation.success) {
37 let dmd = false;
pineafan4edb7762022-06-26 19:21:04 +010038 let config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000039 try {
40 if (interaction.options.getString("notify") != "no") {
41 await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010042 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000043 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000044 .setTitle("Softbanned")
45 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000046 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000047 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010048 ],
49 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
50 .setStyle("LINK")
51 .setLabel(config.moderation.ban.text)
52 .setURL(config.moderation.ban.link)
53 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000054 })
55 dmd = true
56 }
57 } catch {}
pineafan4edb7762022-06-26 19:21:04 +010058 let member = (interaction.options.getMember("user") as GuildMember)
pineafan8b4b17f2022-02-27 20:42:52 +000059 try {
pineafan4edb7762022-06-26 19:21:04 +010060 await member.ban({
pineafan8b4b17f2022-02-27 20:42:52 +000061 days: Number(interaction.options.getInteger("delete") ?? 0),
62 reason: interaction.options.getString("reason")
pineafan5d1908e2022-02-28 21:34:47 +000063 });
pineafan4edb7762022-06-26 19:21:04 +010064 await interaction.guild.members.unban(member, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000065 } catch {
pineafan4edb7762022-06-26 19:21:04 +010066 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000067 .setEmoji("PUNISH.BAN.RED")
68 .setTitle(`Softban`)
69 .setDescription("Something went wrong and the user was not softbanned")
70 .setStatus("Danger")
71 ], components: []})
72 }
pineafan4edb7762022-06-26 19:21:04 +010073 try { await client.database.history.create("softban", interaction.guild.id, member.user, interaction.options.getString("reason")) } catch {}
pineafan5d1908e2022-02-28 21:34:47 +000074 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan4edb7762022-06-26 19:21:04 +010075 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000076 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
77 .setTitle(`Softban`)
78 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
79 .setStatus(failed ? "Warning" : "Success")
80 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000081 } else {
pineafan4edb7762022-06-26 19:21:04 +010082 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000083 .setEmoji("PUNISH.BAN.GREEN")
84 .setTitle(`Softban`)
85 .setDescription("No changes were made")
86 .setStatus("Success")
87 ], components: []})
88 }
pineafan4f164f32022-02-26 22:07:12 +000089}
90
91const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000092 let member = (interaction.member as GuildMember)
93 let me = (interaction.guild.me as GuildMember)
94 let apply = (interaction.options.getMember("user") as GuildMember)
95 if (member == null || me == null || apply == null) throw "That member is not in the server"
96 let memberPos = member.roles ? member.roles.highest.position : 0
97 let mePos = me.roles ? me.roles.highest.position : 0
98 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000099 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000100 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000101 // Check if Nucleus has permission to ban
pineafan4edb7762022-06-26 19:21:04 +0100102 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the Ban members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000103 // Do not allow softbanning Nucleus
PineappleFan5fe720d2022-05-19 12:01:49 +0100104 if (member.id == me.id) throw "I cannot softban myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000105 // Allow the owner to ban anyone
pineafan167bde32022-05-19 19:33:46 +0100106 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000107 // Check if the user has ban_members permission
pineafan4edb7762022-06-26 19:21:04 +0100108 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the Ban members permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000109 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000110 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000111 // Allow softban
112 return true
pineafan4f164f32022-02-26 22:07:12 +0000113}
114
pineafan8b4b17f2022-02-27 20:42:52 +0000115export { command, callback, check };