blob: ba60d1ebf2e6a5269d26fb39953165dbe0f9d085 [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) {
PineaFan538d3752023-01-12 21:48:23 +000077 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
PineaFan100df682023-01-02 13:26:08 +000078 const messageData: {
79 embeds: EmojiEmbed[];
80 components: ActionRowBuilder<ButtonBuilder>[];
81 } = {
pineafan62ce1922022-08-25 20:34:45 +010082 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.BAN.RED")
85 .setTitle("Banned")
86 .setDescription(
PineaFan538d3752023-01-12 21:48:23 +000087 `You have been banned in ${interaction.guild.name}` +
88 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
pineafan62ce1922022-08-25 20:34:45 +010089 )
90 .setStatus("Danger")
91 ],
PineaFan100df682023-01-02 13:26:08 +000092 components: []
93 };
94 if (config.moderation.ban.text && config.moderation.ban.link) {
95 messageData.embeds[0]!.setFooter(LinkWarningFooter)
96 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
97 .addComponents(new ButtonBuilder()
PineaFan64486c42022-12-28 09:21:04 +000098 .setStyle(ButtonStyle.Link)
99 .setLabel(config.moderation.ban.text)
PineaFan100df682023-01-02 13:26:08 +0000100 .setURL(config.moderation.ban.link)
101 )
102 )
103 }
104 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
105 dmSent = true;
pineafan62ce1922022-08-25 20:34:45 +0100106 }
107 } catch {
PineaFan100df682023-01-02 13:26:08 +0000108 dmSent = false;
pineafan62ce1922022-08-25 20:34:45 +0100109 }
110 try {
111 const member = interaction.options.getMember("user") as GuildMember;
PineaFan100df682023-01-02 13:26:08 +0000112 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
pineafan62ce1922022-08-25 20:34:45 +0100113 member.ban({
PineaFan100df682023-01-02 13:26:08 +0000114 deleteMessageSeconds: days * 24 * 60 * 60,
115 reason: reason ?? "*No reason provided*"
pineafan62ce1922022-08-25 20:34:45 +0100116 });
PineaFana00db1b2023-01-02 15:32:54 +0000117 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
pineafan62ce1922022-08-25 20:34:45 +0100118 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
119 const data = {
120 meta: {
121 type: "memberBan",
122 displayName: "Member Banned",
123 calculateType: "guildMemberPunish",
124 color: NucleusColors.red,
125 emoji: "PUNISH.BAN.RED",
126 timestamp: new Date().getTime()
127 },
128 list: {
129 memberId: entry(member.user.id, `\`${member.user.id}\``),
130 name: entry(member.user.id, renderUser(member.user)),
PineaFan100df682023-01-02 13:26:08 +0000131 banned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
pineafan62ce1922022-08-25 20:34:45 +0100132 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
133 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan100df682023-01-02 13:26:08 +0000134 accountCreated: entry(member.user.createdAt.toString(), renderDelta(member.user.createdAt.getTime())),
PineaFana00db1b2023-01-02 15:32:54 +0000135 serverMemberCount: interaction.guild.memberCount
pineafan62ce1922022-08-25 20:34:45 +0100136 },
137 hidden: {
PineaFana00db1b2023-01-02 15:32:54 +0000138 guild: interaction.guild.id
pineafan62ce1922022-08-25 20:34:45 +0100139 }
140 };
141 log(data);
142 } catch {
143 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 embeds: [
145 new EmojiEmbed()
146 .setEmoji("PUNISH.BAN.RED")
pineafan62ce1922022-08-25 20:34:45 +0100147 .setTitle("Ban")
148 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 .setStatus("Danger")
150 ],
pineafan62ce1922022-08-25 20:34:45 +0100151 components: []
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 });
PineaFan100df682023-01-02 13:26:08 +0000153 if (dmSent && dmMessage) await dmMessage.delete();
pineafan62ce1922022-08-25 20:34:45 +0100154 return;
pineafan4f164f32022-02-26 22:07:12 +0000155 }
PineaFan100df682023-01-02 13:26:08 +0000156 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100157 await interaction.editReply({
158 embeds: [
159 new EmojiEmbed()
pineafan62ce1922022-08-25 20:34:45 +0100160 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
Skyler Greyad002172022-08-16 18:48:26 +0100161 .setTitle("Ban")
pineafan62ce1922022-08-25 20:34:45 +0100162 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
163 .setStatus(failed ? "Warning" : "Success")
Skyler Greyad002172022-08-16 18:48:26 +0100164 ],
165 components: []
166 });
pineafan62ce1922022-08-25 20:34:45 +0100167 } else {
168 await interaction.editReply({
169 embeds: [
170 new EmojiEmbed()
171 .setEmoji("PUNISH.BAN.GREEN")
172 .setTitle("Ban")
173 .setDescription("No changes were made")
174 .setStatus("Success")
175 ],
176 components: []
177 });
Skyler Greyad002172022-08-16 18:48:26 +0100178 }
pineafan63fc5e22022-08-04 22:04:10 +0100179};
pineafan4f164f32022-02-26 22:07:12 +0000180
pineafan62ce1922022-08-25 20:34:45 +0100181const check = async (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000182 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000184 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100185 let apply = interaction.options.getUser("user") as User | GuildMember;
186 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
187 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
188 let applyPos = 0
189 try {
PineaFana00db1b2023-01-02 15:32:54 +0000190 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
pineafan62ce1922022-08-25 20:34:45 +0100191 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
192 } catch {
193 apply = apply as User
194 }
pineafanc1c18792022-08-03 21:41:36 +0100195 // Do not allow banning the owner
PineaFana00db1b2023-01-02 15:32:54 +0000196 if (member.id === interaction.guild.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000197 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100198 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000199 // Check if Nucleus has permission to ban
PineaFan100df682023-01-02 13:26:08 +0000200 if (!me.permissions.has("BanMembers")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000201 // Do not allow banning Nucleus
PineaFan100df682023-01-02 13:26:08 +0000202 if (member.id === me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000203 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000204 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000205 // Check if the user has ban_members permission
PineaFan100df682023-01-02 13:26:08 +0000206 if (!member.permissions.has("BanMembers")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000207 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100208 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000209 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100210 return true;
211};
pineafan4f164f32022-02-26 22:07:12 +0000212
Skyler Grey75ea9172022-08-06 10:22:23 +0100213export { command, callback, check };