blob: 44d425a689d07d907f5ee03955baecea0237846a [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";
pineafan377794f2022-04-18 19:01:01 +01007import readConfig from '../../utils/readConfig.js'
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
11 .setName("softban")
pineafan8b4b17f2022-02-27 20:42:52 +000012 .setDescription("Kicks a user and deletes their messages")
13 .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true))
14 .addStringOption(option => option.setName("reason").setDescription("The reason for the softban").setRequired(false))
pineafan377794f2022-04-18 19:01:01 +010015 .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 +000016 .addChoices([["Yes", "yes"], ["No", "no"]])
17 )
18 .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 +000019
pineafan8b4b17f2022-02-27 20:42:52 +000020const callback = async (interaction: CommandInteraction) => {
21 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010022 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000023 .setEmoji("PUNISH.BAN.RED")
24 .setTitle("Softban")
25 .setDescription(keyValueList({
26 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
27 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
28 })
29 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
30 + `${interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0} day${interaction.options.getInteger("delete") === 1 || interaction.options.getInteger("delete") === null ? "s" : ""} of messages will be deleted\n\n` // TODO:[s addition]
31 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
32 .setColor("Danger")
33// pluralize("day", interaction.options.getInteger("delete"))
34// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
pineafan377794f2022-04-18 19:01:01 +010035 .send()
36 if (confirmation.success) {
37 let dmd = false;
38 let config = await readConfig(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({
pineafan377794f2022-04-18 19:01:01 +010042 embeds: [new generateEmojiEmbed()
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 {}
58 try {
pineafan5d1908e2022-02-28 21:34:47 +000059 await (interaction.options.getMember("user") as GuildMember).ban({
pineafan8b4b17f2022-02-27 20:42:52 +000060 days: Number(interaction.options.getInteger("delete") ?? 0),
61 reason: interaction.options.getString("reason")
pineafan5d1908e2022-02-28 21:34:47 +000062 });
63 await interaction.guild.members.unban(interaction.options.getMember("user") as GuildMember, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000064 } catch {
pineafan377794f2022-04-18 19:01:01 +010065 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000066 .setEmoji("PUNISH.BAN.RED")
67 .setTitle(`Softban`)
68 .setDescription("Something went wrong and the user was not softbanned")
69 .setStatus("Danger")
70 ], components: []})
71 }
pineafan5d1908e2022-02-28 21:34:47 +000072 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +010073 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000074 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
75 .setTitle(`Softban`)
76 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
77 .setStatus(failed ? "Warning" : "Success")
78 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000079 } else {
pineafan377794f2022-04-18 19:01:01 +010080 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000081 .setEmoji("PUNISH.BAN.GREEN")
82 .setTitle(`Softban`)
83 .setDescription("No changes were made")
84 .setStatus("Success")
85 ], components: []})
86 }
pineafan4f164f32022-02-26 22:07:12 +000087}
88
89const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000090 let member = (interaction.member as GuildMember)
91 let me = (interaction.guild.me as GuildMember)
92 let apply = (interaction.options.getMember("user") as GuildMember)
93 if (member == null || me == null || apply == null) throw "That member is not in the server"
94 let memberPos = member.roles ? member.roles.highest.position : 0
95 let mePos = me.roles ? me.roles.highest.position : 0
96 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000097 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +000098 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +000099 // Check if Nucleus has permission to ban
100 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
101 // Do not allow softbanning Nucleus
102 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot softban myself"
103 // Allow the owner to ban anyone
104 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
105 // Check if the user has ban_members permission
106 if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
107 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000108 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000109 // Allow softban
110 return true
pineafan4f164f32022-02-26 22:07:12 +0000111}
112
pineafan8b4b17f2022-02-27 20:42:52 +0000113export { command, callback, check };