blob: e8309fb866fbff7f1e67e80dcbd3d8f07aee4d70 [file] [log] [blame]
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05001import Discord, { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, User, ButtonStyle, SlashCommandSubcommandBuilder } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00004import keyValueList from "../../utils/generateKeyValueList.js";
pineafane625d782022-05-09 18:04:32 +01005import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01006import client from "../../utils/client.js";
PineaFan0d06edc2023-01-17 22:10:31 +00007import { LinkWarningFooter } from "../../utils/defaults.js";
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05008import getEmojiByName from "../../utils/getEmojiByName.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;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050029 // TODO:[Modals] Replace the command arguments 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(
PineaFan0d06edc2023-01-17 22:10:31 +000087 `You have been banned from ${interaction.guild.name}` +
PineaFan1dee28f2023-01-16 22:09:07 +000088 (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)
PineaFan9b2ac4d2023-01-18 14:41:07 +0000100 .setURL(config.moderation.ban.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
PineaFan1dee28f2023-01-16 22:09:07 +0000101 )
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",
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500126 timestamp: Date.now()
PineaFan1dee28f2023-01-16 22:09:07 +0000127 },
128 list: {
129 memberId: entry(member.user.id, `\`${member.user.id}\``),
130 name: entry(member.user.id, renderUser(member.user)),
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500131 banned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan1dee28f2023-01-16 22:09:07 +0000132 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
133 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan0d06edc2023-01-17 22:10:31 +0000134 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
PineaFan1dee28f2023-01-16 22:09:07 +0000135 serverMemberCount: interaction.guild.memberCount
136 },
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500137 separate: {
138 end: getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + ` The user was ${notify ? "" : "not "}notified`
139 },
PineaFan1dee28f2023-01-16 22:09:07 +0000140 hidden: {
141 guild: interaction.guild.id
142 }
143 };
144 log(data);
145 } catch {
146 await interaction.editReply({
147 embeds: [
148 new EmojiEmbed()
149 .setEmoji("PUNISH.BAN.RED")
150 .setTitle("Ban")
151 .setDescription("Something went wrong and the user was not banned")
152 .setStatus("Danger")
153 ],
154 components: []
155 });
156 if (dmSent && dmMessage) await dmMessage.delete();
157 return;
158 }
159 const failed = !dmSent && notify;
160 await interaction.editReply({
161 embeds: [
162 new EmojiEmbed()
163 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
164 .setTitle("Ban")
165 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
166 .setStatus(failed ? "Warning" : "Success")
167 ],
168 components: []
169 });
pineafan63fc5e22022-08-04 22:04:10 +0100170};
pineafan4f164f32022-02-26 22:07:12 +0000171
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500172const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000173 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100174 const member = interaction.member as GuildMember;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500175 // Check if the user has ban_members permission
176 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
177 if(partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000178 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100179 let apply = interaction.options.getUser("user") as User | GuildMember;
180 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
181 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
182 let applyPos = 0
183 try {
PineaFana00db1b2023-01-02 15:32:54 +0000184 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
pineafan62ce1922022-08-25 20:34:45 +0100185 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
186 } catch {
187 apply = apply as User
188 }
pineafanc1c18792022-08-03 21:41:36 +0100189 // Do not allow banning the owner
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500190 if (member.id === interaction.guild.ownerId) return "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000191 // Check if Nucleus can ban the member
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500192 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000193 // Check if Nucleus has permission to ban
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500194 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000195 // Do not allow banning Nucleus
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500196 if (member.id === me.id) return "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000197 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000198 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000199 // Check if the user is below on the role list
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500200 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000201 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100202 return true;
203};
pineafan4f164f32022-02-26 22:07:12 +0000204
Skyler Grey75ea9172022-08-06 10:22:23 +0100205export { command, callback, check };
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500206export const metadata = {
207 longDescription: "Removes a member from the server - this will prevent them from rejoining until they are unbanned, and will delete a specified number of days of messages from them.",
208 premiumOnly: true,
209}