blob: 57db408863d10773d3855a5a4329769e36645b00 [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
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("ban")
13 .setDescription("Bans a user from the server")
14 .addUserOption(option => option.setName("user").setDescription("The user to ban").setRequired(true))
15 .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> => {
pineafan63fc5e22022-08-04 22:04:10 +010018 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000019 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +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 })
pineafan63fc5e22022-08-04 22:04:10 +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 ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010036 .send(reason !== null);
37 reason = reason ?? "";
38 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) {
pineafan63fc5e22022-08-04 22:04:10 +010044 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000045 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010046 const 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 ] : [])]
pineafan63fc5e22022-08-04 22:04:10 +010062 });
63 dmd = true;
pineafan4f164f32022-02-26 22:07:12 +000064 }
pineafan63fc5e22022-08-04 22:04:10 +010065 } catch { dmd = false; }
pineafan4f164f32022-02-26 22:07:12 +000066 try {
pineafan63fc5e22022-08-04 22:04:10 +010067 const 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"
pineafan63fc5e22022-08-04 22:04:10 +010071 });
72 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
73 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
74 const data = {
pineafane625d782022-05-09 18:04:32 +010075 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010076 type: "memberBan",
77 displayName: "Member Banned",
78 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +010079 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)),
pineafan63fc5e22022-08-04 22:04:10 +010090 serverMemberCount: interaction.guild.memberCount
pineafane625d782022-05-09 18:04:32 +010091 },
92 hidden: {
93 guild: interaction.guild.id
94 }
pineafan63fc5e22022-08-04 22:04:10 +010095 };
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")
pineafan63fc5e22022-08-04 22:04:10 +0100100 .setTitle("Ban")
pineafan4f164f32022-02-26 22:07:12 +0000101 .setDescription("Something went wrong and the user was not banned")
102 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +0100103 ], components: []});
104 if (dmd) await dm.delete();
105 return;
pineafan4f164f32022-02-26 22:07:12 +0000106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107 const 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"}`)
pineafan63fc5e22022-08-04 22:04:10 +0100110 .setTitle("Ban")
pineafan5d1908e2022-02-28 21:34:47 +0000111 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
112 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +0100113 ], 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")
pineafan63fc5e22022-08-04 22:04:10 +0100117 .setTitle("Ban")
pineafan4f164f32022-02-26 22:07:12 +0000118 .setDescription("No changes were made")
119 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100120 ], components: []});
pineafan4f164f32022-02-26 22:07:12 +0000121 }
pineafan63fc5e22022-08-04 22:04:10 +0100122};
pineafan4f164f32022-02-26 22:07:12 +0000123
124const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100125 const member = (interaction.member as GuildMember);
126 const me = (interaction.guild.me as GuildMember);
127 const apply = (interaction.options.getMember("user") as GuildMember);
128 if (member === null || me === null || apply === null) throw "That member is not in the server";
129 const memberPos = member.roles ? member.roles.highest.position : 0;
130 const mePos = me.roles ? me.roles.highest.position : 0;
131 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100132 // Do not allow banning the owner
pineafan63fc5e22022-08-04 22:04:10 +0100133 if (member.id === interaction.guild.ownerId) throw "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000134 // Check if Nucleus can ban the member
pineafan63fc5e22022-08-04 22:04:10 +0100135 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan4f164f32022-02-26 22:07:12 +0000136 // Check if Nucleus has permission to ban
pineafane23c4ec2022-07-27 21:56:27 +0100137 if (! me.permissions.has("BAN_MEMBERS")) throw "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000138 // Do not allow banning Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100139 if (member.id === interaction.guild.me.id) throw "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000140 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100141 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000142 // Check if the user has ban_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100143 if (! member.permissions.has("BAN_MEMBERS")) throw "You do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000144 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100145 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan4f164f32022-02-26 22:07:12 +0000146 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100147 return true;
148};
pineafan4f164f32022-02-26 22:07:12 +0000149
150export { command, callback, check };