blob: 1a27a2d63acbe2543fdae4913a50ed4dcf3016fb [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";
pineafan4f164f32022-02-26 22:07:12 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
5import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
6import keyValueList from "../../utils/generateKeyValueList.js";
7
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
10 .setName("ban")
11 .setDescription("Bans a user from the server")
12 .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true))
13 .addStringOption(option => option.setName("reason").setDescription("The reason for the ban").setRequired(false))
14 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are banned | 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))
18
19const callback = async (interaction: CommandInteraction) => {
20 // TODO:[Modals] Replace this with a modal
21 if (await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000022 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000023 .setTitle("Ban")
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 ban <@!${(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()
pineafan8b4b17f2022-02-27 20:42:52 +000040 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000041 .setTitle("Banned")
42 .setDescription(`You have been banned in ${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),
pineafan8b4b17f2022-02-27 20:42:52 +000053 reason: interaction.options.getString("reason") ?? "No reason provided"
pineafan4f164f32022-02-26 22:07:12 +000054 })
55 let failed = (dmd == false && interaction.options.getString("notify") != "no")
56 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000057 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
pineafan4f164f32022-02-26 22:07:12 +000058 .setTitle(`Ban`)
pineafan8b4b17f2022-02-27 20:42:52 +000059 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
pineafan4f164f32022-02-26 22:07:12 +000060 .setStatus(failed ? "Warning" : "Success")
61 ], components: []})
62 } catch {
63 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000064 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000065 .setTitle(`Ban`)
66 .setDescription("Something went wrong and the user was not banned")
67 .setStatus("Danger")
68 ], components: []})
69 }
70 } else {
71 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000072 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +000073 .setTitle(`Ban`)
74 .setDescription("No changes were made")
75 .setStatus("Success")
76 ], components: []})
77 }
78}
79
80const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
81 // 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 banning Nucleus
86 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot ban 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 ban
94 return true
95}
96
97export { command, callback, check };