blob: 5a01287a218caa00b92997504bf8dffbe1ebe9c4 [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";
pineafan377794f2022-04-18 19:01:01 +01005import generateEmojiEmbed 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
pineafan8b4b17f2022-02-27 20:42:52 +000021const callback = async (interaction: CommandInteraction) => {
22 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010023 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000024 .setEmoji("PUNISH.BAN.RED")
25 .setTitle("Softban")
26 .setDescription(keyValueList({
27 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
28 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
29 })
30 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
pineafan6702cef2022-06-13 17:52:37 +010031 + `${addPlural(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
pineafan8b4b17f2022-02-27 20:42:52 +000032 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
33 .setColor("Danger")
pineafan377794f2022-04-18 19:01:01 +010034 .send()
35 if (confirmation.success) {
36 let dmd = false;
pineafan6702cef2022-06-13 17:52:37 +010037 let config = await client.database.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000038 try {
39 if (interaction.options.getString("notify") != "no") {
40 await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010041 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000042 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000043 .setTitle("Softbanned")
44 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000045 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000046 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010047 ],
48 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
49 .setStyle("LINK")
50 .setLabel(config.moderation.ban.text)
51 .setURL(config.moderation.ban.link)
52 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000053 })
54 dmd = true
55 }
56 } catch {}
57 try {
pineafan5d1908e2022-02-28 21:34:47 +000058 await (interaction.options.getMember("user") as GuildMember).ban({
pineafan8b4b17f2022-02-27 20:42:52 +000059 days: Number(interaction.options.getInteger("delete") ?? 0),
60 reason: interaction.options.getString("reason")
pineafan5d1908e2022-02-28 21:34:47 +000061 });
62 await interaction.guild.members.unban(interaction.options.getMember("user") as GuildMember, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000063 } catch {
pineafan377794f2022-04-18 19:01:01 +010064 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000065 .setEmoji("PUNISH.BAN.RED")
66 .setTitle(`Softban`)
67 .setDescription("Something went wrong and the user was not softbanned")
68 .setStatus("Danger")
69 ], components: []})
70 }
pineafan5d1908e2022-02-28 21:34:47 +000071 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +010072 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000073 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
74 .setTitle(`Softban`)
75 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
76 .setStatus(failed ? "Warning" : "Success")
77 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000078 } else {
pineafan377794f2022-04-18 19:01:01 +010079 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000080 .setEmoji("PUNISH.BAN.GREEN")
81 .setTitle(`Softban`)
82 .setDescription("No changes were made")
83 .setStatus("Success")
84 ], components: []})
85 }
pineafan4f164f32022-02-26 22:07:12 +000086}
87
88const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000089 let member = (interaction.member as GuildMember)
90 let me = (interaction.guild.me as GuildMember)
91 let apply = (interaction.options.getMember("user") as GuildMember)
92 if (member == null || me == null || apply == null) throw "That member is not in the server"
93 let memberPos = member.roles ? member.roles.highest.position : 0
94 let mePos = me.roles ? me.roles.highest.position : 0
95 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000096 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +000097 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +000098 // Check if Nucleus has permission to ban
PineappleFan5fe720d2022-05-19 12:01:49 +010099 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000100 // Do not allow softbanning Nucleus
PineappleFan5fe720d2022-05-19 12:01:49 +0100101 if (member.id == me.id) throw "I cannot softban myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000102 // Allow the owner to ban anyone
pineafan167bde32022-05-19 19:33:46 +0100103 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000104 // Check if the user has ban_members permission
pineafan663dc472022-05-10 18:13:47 +0100105 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000106 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000107 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000108 // Allow softban
109 return true
pineafan4f164f32022-02-26 22:07:12 +0000110}
111
pineafan8b4b17f2022-02-27 20:42:52 +0000112export { command, callback, check };