blob: 330edfd57a2351e60187b7f7a290b82084ad408e [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 ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000062 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010063 .send(reason !== null);
64 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010065 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010066 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010067 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010068 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
69 } while (!timedOut && !chosen)
PineaFan1dee28f2023-01-16 22:09:07 +000070 if (timedOut || !confirmation.success) return;
71 reason = reason.length ? reason : null
72 let dmSent = false;
73 let dmMessage;
74 const config = await client.database.guilds.read(interaction.guild.id);
75 try {
76 if (notify) {
77 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
78 const messageData: {
79 embeds: EmojiEmbed[];
80 components: ActionRowBuilder<ButtonBuilder>[];
81 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.BAN.RED")
PineaFan1dee28f2023-01-16 22:09:07 +000085 .setTitle("Banned")
86 .setDescription(
87 `You have been banned in ${interaction.guild.name}` +
88 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
89 )
Skyler Grey75ea9172022-08-06 10:22:23 +010090 .setStatus("Danger")
91 ],
pineafan62ce1922022-08-25 20:34:45 +010092 components: []
PineaFan1dee28f2023-01-16 22:09:07 +000093 };
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()
98 .setStyle(ButtonStyle.Link)
99 .setLabel(config.moderation.ban.text)
100 .setURL(config.moderation.ban.link)
101 )
102 )
103 }
104 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
105 dmSent = true;
pineafan4f164f32022-02-26 22:07:12 +0000106 }
PineaFan1dee28f2023-01-16 22:09:07 +0000107 } catch {
108 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100109 }
PineaFan1dee28f2023-01-16 22:09:07 +0000110 try {
111 const member = interaction.options.getMember("user") as GuildMember;
112 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
113 member.ban({
114 deleteMessageSeconds: days * 24 * 60 * 60,
115 reason: reason ?? "*No reason provided*"
116 });
117 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
118 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)),
131 banned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
132 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
133 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
134 accountCreated: entry(member.user.createdAt.toString(), renderDelta(member.user.createdAt.getTime())),
135 serverMemberCount: interaction.guild.memberCount
136 },
137 hidden: {
138 guild: interaction.guild.id
139 }
140 };
141 log(data);
142 } catch {
143 await interaction.editReply({
144 embeds: [
145 new EmojiEmbed()
146 .setEmoji("PUNISH.BAN.RED")
147 .setTitle("Ban")
148 .setDescription("Something went wrong and the user was not banned")
149 .setStatus("Danger")
150 ],
151 components: []
152 });
153 if (dmSent && dmMessage) await dmMessage.delete();
154 return;
155 }
156 const failed = !dmSent && notify;
157 await interaction.editReply({
158 embeds: [
159 new EmojiEmbed()
160 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
161 .setTitle("Ban")
162 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
163 .setStatus(failed ? "Warning" : "Success")
164 ],
165 components: []
166 });
pineafan63fc5e22022-08-04 22:04:10 +0100167};
pineafan4f164f32022-02-26 22:07:12 +0000168
pineafan62ce1922022-08-25 20:34:45 +0100169const check = async (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000170 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100171 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000172 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100173 let apply = interaction.options.getUser("user") as User | GuildMember;
174 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
175 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
176 let applyPos = 0
177 try {
PineaFana00db1b2023-01-02 15:32:54 +0000178 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
pineafan62ce1922022-08-25 20:34:45 +0100179 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
180 } catch {
181 apply = apply as User
182 }
pineafanc1c18792022-08-03 21:41:36 +0100183 // Do not allow banning the owner
PineaFana00db1b2023-01-02 15:32:54 +0000184 if (member.id === interaction.guild.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000185 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100186 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000187 // Check if Nucleus has permission to ban
PineaFan100df682023-01-02 13:26:08 +0000188 if (!me.permissions.has("BanMembers")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000189 // Do not allow banning Nucleus
PineaFan100df682023-01-02 13:26:08 +0000190 if (member.id === me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000191 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000192 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000193 // Check if the user has ban_members permission
PineaFan100df682023-01-02 13:26:08 +0000194 if (!member.permissions.has("BanMembers")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000195 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100196 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000197 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100198 return true;
199};
pineafan4f164f32022-02-26 22:07:12 +0000200
Skyler Grey75ea9172022-08-06 10:22:23 +0100201export { command, callback, check };