blob: 2068b15a9ae079405540b0c1232c64688a0f3bd2 [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))
pineafan02ba0232022-07-24 22:16:15 +010015 .addNumberOption(option => option.setName("delete").setDescription("The days of messages to delete | Default: 0").setMinValue(0).setMaxValue(7).setRequired(false))
pineafan4f164f32022-02-26 22:07:12 +000016
pineafan4edb7762022-06-26 19:21:04 +010017const callback = async (interaction: CommandInteraction): Promise<any> => {
18 const { renderUser } = client.logger
pineafan4f164f32022-02-26 22:07:12 +000019 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010020 let reason = null
pineafan02ba0232022-07-24 22:16:15 +010021 let notify = true;
22 let confirmation;
pineafan73a7c4a2022-07-24 10:38:04 +010023 while (true) {
24 confirmation = await new confirmationMessage(interaction)
25 .setEmoji("PUNISH.BAN.RED")
26 .setTitle("Ban")
27 .setDescription(keyValueList({
28 "user": renderUser(interaction.options.getUser("user")),
29 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
30 })
pineafan02ba0232022-07-24 22:16:15 +010031 + `The user **will${notify ? '' : ' not'}** be notified\n`
pineafan73a7c4a2022-07-24 10:38:04 +010032 + `${addPlurals(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
33 + `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
34 .setColor("Danger")
35 .addReasonButton(reason ?? "")
36 .send(reason !== null)
37 reason = reason ?? ""
pineafan02ba0232022-07-24 22:16:15 +010038 if (confirmation.cancelled) return
39 if (confirmation.success) break
40 if (confirmation.newReason) reason = confirmation.newReason
41 if (confirmation.components) notify = confirmation.components.notify.active
pineafan73a7c4a2022-07-24 10:38:04 +010042 }
pineafan377794f2022-04-18 19:01:01 +010043 if (confirmation.success) {
pineafan4f164f32022-02-26 22:07:12 +000044 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000045 let dm;
pineafan4edb7762022-06-26 19:21:04 +010046 let config = await client.database.guilds.read(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000047 try {
pineafan02ba0232022-07-24 22:16:15 +010048 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000049 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010050 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000051 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000052 .setTitle("Banned")
53 .setDescription(`You have been banned in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010054 (reason ? ` for:\n> ${reason}` : "."))
pineafan4f164f32022-02-26 22:07:12 +000055 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010056 ],
57 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
58 .setStyle("LINK")
59 .setLabel(config.moderation.ban.text)
60 .setURL(config.moderation.ban.link)
61 ] : [])]
pineafan4f164f32022-02-26 22:07:12 +000062 })
63 dmd = true
64 }
65 } catch {}
66 try {
pineafane625d782022-05-09 18:04:32 +010067 let member = (interaction.options.getMember("user") as GuildMember)
pineafane625d782022-05-09 18:04:32 +010068 member.ban({
pineafan02ba0232022-07-24 22:16:15 +010069 days: Number(interaction.options.getNumber("delete") ?? 0),
pineafan4edb7762022-06-26 19:21:04 +010070 reason: reason ?? "No reason provided"
pineafan4f164f32022-02-26 22:07:12 +000071 })
pineafan4edb7762022-06-26 19:21:04 +010072 try { await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason) } catch {}
73 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
pineafane625d782022-05-09 18:04:32 +010074 let data = {
75 meta: {
76 type: 'memberBan',
77 displayName: 'Member Banned',
78 calculateType: 'guildMemberPunish',
79 color: NucleusColors.red,
80 emoji: "PUNISH.BAN.RED",
81 timestamp: new Date().getTime()
82 },
83 list: {
pineafanda6e5342022-07-03 10:03:16 +010084 memberId: entry(member.user.id, `\`${member.user.id}\``),
pineafane625d782022-05-09 18:04:32 +010085 name: entry(member.user.id, renderUser(member.user)),
86 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
87 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
88 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
89 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
90 serverMemberCount: interaction.guild.memberCount,
91 },
92 hidden: {
93 guild: interaction.guild.id
94 }
95 }
pineafan4edb7762022-06-26 19:21:04 +010096 log(data);
pineafan4f164f32022-02-26 22:07:12 +000097 } catch {
pineafan4edb7762022-06-26 19:21:04 +010098 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000099 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +0000100 .setTitle(`Ban`)
101 .setDescription("Something went wrong and the user was not banned")
102 .setStatus("Danger")
103 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +0000104 if (dmd) await dm.delete()
105 return
pineafan4f164f32022-02-26 22:07:12 +0000106 }
pineafan02ba0232022-07-24 22:16:15 +0100107 let failed = (dmd == false && notify)
pineafan4edb7762022-06-26 19:21:04 +0100108 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000109 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
110 .setTitle(`Ban`)
111 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
112 .setStatus(failed ? "Warning" : "Success")
113 ], components: []})
pineafan4f164f32022-02-26 22:07:12 +0000114 } else {
pineafan4edb7762022-06-26 19:21:04 +0100115 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000116 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +0000117 .setTitle(`Ban`)
118 .setDescription("No changes were made")
119 .setStatus("Success")
120 ], components: []})
121 }
122}
123
124const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000125 let member = (interaction.member as GuildMember)
126 let me = (interaction.guild.me as GuildMember)
127 let apply = (interaction.options.getMember("user") as GuildMember)
128 if (member == null || me == null || apply == null) throw "That member is not in the server"
129 let memberPos = member.roles ? member.roles.highest.position : 0
130 let mePos = me.roles ? me.roles.highest.position : 0
131 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan4f164f32022-02-26 22:07:12 +0000132 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000133 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000134 // Check if Nucleus has permission to ban
pineafan4edb7762022-06-26 19:21:04 +0100135 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the Ban members permission";
pineafan4f164f32022-02-26 22:07:12 +0000136 // Do not allow banning Nucleus
pineafan663dc472022-05-10 18:13:47 +0100137 if (member.id == interaction.guild.me.id) throw "I cannot ban myself"
pineafan4f164f32022-02-26 22:07:12 +0000138 // Allow the owner to ban anyone
pineafan663dc472022-05-10 18:13:47 +0100139 if (member.id == interaction.guild.ownerId) return true
pineafan4f164f32022-02-26 22:07:12 +0000140 // Check if the user has ban_members permission
pineafan4edb7762022-06-26 19:21:04 +0100141 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the Ban members permission";
pineafan4f164f32022-02-26 22:07:12 +0000142 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000143 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000144 // Allow ban
145 return true
146}
147
148export { command, callback, check };