blob: 8b17db35e6ffa2be6a66d1306f84647ff4e1cd87 [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";
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
21const callback = async (interaction: CommandInteraction) => {
22 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010023 let confirmation = await new confirmationMessage(interaction)
pineafan8b4b17f2022-02-27 20:42:52 +000024 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000025 .setTitle("Ban")
26 .setDescription(keyValueList({
27 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
28 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
29 })
30 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n`
pineafane625d782022-05-09 18:04:32 +010031 + `${addPlurals(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
pineafan4f164f32022-02-26 22:07:12 +000032 + `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
33 .setColor("Danger")
pineafan377794f2022-04-18 19:01:01 +010034 .send()
35 if (confirmation.success) {
pineafan4f164f32022-02-26 22:07:12 +000036 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000037 let dm;
pineafan6702cef2022-06-13 17:52:37 +010038 let config = await client.database.read(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000039 try {
40 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000041 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010042 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000043 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000044 .setTitle("Banned")
45 .setDescription(`You have been banned in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000046 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan4f164f32022-02-26 22:07:12 +000047 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010048 ],
49 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
50 .setStyle("LINK")
51 .setLabel(config.moderation.ban.text)
52 .setURL(config.moderation.ban.link)
53 ] : [])]
pineafan4f164f32022-02-26 22:07:12 +000054 })
55 dmd = true
56 }
57 } catch {}
58 try {
pineafane625d782022-05-09 18:04:32 +010059 let member = (interaction.options.getMember("user") as GuildMember)
60 let reason = interaction.options.getString("reason") ?? "No reason provided"
61 member.ban({
pineafan4f164f32022-02-26 22:07:12 +000062 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafane625d782022-05-09 18:04:32 +010063 reason: reason
pineafan4f164f32022-02-26 22:07:12 +000064 })
pineafane625d782022-05-09 18:04:32 +010065 // @ts-ignore
66 const { log, NucleusColors, entry, renderUser, renderDelta } = interaction.user.client.logger
67 let data = {
68 meta: {
69 type: 'memberBan',
70 displayName: 'Member Banned',
71 calculateType: 'guildMemberPunish',
72 color: NucleusColors.red,
73 emoji: "PUNISH.BAN.RED",
74 timestamp: new Date().getTime()
75 },
76 list: {
77 id: entry(member.user.id, `\`${member.user.id}\``),
78 name: entry(member.user.id, renderUser(member.user)),
79 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
80 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
81 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
82 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
83 serverMemberCount: interaction.guild.memberCount,
84 },
85 hidden: {
86 guild: interaction.guild.id
87 }
88 }
89 log(data, member.user.client);
pineafan4f164f32022-02-26 22:07:12 +000090 } catch {
pineafan377794f2022-04-18 19:01:01 +010091 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000092 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000093 .setTitle(`Ban`)
94 .setDescription("Something went wrong and the user was not banned")
95 .setStatus("Danger")
96 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000097 if (dmd) await dm.delete()
98 return
pineafan4f164f32022-02-26 22:07:12 +000099 }
pineafan5d1908e2022-02-28 21:34:47 +0000100 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +0100101 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000102 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
103 .setTitle(`Ban`)
104 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
105 .setStatus(failed ? "Warning" : "Success")
106 ], components: []})
pineafan4f164f32022-02-26 22:07:12 +0000107 } else {
pineafan377794f2022-04-18 19:01:01 +0100108 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000109 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +0000110 .setTitle(`Ban`)
111 .setDescription("No changes were made")
112 .setStatus("Success")
113 ], components: []})
114 }
115}
116
117const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000118 let member = (interaction.member as GuildMember)
119 let me = (interaction.guild.me as GuildMember)
120 let apply = (interaction.options.getMember("user") as GuildMember)
121 if (member == null || me == null || apply == null) throw "That member is not in the server"
122 let memberPos = member.roles ? member.roles.highest.position : 0
123 let mePos = me.roles ? me.roles.highest.position : 0
124 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan4f164f32022-02-26 22:07:12 +0000125 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000126 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000127 // Check if Nucleus has permission to ban
PineappleFan5fe720d2022-05-19 12:01:49 +0100128 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
pineafan4f164f32022-02-26 22:07:12 +0000129 // Do not allow banning Nucleus
pineafan663dc472022-05-10 18:13:47 +0100130 if (member.id == interaction.guild.me.id) throw "I cannot ban myself"
pineafan4f164f32022-02-26 22:07:12 +0000131 // Allow the owner to ban anyone
pineafan663dc472022-05-10 18:13:47 +0100132 if (member.id == interaction.guild.ownerId) return true
pineafan4f164f32022-02-26 22:07:12 +0000133 // Check if the user has ban_members permission
pineafan663dc472022-05-10 18:13:47 +0100134 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
pineafan4f164f32022-02-26 22:07:12 +0000135 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000136 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000137 // Allow ban
138 return true
139}
140
141export { command, callback, check };