blob: 1e17e5310017041b0190d4ee86004ae008de213e [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 readConfig from '../../utils/readConfig.js';
8import addPlurals from "../../utils/plurals.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")
34// pluralize("day", interaction.options.getInteger("delete"))
35// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
pineafan377794f2022-04-18 19:01:01 +010036 .send()
37 if (confirmation.success) {
pineafan4f164f32022-02-26 22:07:12 +000038 let dmd = false
pineafan5d1908e2022-02-28 21:34:47 +000039 let dm;
pineafan377794f2022-04-18 19:01:01 +010040 let config = await readConfig(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000041 try {
42 if (interaction.options.getString("notify") != "no") {
pineafan5d1908e2022-02-28 21:34:47 +000043 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan377794f2022-04-18 19:01:01 +010044 embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000045 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000046 .setTitle("Banned")
47 .setDescription(`You have been banned in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000048 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan4f164f32022-02-26 22:07:12 +000049 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010050 ],
51 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
52 .setStyle("LINK")
53 .setLabel(config.moderation.ban.text)
54 .setURL(config.moderation.ban.link)
55 ] : [])]
pineafan4f164f32022-02-26 22:07:12 +000056 })
57 dmd = true
58 }
59 } catch {}
60 try {
pineafane625d782022-05-09 18:04:32 +010061 let member = (interaction.options.getMember("user") as GuildMember)
62 let reason = interaction.options.getString("reason") ?? "No reason provided"
63 member.ban({
pineafan4f164f32022-02-26 22:07:12 +000064 days: Number(interaction.options.getInteger("delete") ?? 0),
pineafane625d782022-05-09 18:04:32 +010065 reason: reason
pineafan4f164f32022-02-26 22:07:12 +000066 })
pineafane625d782022-05-09 18:04:32 +010067 // @ts-ignore
68 const { log, NucleusColors, entry, renderUser, renderDelta } = interaction.user.client.logger
69 let data = {
70 meta: {
71 type: 'memberBan',
72 displayName: 'Member Banned',
73 calculateType: 'guildMemberPunish',
74 color: NucleusColors.red,
75 emoji: "PUNISH.BAN.RED",
76 timestamp: new Date().getTime()
77 },
78 list: {
79 id: entry(member.user.id, `\`${member.user.id}\``),
80 name: entry(member.user.id, renderUser(member.user)),
81 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
82 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
83 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
84 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
85 serverMemberCount: interaction.guild.memberCount,
86 },
87 hidden: {
88 guild: interaction.guild.id
89 }
90 }
91 log(data, member.user.client);
pineafan4f164f32022-02-26 22:07:12 +000092 } catch {
pineafan377794f2022-04-18 19:01:01 +010093 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000094 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000095 .setTitle(`Ban`)
96 .setDescription("Something went wrong and the user was not banned")
97 .setStatus("Danger")
98 ], components: []})
pineafan5d1908e2022-02-28 21:34:47 +000099 if (dmd) await dm.delete()
100 return
pineafan4f164f32022-02-26 22:07:12 +0000101 }
pineafan5d1908e2022-02-28 21:34:47 +0000102 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan377794f2022-04-18 19:01:01 +0100103 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000104 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
105 .setTitle(`Ban`)
106 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
107 .setStatus(failed ? "Warning" : "Success")
108 ], components: []})
pineafan4f164f32022-02-26 22:07:12 +0000109 } else {
pineafan377794f2022-04-18 19:01:01 +0100110 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000111 .setEmoji("PUNISH.BAN.GREEN")
pineafan4f164f32022-02-26 22:07:12 +0000112 .setTitle(`Ban`)
113 .setDescription("No changes were made")
114 .setStatus("Success")
115 ], components: []})
116 }
117}
118
119const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000120 let member = (interaction.member as GuildMember)
121 let me = (interaction.guild.me as GuildMember)
122 let apply = (interaction.options.getMember("user") as GuildMember)
123 if (member == null || me == null || apply == null) throw "That member is not in the server"
124 let memberPos = member.roles ? member.roles.highest.position : 0
125 let mePos = me.roles ? me.roles.highest.position : 0
126 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan4f164f32022-02-26 22:07:12 +0000127 // Check if Nucleus can ban the member
pineafan5d1908e2022-02-28 21:34:47 +0000128 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000129 // Check if Nucleus has permission to ban
PineappleFan5fe720d2022-05-19 12:01:49 +0100130 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the `ban_members` permission";
pineafan4f164f32022-02-26 22:07:12 +0000131 // Do not allow banning Nucleus
pineafan663dc472022-05-10 18:13:47 +0100132 if (member.id == interaction.guild.me.id) throw "I cannot ban myself"
pineafan4f164f32022-02-26 22:07:12 +0000133 // Allow the owner to ban anyone
pineafan663dc472022-05-10 18:13:47 +0100134 if (member.id == interaction.guild.ownerId) return true
pineafan4f164f32022-02-26 22:07:12 +0000135 // Check if the user has ban_members permission
pineafan663dc472022-05-10 18:13:47 +0100136 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the `ban_members` permission";
pineafan4f164f32022-02-26 22:07:12 +0000137 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000138 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan4f164f32022-02-26 22:07:12 +0000139 // Allow ban
140 return true
141}
142
143export { command, callback, check };