blob: 8946e17a6dcd894855da3f122633d696c6bdb6fd [file] [log] [blame]
pineafan4f164f32022-02-26 22:07:12 +00001import Discord, { CommandInteraction, GuildMember } from "discord.js";
2import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
4import getEmojiByName from "../../utils/getEmojiByName.js";
5import confirmationMessage from "../../utils/confirmationMessage.js";
6import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
7import keyValueList from "../../utils/generateKeyValueList.js";
8
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
22 if (await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.BAN")
24 .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" }
35 .send()) {
36 let dmd = false
37 try {
38 if (interaction.options.getString("notify") != "no") {
39 await (interaction.options.getMember("user") as GuildMember).send({
40 embeds: [new EmojiEmbed()
41 .setEmoji("MEMBER.BAN")
42 .setTitle("Banned")
43 .setDescription(`You have been banned in ${interaction.guild.name}` +
44 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided."))
45 .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),
54 reason: interaction.options.getString("reason")
55 })
56 let failed = (dmd == false && interaction.options.getString("notify") != "no")
57 await interaction.editReply({embeds: [new EmojiEmbed()
58 .setEmoji(`PUNISH.${failed ? "SOFT" : ""}BAN`) // TODO: Add green ban icon for success
59 .setTitle(`Ban`)
60 .setDescription("The member was banned" + (failed ? ", but the user could not be messaged" : ""))
61 .setStatus(failed ? "Warning" : "Success")
62 ], components: []})
63 } catch {
64 await interaction.editReply({embeds: [new EmojiEmbed()
65 .setEmoji("MEMBER.BAN")
66 .setTitle(`Ban`)
67 .setDescription("Something went wrong and the user was not banned")
68 .setStatus("Danger")
69 ], components: []})
70 }
71 } else {
72 await interaction.editReply({embeds: [new EmojiEmbed()
73 .setEmoji("MEMBER.UNBAN")
74 .setTitle(`Ban`)
75 .setDescription("No changes were made")
76 .setStatus("Success")
77 ], components: []})
78 }
79}
80
81const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
82 // Check if Nucleus can ban the member
83 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"
84 // Check if Nucleus has permission to ban
85 if (! interaction.guild.me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
86 // Do not allow banning Nucleus
87 if ((interaction.member as GuildMember).id == interaction.guild.me.id) throw "I cannot ban myself"
88 // Allow the owner to ban anyone
89 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
90 // Check if the user has ban_members permission
91 if (! (interaction.member as GuildMember).permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
92 // Check if the user is below on the role list
93 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"
94 // Allow ban
95 return true
96}
97
98export { command, callback, check };