blob: 7346bcc5aede2dc536035905ff9898a92ff43beb [file] [log] [blame]
PineaFan100df682023-01-02 13:26:08 +00001import Discord, { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, User, ButtonStyle } 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";
PineaFan100df682023-01-02 13:26:08 +00008import { LinkWarningFooter } from "../../utils/defaultEmbeds.js";
pineafan4f164f32022-02-26 22:07:12 +00009
PineaFan64486c42022-12-28 09:21:04 +000010
pineafan4f164f32022-02-26 22:07:12 +000011const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("ban")
14 .setDescription("Bans a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010015 .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true))
Skyler Grey75ea9172022-08-06 10:22:23 +010016 .addNumberOption((option) =>
17 option
18 .setName("delete")
PineaFan100df682023-01-02 13:26:08 +000019 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010020 .setMinValue(0)
21 .setMaxValue(7)
22 .setRequired(false)
23 );
pineafan4f164f32022-02-26 22:07:12 +000024
PineaFan64486c42022-12-28 09:21:04 +000025
pineafanbd02b4a2022-08-05 22:01:38 +010026const callback = async (interaction: CommandInteraction): Promise<void> => {
PineaFana00db1b2023-01-02 15:32:54 +000027 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010028 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000029 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010030 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010031 let notify = true;
32 let confirmation;
pineafan62ce1922022-08-25 20:34:45 +010033 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010034 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010035 do {
pineafan73a7c4a2022-07-24 10:38:04 +010036 confirmation = await new confirmationMessage(interaction)
37 .setEmoji("PUNISH.BAN.RED")
38 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010039 .setDescription(
40 keyValueList({
PineaFan64486c42022-12-28 09:21:04 +000041 user: renderUser(interaction.options.getUser("user")!),
pineafan62ce1922022-08-25 20:34:45 +010042 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010043 }) +
44 `The user **will${notify ? "" : " not"}** be notified\n` +
45 `${addPlurals(
PineaFan100df682023-01-02 13:26:08 +000046 (interaction.options.get("delete")?.value as number | null) ?? 0, "day")
47 } of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010048 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010049 )
pineafan62ce1922022-08-25 20:34:45 +010050 .addCustomBoolean(
51 "notify",
52 "Notify user",
53 false,
54 undefined,
PineaFan100df682023-01-02 13:26:08 +000055 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000056 null,
pineafan62ce1922022-08-25 20:34:45 +010057 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
58 notify
59 )
pineafan73a7c4a2022-07-24 10:38:04 +010060 .setColor("Danger")
61 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010062 .send(reason !== null);
63 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010064 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010065 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010066 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010067 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
68 } while (!timedOut && !chosen)
Skyler Greyad002172022-08-16 18:48:26 +010069 if (timedOut) return;
pineafan62ce1922022-08-25 20:34:45 +010070 if (confirmation.success) {
71 reason = reason.length ? reason : null
PineaFan100df682023-01-02 13:26:08 +000072 let dmSent = false;
73 let dmMessage;
PineaFana00db1b2023-01-02 15:32:54 +000074 const config = await client.database.guilds.read(interaction.guild.id);
pineafan62ce1922022-08-25 20:34:45 +010075 try {
76 if (notify) {
PineaFan100df682023-01-02 13:26:08 +000077 const messageData: {
78 embeds: EmojiEmbed[];
79 components: ActionRowBuilder<ButtonBuilder>[];
80 } = {
pineafan62ce1922022-08-25 20:34:45 +010081 embeds: [
82 new EmojiEmbed()
83 .setEmoji("PUNISH.BAN.RED")
84 .setTitle("Banned")
85 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000086 `You have been banned in ${interaction.guild.name}` + (reason ? ` for:\n> ${reason}` : ".")
pineafan62ce1922022-08-25 20:34:45 +010087 )
88 .setStatus("Danger")
89 ],
PineaFan100df682023-01-02 13:26:08 +000090 components: []
91 };
92 if (config.moderation.ban.text && config.moderation.ban.link) {
93 messageData.embeds[0]!.setFooter(LinkWarningFooter)
94 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
95 .addComponents(new ButtonBuilder()
PineaFan64486c42022-12-28 09:21:04 +000096 .setStyle(ButtonStyle.Link)
97 .setLabel(config.moderation.ban.text)
PineaFan100df682023-01-02 13:26:08 +000098 .setURL(config.moderation.ban.link)
99 )
100 )
101 }
102 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
103 dmSent = true;
pineafan62ce1922022-08-25 20:34:45 +0100104 }
105 } catch {
PineaFan100df682023-01-02 13:26:08 +0000106 dmSent = false;
pineafan62ce1922022-08-25 20:34:45 +0100107 }
108 try {
109 const member = interaction.options.getMember("user") as GuildMember;
PineaFan100df682023-01-02 13:26:08 +0000110 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
pineafan62ce1922022-08-25 20:34:45 +0100111 member.ban({
PineaFan100df682023-01-02 13:26:08 +0000112 deleteMessageSeconds: days * 24 * 60 * 60,
113 reason: reason ?? "*No reason provided*"
pineafan62ce1922022-08-25 20:34:45 +0100114 });
PineaFana00db1b2023-01-02 15:32:54 +0000115 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
pineafan62ce1922022-08-25 20:34:45 +0100116 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
117 const data = {
118 meta: {
119 type: "memberBan",
120 displayName: "Member Banned",
121 calculateType: "guildMemberPunish",
122 color: NucleusColors.red,
123 emoji: "PUNISH.BAN.RED",
124 timestamp: new Date().getTime()
125 },
126 list: {
127 memberId: entry(member.user.id, `\`${member.user.id}\``),
128 name: entry(member.user.id, renderUser(member.user)),
PineaFan100df682023-01-02 13:26:08 +0000129 banned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
pineafan62ce1922022-08-25 20:34:45 +0100130 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
131 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan100df682023-01-02 13:26:08 +0000132 accountCreated: entry(member.user.createdAt.toString(), renderDelta(member.user.createdAt.getTime())),
PineaFana00db1b2023-01-02 15:32:54 +0000133 serverMemberCount: interaction.guild.memberCount
pineafan62ce1922022-08-25 20:34:45 +0100134 },
135 hidden: {
PineaFana00db1b2023-01-02 15:32:54 +0000136 guild: interaction.guild.id
pineafan62ce1922022-08-25 20:34:45 +0100137 }
138 };
139 log(data);
140 } catch {
141 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 embeds: [
143 new EmojiEmbed()
144 .setEmoji("PUNISH.BAN.RED")
pineafan62ce1922022-08-25 20:34:45 +0100145 .setTitle("Ban")
146 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 .setStatus("Danger")
148 ],
pineafan62ce1922022-08-25 20:34:45 +0100149 components: []
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 });
PineaFan100df682023-01-02 13:26:08 +0000151 if (dmSent && dmMessage) await dmMessage.delete();
pineafan62ce1922022-08-25 20:34:45 +0100152 return;
pineafan4f164f32022-02-26 22:07:12 +0000153 }
PineaFan100df682023-01-02 13:26:08 +0000154 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100155 await interaction.editReply({
156 embeds: [
157 new EmojiEmbed()
pineafan62ce1922022-08-25 20:34:45 +0100158 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
Skyler Greyad002172022-08-16 18:48:26 +0100159 .setTitle("Ban")
pineafan62ce1922022-08-25 20:34:45 +0100160 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
161 .setStatus(failed ? "Warning" : "Success")
Skyler Greyad002172022-08-16 18:48:26 +0100162 ],
163 components: []
164 });
pineafan62ce1922022-08-25 20:34:45 +0100165 } else {
166 await interaction.editReply({
167 embeds: [
168 new EmojiEmbed()
169 .setEmoji("PUNISH.BAN.GREEN")
170 .setTitle("Ban")
171 .setDescription("No changes were made")
172 .setStatus("Success")
173 ],
174 components: []
175 });
Skyler Greyad002172022-08-16 18:48:26 +0100176 }
pineafan63fc5e22022-08-04 22:04:10 +0100177};
pineafan4f164f32022-02-26 22:07:12 +0000178
pineafan62ce1922022-08-25 20:34:45 +0100179const check = async (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000180 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000182 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100183 let apply = interaction.options.getUser("user") as User | GuildMember;
184 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
185 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
186 let applyPos = 0
187 try {
PineaFana00db1b2023-01-02 15:32:54 +0000188 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
pineafan62ce1922022-08-25 20:34:45 +0100189 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
190 } catch {
191 apply = apply as User
192 }
pineafanc1c18792022-08-03 21:41:36 +0100193 // Do not allow banning the owner
PineaFana00db1b2023-01-02 15:32:54 +0000194 if (member.id === interaction.guild.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000195 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100196 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000197 // Check if Nucleus has permission to ban
PineaFan100df682023-01-02 13:26:08 +0000198 if (!me.permissions.has("BanMembers")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000199 // Do not allow banning Nucleus
PineaFan100df682023-01-02 13:26:08 +0000200 if (member.id === me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000201 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000202 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000203 // Check if the user has ban_members permission
PineaFan100df682023-01-02 13:26:08 +0000204 if (!member.permissions.has("BanMembers")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000205 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100206 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000207 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100208 return true;
209};
pineafan4f164f32022-02-26 22:07:12 +0000210
Skyler Grey75ea9172022-08-06 10:22:23 +0100211export { command, callback, check };