blob: 29d3bef368c3c362e6e8b74fc2eb5a2c916c785e [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";
pineafane625d782022-05-09 18:04:32 +01007import readConfig from '../../utils/readConfig.js';
8import addPlurals 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`
pineafane625d782022-05-09 18:04:32 +010031 + `${addPlurals(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")
34// pluralize("day", interaction.options.getInteger("delete"))
35// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
pineafan377794f2022-04-18 19:01:01 +010036 .send()
37 if (confirmation.success) {
38 let dmd = false;
39 let config = await readConfig(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000040 try {
41 if (interaction.options.getString("notify") != "no") {
42 await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010043 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000044 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000045 .setTitle("Softbanned")
46 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000047 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000048 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010049 ],
50 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
51 .setStyle("LINK")
52 .setLabel(config.moderation.ban.text)
53 .setURL(config.moderation.ban.link)
54 ] : [])]
pineafan8b4b17f2022-02-27 20:42:52 +000055 })
56 dmd = true
57 }
58 } catch {}
59 try {
pineafan5d1908e2022-02-28 21:34:47 +000060 await (interaction.options.getMember("user") as GuildMember).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 });
64 await interaction.guild.members.unban(interaction.options.getMember("user") as GuildMember, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000065 } catch {
pineafan377794f2022-04-18 19:01:01 +010066 await interaction.editReply({embeds: [new generateEmojiEmbed()
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 }
pineafan5d1908e2022-02-28 21:34:47 +000073 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +010074 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000075 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
76 .setTitle(`Softban`)
77 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
78 .setStatus(failed ? "Warning" : "Success")
79 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000080 } else {
pineafan377794f2022-04-18 19:01:01 +010081 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000082 .setEmoji("PUNISH.BAN.GREEN")
83 .setTitle(`Softban`)
84 .setDescription("No changes were made")
85 .setStatus("Success")
86 ], components: []})
87 }
pineafan4f164f32022-02-26 22:07:12 +000088}
89
90const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000091 let member = (interaction.member as GuildMember)
92 let me = (interaction.guild.me as GuildMember)
93 let apply = (interaction.options.getMember("user") as GuildMember)
94 if (member == null || me == null || apply == null) throw "That member is not in the server"
95 let memberPos = member.roles ? member.roles.highest.position : 0
96 let mePos = me.roles ? me.roles.highest.position : 0
97 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000098 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +000099 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000100 // Check if Nucleus has permission to ban
PineappleFan5fe720d2022-05-19 12:01:49 +0100101 if (!me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000102 // Do not allow softbanning Nucleus
PineappleFan5fe720d2022-05-19 12:01:49 +0100103 if (member.id == me.id) throw "I cannot softban myself"
pineafan8b4b17f2022-02-27 20:42:52 +0000104 // Allow the owner to ban anyone
pineafan167bde32022-05-19 19:33:46 +0100105 if (member.id == interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000106 // Check if the user has ban_members permission
pineafan663dc472022-05-10 18:13:47 +0100107 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000108 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000109 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000110 // Allow softban
111 return true
pineafan4f164f32022-02-26 22:07:12 +0000112}
113
pineafan8b4b17f2022-02-27 20:42:52 +0000114export { command, callback, check };