blob: 11c6c79e3a72090d7e2ff82cd2fa510a37752e4b [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";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafane625d782022-05-09 18:04:32 +01007import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01008import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("ban")
13 .setDescription("Bans a user from the server")
14 .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true))
15 .addStringOption(option => option.setName("reason").setDescription("The reason for the ban").setRequired(false))
16 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are banned | Default yes").setRequired(false)
17 .addChoices([["Yes", "yes"], ["No", "no"]])
18 )
19 .addIntegerOption(option => option.setName("delete").setDescription("The days of messages to delete | Default 0").setMinValue(0).setMaxValue(7).setRequired(false))
20
pineafan4edb7762022-06-26 19:21:04 +010021const callback = async (interaction: CommandInteraction): Promise<any> => {
22 const { renderUser } = client.logger
pineafan4f164f32022-02-26 22:07:12 +000023 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010024 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000025 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000026 .setTitle("Ban")
27 .setDescription(keyValueList({
pineafan4edb7762022-06-26 19:21:04 +010028 "user": renderUser(interaction.options.getUser("user")),
pineafan4f164f32022-02-26 22:07:12 +000029 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
30 })
31 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
pineafane625d782022-05-09 18:04:32 +010032 + `${addPlurals(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
pineafan4f164f32022-02-26 22:07:12 +000033 + `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
34 .setColor("Danger")
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;
pineafan4edb7762022-06-26 19:21:04 +010039 let config = await client.database.guilds.read(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({
pineafan4edb7762022-06-26 19:21:04 +010043 embeds: [new EmojiEmbed()
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 {
pineafane625d782022-05-09 18:04:32 +010060 let member = (interaction.options.getMember("user") as GuildMember)
pineafan4edb7762022-06-26 19:21:04 +010061 let reason = interaction.options.getString("reason")
pineafane625d782022-05-09 18:04:32 +010062 member.ban({
pineafan4f164f32022-02-26 22:07:12 +000063 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafan4edb7762022-06-26 19:21:04 +010064 reason: reason ?? "No reason provided"
pineafan4f164f32022-02-26 22:07:12 +000065 })
pineafan4edb7762022-06-26 19:21:04 +010066 try { await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason) } catch {}
67 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
pineafane625d782022-05-09 18:04:32 +010068 let data = {
69 meta: {
70 type: 'memberBan',
71 displayName: 'Member Banned',
72 calculateType: 'guildMemberPunish',
73 color: NucleusColors.red,
74 emoji: "PUNISH.BAN.RED",
75 timestamp: new Date().getTime()
76 },
77 list: {
pineafanda6e5342022-07-03 10:03:16 +010078 memberId: entry(member.user.id, `\`${member.user.id}\``),
pineafane625d782022-05-09 18:04:32 +010079 name: entry(member.user.id, renderUser(member.user)),
80 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
81 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
82 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
83 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
84 serverMemberCount: interaction.guild.memberCount,
85 },
86 hidden: {
87 guild: interaction.guild.id
88 }
89 }
pineafan4edb7762022-06-26 19:21:04 +010090 log(data);
pineafan4f164f32022-02-26 22:07:12 +000091 } catch {
pineafan4edb7762022-06-26 19:21:04 +010092 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000093 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000094 .setTitle(`Ban`)
95 .setDescription("Something went wrong and the user was not banned")
96 .setStatus("Danger")
97 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000098 if (dmd) await dm.delete()
99 return
pineafan4f164f32022-02-26 22:07:12 +0000100 }
pineafan5d1908e2022-02-28 21:34:47 +0000101 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan4edb7762022-06-26 19:21:04 +0100102 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000103 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
104 .setTitle(`Ban`)
105 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
106 .setStatus(failed ? "Warning" : "Success")
107 ], components: []})
pineafan4f164f32022-02-26 22:07:12 +0000108 } else {
pineafan4edb7762022-06-26 19:21:04 +0100109 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000110 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +0000111 .setTitle(`Ban`)
112 .setDescription("No changes were made")
113 .setStatus("Success")
114 ], components: []})
115 }
116}
117
118const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000119 let member = (interaction.member as GuildMember)
120 let me = (interaction.guild.me as GuildMember)
121 let apply = (interaction.options.getMember("user") as GuildMember)
122 if (member == null || me == null || apply == null) throw "That member is not in the server"
123 let memberPos = member.roles ? member.roles.highest.position : 0
124 let mePos = me.roles ? me.roles.highest.position : 0
125 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan4f164f32022-02-26 22:07:12 +0000126 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000127 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000128 // Check if Nucleus has permission to ban
pineafan4edb7762022-06-26 19:21:04 +0100129 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the Ban members permission";
pineafan4f164f32022-02-26 22:07:12 +0000130 // Do not allow banning Nucleus
pineafan663dc472022-05-10 18:13:47 +0100131 if (member.id == interaction.guild.me.id) throw "I cannot ban myself"
pineafan4f164f32022-02-26 22:07:12 +0000132 // Allow the owner to ban anyone
pineafan663dc472022-05-10 18:13:47 +0100133 if (member.id == interaction.guild.ownerId) return true
pineafan4f164f32022-02-26 22:07:12 +0000134 // Check if the user has ban_members permission
pineafan4edb7762022-06-26 19:21:04 +0100135 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the Ban members permission";
pineafan4f164f32022-02-26 22:07:12 +0000136 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000137 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000138 // Allow ban
139 return true
140}
141
142export { command, callback, check };