blob: a55dcfa4668a40e4147b9604b8ecbbf38530938c [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";
pineafan4f164f32022-02-26 22:07:12 +00003import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00005import keyValueList from "../../utils/generateKeyValueList.js";
pineafane625d782022-05-09 18:04:32 +01006import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("ban")
12 .setDescription("Bans a user from the server")
13 .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true))
14 .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 +000015
pineafanbd02b4a2022-08-05 22:01:38 +010016const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan63fc5e22022-08-04 22:04:10 +010017 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000018 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010019 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010020 let notify = true;
21 let confirmation;
pineafan73a7c4a2022-07-24 10:38:04 +010022 while (true) {
23 confirmation = await new confirmationMessage(interaction)
24 .setEmoji("PUNISH.BAN.RED")
25 .setTitle("Ban")
26 .setDescription(keyValueList({
27 "user": renderUser(interaction.options.getUser("user")),
28 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
29 })
pineafan63fc5e22022-08-04 22:04:10 +010030 + `The user **will${notify ? "" : " not"}** be notified\n`
pineafan73a7c4a2022-07-24 10:38:04 +010031 + `${addPlurals(interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0, "day")} of messages will be deleted\n\n`
32 + `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
33 .setColor("Danger")
34 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010035 .send(reason !== null);
36 reason = reason ?? "";
37 if (confirmation.cancelled) return;
38 if (confirmation.success) break;
39 if (confirmation.newReason) reason = confirmation.newReason;
40 if (confirmation.components) notify = confirmation.components.notify.active;
pineafan73a7c4a2022-07-24 10:38:04 +010041 }
pineafan377794f2022-04-18 19:01:01 +010042 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010043 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000044 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010045 const config = await client.database.guilds.read(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000046 try {
pineafan02ba0232022-07-24 22:16:15 +010047 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000048 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010049 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000050 .setEmoji("PUNISH.BAN.RED")
pineafan4f164f32022-02-26 22:07:12 +000051 .setTitle("Banned")
52 .setDescription(`You have been banned in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010053 (reason ? ` for:\n> ${reason}` : "."))
pineafan4f164f32022-02-26 22:07:12 +000054 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010055 ],
56 components: [new MessageActionRow().addComponents(config.moderation.ban.text ? [new MessageButton()
57 .setStyle("LINK")
58 .setLabel(config.moderation.ban.text)
59 .setURL(config.moderation.ban.link)
60 ] : [])]
pineafan63fc5e22022-08-04 22:04:10 +010061 });
62 dmd = true;
pineafan4f164f32022-02-26 22:07:12 +000063 }
pineafan63fc5e22022-08-04 22:04:10 +010064 } catch { dmd = false; }
pineafan4f164f32022-02-26 22:07:12 +000065 try {
pineafan63fc5e22022-08-04 22:04:10 +010066 const member = (interaction.options.getMember("user") as GuildMember);
pineafane625d782022-05-09 18:04:32 +010067 member.ban({
pineafan02ba0232022-07-24 22:16:15 +010068 days: Number(interaction.options.getNumber("delete") ?? 0),
pineafan4edb7762022-06-26 19:21:04 +010069 reason: reason ?? "No reason provided"
pineafan63fc5e22022-08-04 22:04:10 +010070 });
71 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
72 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
73 const data = {
pineafane625d782022-05-09 18:04:32 +010074 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010075 type: "memberBan",
76 displayName: "Member Banned",
77 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010078 color: NucleusColors.red,
79 emoji: "PUNISH.BAN.RED",
80 timestamp: new Date().getTime()
81 },
82 list: {
pineafanda6e5342022-07-03 10:03:16 +010083 memberId: entry(member.user.id, `\`${member.user.id}\``),
pineafane625d782022-05-09 18:04:32 +010084 name: entry(member.user.id, renderUser(member.user)),
85 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
86 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
87 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
88 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
pineafan63fc5e22022-08-04 22:04:10 +010089 serverMemberCount: interaction.guild.memberCount
pineafane625d782022-05-09 18:04:32 +010090 },
91 hidden: {
92 guild: interaction.guild.id
93 }
pineafan63fc5e22022-08-04 22:04:10 +010094 };
pineafan4edb7762022-06-26 19:21:04 +010095 log(data);
pineafan4f164f32022-02-26 22:07:12 +000096 } catch {
pineafan4edb7762022-06-26 19:21:04 +010097 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000098 .setEmoji("PUNISH.BAN.RED")
pineafan63fc5e22022-08-04 22:04:10 +010099 .setTitle("Ban")
pineafan4f164f32022-02-26 22:07:12 +0000100 .setDescription("Something went wrong and the user was not banned")
101 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +0100102 ], components: []});
103 if (dmd) await dm.delete();
104 return;
pineafan4f164f32022-02-26 22:07:12 +0000105 }
pineafan63fc5e22022-08-04 22:04:10 +0100106 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +0100107 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000108 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +0100109 .setTitle("Ban")
pineafan5d1908e2022-02-28 21:34:47 +0000110 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
111 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +0100112 ], components: []});
pineafan4f164f32022-02-26 22:07:12 +0000113 } else {
pineafan4edb7762022-06-26 19:21:04 +0100114 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000115 .setEmoji("PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100116 .setTitle("Ban")
pineafan4f164f32022-02-26 22:07:12 +0000117 .setDescription("No changes were made")
118 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100119 ], components: []});
pineafan4f164f32022-02-26 22:07:12 +0000120 }
pineafan63fc5e22022-08-04 22:04:10 +0100121};
pineafan4f164f32022-02-26 22:07:12 +0000122
pineafanbd02b4a2022-08-05 22:01:38 +0100123const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +0100124 const member = (interaction.member as GuildMember);
125 const me = (interaction.guild.me as GuildMember);
126 const apply = (interaction.options.getMember("user") as GuildMember);
127 if (member === null || me === null || apply === null) throw "That member is not in the server";
128 const memberPos = member.roles ? member.roles.highest.position : 0;
129 const mePos = me.roles ? me.roles.highest.position : 0;
130 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100131 // Do not allow banning the owner
pineafan63fc5e22022-08-04 22:04:10 +0100132 if (member.id === interaction.guild.ownerId) throw "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000133 // Check if Nucleus can ban the member
pineafan63fc5e22022-08-04 22:04:10 +0100134 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan4f164f32022-02-26 22:07:12 +0000135 // Check if Nucleus has permission to ban
pineafane23c4ec2022-07-27 21:56:27 +0100136 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000137 // Do not allow banning Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100138 if (member.id === interaction.guild.me.id) throw "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000139 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100140 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000141 // Check if the user has ban_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100142 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000143 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100144 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan4f164f32022-02-26 22:07:12 +0000145 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100146 return true;
147};
pineafan4f164f32022-02-26 22:07:12 +0000148
149export { command, callback, check };