blob: 9a82970cc038f6c817b7016e1c0fd75633dd8683 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01002import type { 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")
Skyler Grey11236ba2022-08-08 21:13:33 +010013 .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true))
Skyler Grey75ea9172022-08-06 10:22:23 +010014 .addNumberOption((option) =>
15 option
16 .setName("delete")
17 .setDescription("The days of messages to delete | Default: 0")
18 .setMinValue(0)
19 .setMaxValue(7)
20 .setRequired(false)
21 );
pineafan4f164f32022-02-26 22:07:12 +000022
pineafanbd02b4a2022-08-05 22:01:38 +010023const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan63fc5e22022-08-04 22:04:10 +010024 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000025 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010026 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010027 let notify = true;
28 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010029 let cancelled = false;
30 let timedOut = false;
31 while (!timedOut && !cancelled) {
pineafan73a7c4a2022-07-24 10:38:04 +010032 confirmation = await new confirmationMessage(interaction)
33 .setEmoji("PUNISH.BAN.RED")
34 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010035 .setDescription(
36 keyValueList({
37 user: renderUser(interaction.options.getUser("user")),
Skyler Grey11236ba2022-08-08 21:13:33 +010038 reason: reason ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010039 }) +
40 `The user **will${notify ? "" : " not"}** be notified\n` +
41 `${addPlurals(
Skyler Grey11236ba2022-08-08 21:13:33 +010042 interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0,
Skyler Grey75ea9172022-08-06 10:22:23 +010043 "day"
44 )} of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010045 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010046 )
pineafan73a7c4a2022-07-24 10:38:04 +010047 .setColor("Danger")
48 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010049 .send(reason !== null);
50 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010051 if (confirmation.cancelled) timedOut = true;
52 else if (confirmation.success) cancelled = true;
53 else if (confirmation.newReason) reason = confirmation.newReason;
54 else if (confirmation.components) notify = confirmation.components.notify.active;
pineafan73a7c4a2022-07-24 10:38:04 +010055 }
Skyler Greyad002172022-08-16 18:48:26 +010056 if (timedOut) return;
57 let dmd = false;
58 let dm;
59 const config = await client.database.guilds.read(interaction.guild.id);
60 try {
61 if (notify) {
62 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010063 embeds: [
64 new EmojiEmbed()
65 .setEmoji("PUNISH.BAN.RED")
Skyler Greyad002172022-08-16 18:48:26 +010066 .setTitle("Banned")
67 .setDescription(
68 `You have been banned in ${interaction.guild.name}` + (reason ? ` for:\n> ${reason}` : ".")
69 )
Skyler Grey75ea9172022-08-06 10:22:23 +010070 .setStatus("Danger")
71 ],
Skyler Greyad002172022-08-16 18:48:26 +010072 components: [
73 new MessageActionRow().addComponents(
74 config.moderation.ban.text
75 ? [
76 new MessageButton()
77 .setStyle("LINK")
78 .setLabel(config.moderation.ban.text)
79 .setURL(config.moderation.ban.link)
80 ]
81 : []
82 )
83 ]
Skyler Grey75ea9172022-08-06 10:22:23 +010084 });
Skyler Greyad002172022-08-16 18:48:26 +010085 dmd = true;
pineafan4f164f32022-02-26 22:07:12 +000086 }
Skyler Greyad002172022-08-16 18:48:26 +010087 } catch {
88 dmd = false;
pineafan4f164f32022-02-26 22:07:12 +000089 }
Skyler Greyad002172022-08-16 18:48:26 +010090 try {
91 const member = interaction.options.getMember("user") as GuildMember;
92 member.ban({
93 days: Number(interaction.options.getNumber("delete") ?? 0),
94 reason: reason ?? "No reason provided"
95 });
96 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
97 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
98 const data = {
99 meta: {
100 type: "memberBan",
101 displayName: "Member Banned",
102 calculateType: "guildMemberPunish",
103 color: NucleusColors.red,
104 emoji: "PUNISH.BAN.RED",
105 timestamp: new Date().getTime()
106 },
107 list: {
108 memberId: entry(member.user.id, `\`${member.user.id}\``),
109 name: entry(member.user.id, renderUser(member.user)),
110 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
111 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
112 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
113 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
114 serverMemberCount: interaction.guild.memberCount
115 },
116 hidden: {
117 guild: interaction.guild.id
118 }
119 };
120 log(data);
121 } catch {
122 await interaction.editReply({
123 embeds: [
124 new EmojiEmbed()
125 .setEmoji("PUNISH.BAN.RED")
126 .setTitle("Ban")
127 .setDescription("Something went wrong and the user was not banned")
128 .setStatus("Danger")
129 ],
130 components: []
131 });
132 if (dmd) await dm.delete();
133 return;
134 }
135 const failed = !dmd && notify;
136 await interaction.editReply({
137 embeds: [
138 new EmojiEmbed()
139 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
140 .setTitle("Ban")
141 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
142 .setStatus(failed ? "Warning" : "Success")
143 ],
144 components: []
145 });
pineafan63fc5e22022-08-04 22:04:10 +0100146};
pineafan4f164f32022-02-26 22:07:12 +0000147
pineafanbd02b4a2022-08-05 22:01:38 +0100148const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 const member = interaction.member as GuildMember;
150 const me = interaction.guild.me!;
151 const apply = interaction.options.getMember("user") as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100152 if (member === null || me === null || apply === null) throw new Error("That member is not in the server");
pineafan63fc5e22022-08-04 22:04:10 +0100153 const memberPos = member.roles ? member.roles.highest.position : 0;
154 const mePos = me.roles ? me.roles.highest.position : 0;
155 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100156 // Do not allow banning the owner
pineafan3a02ea32022-08-11 21:35:04 +0100157 if (member.id === interaction.guild.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000158 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100159 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000160 // Check if Nucleus has permission to ban
pineafan3a02ea32022-08-11 21:35:04 +0100161 if (!me.permissions.has("BAN_MEMBERS")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000162 // Do not allow banning Nucleus
pineafan3a02ea32022-08-11 21:35:04 +0100163 if (member.id === interaction.guild.me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000164 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100165 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000166 // Check if the user has ban_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100167 if (!member.permissions.has("BAN_MEMBERS")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000168 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100169 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000170 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100171 return true;
172};
pineafan4f164f32022-02-26 22:07:12 +0000173
Skyler Grey75ea9172022-08-06 10:22:23 +0100174export { command, callback, check };