blob: e94035e4aee44f328d08e3faeefbff2dbcb0d536 [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";
pineafan4f164f32022-02-26 22:07:12 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan377794f2022-04-18 19:01:01 +01005import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +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("ban")
12 .setDescription("Bans a user from the server")
13 .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true))
14 .addStringOption(option => option.setName("reason").setDescription("The reason for the ban").setRequired(false))
15 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are banned | Default yes").setRequired(false)
16 .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))
19
20const 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")
pineafan4f164f32022-02-26 22:07:12 +000024 .setTitle("Ban")
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 ban <@!${(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) {
pineafan4f164f32022-02-26 22:07:12 +000037 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000038 let dm;
pineafan377794f2022-04-18 19:01:01 +010039 let config = await readConfig(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000040 try {
41 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000042 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010043 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000044 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000045 .setTitle("Banned")
46 .setDescription(`You have been banned in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000047 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan4f164f32022-02-26 22:07:12 +000048 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010049 ],
50 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
51 .setStyle("LINK")
52 .setLabel(config.moderation.ban.text)
53 .setURL(config.moderation.ban.link)
54 ] : [])]
pineafan4f164f32022-02-26 22:07:12 +000055 })
56 dmd = true
57 }
58 } catch {}
59 try {
60 (interaction.options.getMember("user") as GuildMember).ban({
61 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafan8b4b17f2022-02-27 20:42:52 +000062 reason: interaction.options.getString("reason") ?? "No reason provided"
pineafan4f164f32022-02-26 22:07:12 +000063 })
pineafan4f164f32022-02-26 22:07:12 +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")
pineafan4f164f32022-02-26 22:07:12 +000067 .setTitle(`Ban`)
68 .setDescription("Something went wrong and the user was not banned")
69 .setStatus("Danger")
70 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000071 if (dmd) await dm.delete()
72 return
pineafan4f164f32022-02-26 22:07:12 +000073 }
pineafan5d1908e2022-02-28 21:34:47 +000074 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +010075 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000076 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
77 .setTitle(`Ban`)
78 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
79 .setStatus(failed ? "Warning" : "Success")
80 ], components: []})
pineafan4f164f32022-02-26 22:07:12 +000081 } else {
pineafan377794f2022-04-18 19:01:01 +010082 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000083 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +000084 .setTitle(`Ban`)
85 .setDescription("No changes were made")
86 .setStatus("Success")
87 ], components: []})
88 }
89}
90
91const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000092 let member = (interaction.member as GuildMember)
93 let me = (interaction.guild.me as GuildMember)
94 let apply = (interaction.options.getMember("user") as GuildMember)
95 if (member == null || me == null || apply == null) throw "That member is not in the server"
96 let memberPos = member.roles ? member.roles.highest.position : 0
97 let mePos = me.roles ? me.roles.highest.position : 0
98 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan4f164f32022-02-26 22:07:12 +000099 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000100 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000101 // Check if Nucleus has permission to ban
102 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
103 // Do not allow banning Nucleus
104 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot ban myself"
105 // Allow the owner to ban anyone
106 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
107 // Check if the user has ban_members permission
108 if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
109 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000110 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000111 // Allow ban
112 return true
113}
114
115export { command, callback, check };