blob: 92ed3a7662c4a8428b45f33f71904f354a146fe3 [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
pineafan5d1908e2022-02-28 21:34:47 +000036 let dm;
pineafan4f164f32022-02-26 22:07:12 +000037 try {
38 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000039 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4f164f32022-02-26 22:07:12 +000040 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000041 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000042 .setTitle("Banned")
43 .setDescription(`You have been banned in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000044 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan4f164f32022-02-26 22:07:12 +000045 .setStatus("Danger")
46 ]
47 })
48 dmd = true
49 }
50 } catch {}
51 try {
52 (interaction.options.getMember("user") as GuildMember).ban({
53 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafan8b4b17f2022-02-27 20:42:52 +000054 reason: interaction.options.getString("reason") ?? "No reason provided"
pineafan4f164f32022-02-26 22:07:12 +000055 })
pineafan4f164f32022-02-26 22:07:12 +000056 } catch {
57 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000058 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000059 .setTitle(`Ban`)
60 .setDescription("Something went wrong and the user was not banned")
61 .setStatus("Danger")
62 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000063 if (dmd) await dm.delete()
64 return
pineafan4f164f32022-02-26 22:07:12 +000065 }
pineafan5d1908e2022-02-28 21:34:47 +000066 let failed = (dmd == false && interaction.options.getString("notify") != "no")
67 await interaction.editReply({embeds: [new EmojiEmbed()
68 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
69 .setTitle(`Ban`)
70 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
71 .setStatus(failed ? "Warning" : "Success")
72 ], components: []})
pineafan4f164f32022-02-26 22:07:12 +000073 } else {
74 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000075 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +000076 .setTitle(`Ban`)
77 .setDescription("No changes were made")
78 .setStatus("Success")
79 ], components: []})
80 }
81}
82
83const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000084 let member = (interaction.member as GuildMember)
85 let me = (interaction.guild.me as GuildMember)
86 let apply = (interaction.options.getMember("user") as GuildMember)
87 if (member == null || me == null || apply == null) throw "That member is not in the server"
88 let memberPos = member.roles ? member.roles.highest.position : 0
89 let mePos = me.roles ? me.roles.highest.position : 0
90 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan4f164f32022-02-26 22:07:12 +000091 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +000092 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +000093 // Check if Nucleus has permission to ban
94 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
95 // Do not allow banning Nucleus
96 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot ban myself"
97 // Allow the owner to ban anyone
98 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
99 // Check if the user has ban_members permission
100 if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
101 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000102 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000103 // Allow ban
104 return true
105}
106
107export { command, callback, check };