blob: bcebbd5db3479988061897e1a78ef4513bc2673e [file] [log] [blame]
pineafan8b4b17f2022-02-27 20:42:52 +00001import { CommandInteraction, GuildMember } 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";
5import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
6import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
10 .setName("softban")
pineafan8b4b17f2022-02-27 20:42:52 +000011 .setDescription("Kicks a user and deletes their messages")
12 .addUserOption(option => option.setName("user").setDescription("The user to softban").setRequired(true))
13 .addStringOption(option => option.setName("reason").setDescription("The reason for the softban").setRequired(false))
14 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are banbanned | Default yes").setRequired(false)
15 .addChoices([["Yes", "yes"], ["No", "no"]])
16 )
17 .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 +000018
pineafan8b4b17f2022-02-27 20:42:52 +000019const callback = async (interaction: CommandInteraction) => {
20 // TODO:[Modals] Replace this with a modal
21 if (await new confirmationMessage(interaction)
22 .setEmoji("PUNISH.BAN.RED")
23 .setTitle("Softban")
24 .setDescription(keyValueList({
25 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
26 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
27 })
28 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
29 + `${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]
30 + `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
31 .setColor("Danger")
32// pluralize("day", interaction.options.getInteger("delete"))
33// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
34 .send()) {
35 let dmd = false
36 try {
37 if (interaction.options.getString("notify") != "no") {
38 await (interaction.options.getMember("user") as GuildMember).send({
39 embeds: [new EmojiEmbed()
40 .setEmoji("PUNISH.BAN.RED")
pineafan5d1908e2022-02-28 21:34:47 +000041 .setTitle("Softbanned")
42 .setDescription(`You have been softbanned from ${interaction.guild.name}` +
pineafan8b4b17f2022-02-27 20:42:52 +000043 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided."))
44 .setStatus("Danger")
45 ]
46 })
47 dmd = true
48 }
49 } catch {}
50 try {
pineafan5d1908e2022-02-28 21:34:47 +000051 await (interaction.options.getMember("user") as GuildMember).ban({
pineafan8b4b17f2022-02-27 20:42:52 +000052 days: Number(interaction.options.getInteger("delete") ?? 0),
53 reason: interaction.options.getString("reason")
pineafan5d1908e2022-02-28 21:34:47 +000054 });
55 await interaction.guild.members.unban(interaction.options.getMember("user") as GuildMember, "Softban");
pineafan8b4b17f2022-02-27 20:42:52 +000056 } catch {
57 await interaction.editReply({embeds: [new EmojiEmbed()
58 .setEmoji("PUNISH.BAN.RED")
59 .setTitle(`Softban`)
60 .setDescription("Something went wrong and the user was not softbanned")
61 .setStatus("Danger")
62 ], components: []})
63 }
pineafan5d1908e2022-02-28 21:34:47 +000064 let failed = (dmd == false && interaction.options.getString("notify") != "no")
65 await interaction.editReply({embeds: [new EmojiEmbed()
66 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
67 .setTitle(`Softban`)
68 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
69 .setStatus(failed ? "Warning" : "Success")
70 ], components: []})
pineafan8b4b17f2022-02-27 20:42:52 +000071 } else {
72 await interaction.editReply({embeds: [new EmojiEmbed()
73 .setEmoji("PUNISH.BAN.GREEN")
74 .setTitle(`Softban`)
75 .setDescription("No changes were made")
76 .setStatus("Success")
77 ], components: []})
78 }
pineafan4f164f32022-02-26 22:07:12 +000079}
80
81const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000082 let member = (interaction.member as GuildMember)
83 let me = (interaction.guild.me as GuildMember)
84 let apply = (interaction.options.getMember("user") as GuildMember)
85 if (member == null || me == null || apply == null) throw "That member is not in the server"
86 let memberPos = member.roles ? member.roles.highest.position : 0
87 let mePos = me.roles ? me.roles.highest.position : 0
88 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +000089 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +000090 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +000091 // Check if Nucleus has permission to ban
92 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
93 // Do not allow softbanning Nucleus
94 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot softban myself"
95 // Allow the owner to ban anyone
96 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
97 // Check if the user has ban_members permission
98 if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
99 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000100 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000101 // Allow softban
102 return true
pineafan4f164f32022-02-26 22:07:12 +0000103}
104
pineafan8b4b17f2022-02-27 20:42:52 +0000105export { command, callback, check };