blob: 6da3a6835145a15250892781afd2986092aefb8f [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")
41 .setTitle("Kick")
42 .setDescription(`You have been kicked from ${interaction.guild.name}` +
43 (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 {
51 (interaction.options.getMember("user") as GuildMember).ban({
52 days: Number(interaction.options.getInteger("delete") ?? 0),
53 reason: interaction.options.getString("reason")
54 }) // TODO: unban here
55 let failed = (dmd == false && interaction.options.getString("notify") != "no")
56 await interaction.editReply({embeds: [new EmojiEmbed()
57 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
58 .setTitle(`Softban`)
59 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
60 .setStatus(failed ? "Warning" : "Success")
61 ], components: []})
62 } catch {
63 await interaction.editReply({embeds: [new EmojiEmbed()
64 .setEmoji("PUNISH.BAN.RED")
65 .setTitle(`Softban`)
66 .setDescription("Something went wrong and the user was not softbanned")
67 .setStatus("Danger")
68 ], components: []})
69 }
70 } else {
71 await interaction.editReply({embeds: [new EmojiEmbed()
72 .setEmoji("PUNISH.BAN.GREEN")
73 .setTitle(`Softban`)
74 .setDescription("No changes were made")
75 .setStatus("Success")
76 ], components: []})
77 }
pineafan4f164f32022-02-26 22:07:12 +000078}
79
80const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan8b4b17f2022-02-27 20:42:52 +000081 // Check if Nucleus can ban the member
82 if (! (interaction.guild.me.roles.highest.position > (interaction.member as GuildMember).roles.highest.position)) throw "I do not have a role higher than that member"
83 // Check if Nucleus has permission to ban
84 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
85 // Do not allow softbanning Nucleus
86 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot softban myself"
87 // Allow the owner to ban anyone
88 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
89 // Check if the user has ban_members permission
90 if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
91 // Check if the user is below on the role list
92 if (! ((interaction.member as GuildMember).roles.highest.position > (interaction.options.getMember("user") as GuildMember).roles.highest.position)) throw "You do not have a role higher than that member"
93 // Allow softban
94 return true
pineafan4f164f32022-02-26 22:07:12 +000095}
96
pineafan8b4b17f2022-02-27 20:42:52 +000097export { command, callback, check };