blob: 3ed7f1477e05abfd9266b5af555608633cedaf2a [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import Discord, {
2 CommandInteraction,
3 GuildMember,
4 ActionRowBuilder,
5 ButtonBuilder,
Skyler Greyda16adf2023-03-05 10:22:12 +00006 ButtonStyle,
pineafan6de4da52023-03-07 20:43:44 +00007 SlashCommandSubcommandBuilder,
8 ButtonInteraction
Skyler Greyda16adf2023-03-05 10:22:12 +00009} from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +000010import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +010011import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +000012import keyValueList from "../../utils/generateKeyValueList.js";
pineafane625d782022-05-09 18:04:32 +010013import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +010014import client from "../../utils/client.js";
PineaFan0d06edc2023-01-17 22:10:31 +000015import { LinkWarningFooter } from "../../utils/defaults.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -050016import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +000017
18const command = (builder: SlashCommandSubcommandBuilder) =>
19 builder
pineafan63fc5e22022-08-04 22:04:10 +010020 .setName("ban")
21 .setDescription("Bans a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010022 .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true))
Skyler Grey75ea9172022-08-06 10:22:23 +010023 .addNumberOption((option) =>
24 option
25 .setName("delete")
PineaFan100df682023-01-02 13:26:08 +000026 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010027 .setMinValue(0)
28 .setMaxValue(7)
29 .setRequired(false)
30 );
pineafan4f164f32022-02-26 22:07:12 +000031
pineafan6de4da52023-03-07 20:43:44 +000032const callback = async (interaction: CommandInteraction | ButtonInteraction, member?: GuildMember): Promise<void> => {
PineaFana00db1b2023-01-02 15:32:54 +000033 if (!interaction.guild) return;
pineafan6de4da52023-03-07 20:43:44 +000034 let deleteDays;
35 if (!interaction.isButton()) {
36 member = interaction.options.getMember("user") as GuildMember;
pineafan1e462ab2023-03-07 21:34:06 +000037 deleteDays = (interaction.options.get("delete")?.value as number | null) ?? 0;
pineafan6de4da52023-03-07 20:43:44 +000038 } else {
39 deleteDays = 0;
40 }
41 if (!member) return;
pineafan63fc5e22022-08-04 22:04:10 +010042 const { renderUser } = client.logger;
TheCodedProf2e54a772023-02-14 16:26:47 -050043 // TODO:[Modals] Replace the command arguments with a modal
pineafan63fc5e22022-08-04 22:04:10 +010044 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010045 let notify = true;
46 let confirmation;
pineafan62ce1922022-08-25 20:34:45 +010047 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010048 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010049 do {
pineafan73a7c4a2022-07-24 10:38:04 +010050 confirmation = await new confirmationMessage(interaction)
51 .setEmoji("PUNISH.BAN.RED")
52 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010053 .setDescription(
54 keyValueList({
pineafan6de4da52023-03-07 20:43:44 +000055 user: renderUser(member.user),
Skyler Greyda16adf2023-03-05 10:22:12 +000056 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010057 }) +
58 `The user **will${notify ? "" : " not"}** be notified\n` +
pineafan6de4da52023-03-07 20:43:44 +000059 `${addPlurals(deleteDays, "day")} of messages will be deleted\n\n` +
60 `Are you sure you want to ban <@!${member.id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010061 )
pineafan62ce1922022-08-25 20:34:45 +010062 .addCustomBoolean(
63 "notify",
64 "Notify user",
65 false,
66 undefined,
PineaFan100df682023-01-02 13:26:08 +000067 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000068 null,
pineafan62ce1922022-08-25 20:34:45 +010069 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
70 notify
71 )
pineafan73a7c4a2022-07-24 10:38:04 +010072 .setColor("Danger")
73 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000074 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010075 .send(reason !== null);
76 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010077 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010078 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010079 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010080 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
Skyler Greyda16adf2023-03-05 10:22:12 +000081 } while (!timedOut && !chosen);
PineaFan1dee28f2023-01-16 22:09:07 +000082 if (timedOut || !confirmation.success) return;
Skyler Greyda16adf2023-03-05 10:22:12 +000083 reason = reason.length ? reason : null;
PineaFan1dee28f2023-01-16 22:09:07 +000084 let dmSent = false;
85 let dmMessage;
86 const config = await client.database.guilds.read(interaction.guild.id);
87 try {
88 if (notify) {
PineaFana6e37932023-09-25 22:09:08 +010089 let formattedReason: string | null = null;
Skyler Greyda16adf2023-03-05 10:22:12 +000090 if (reason) {
PineaFana6e37932023-09-25 22:09:08 +010091 formattedReason = reason
Skyler Greyda16adf2023-03-05 10:22:12 +000092 .split("\n")
93 .map((line) => "> " + line)
94 .join("\n");
95 }
PineaFan1dee28f2023-01-16 22:09:07 +000096 const messageData: {
97 embeds: EmojiEmbed[];
98 components: ActionRowBuilder<ButtonBuilder>[];
99 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 embeds: [
101 new EmojiEmbed()
102 .setEmoji("PUNISH.BAN.RED")
PineaFan1dee28f2023-01-16 22:09:07 +0000103 .setTitle("Banned")
104 .setDescription(
PineaFan0d06edc2023-01-17 22:10:31 +0000105 `You have been banned from ${interaction.guild.name}` +
PineaFana6e37932023-09-25 22:09:08 +0100106 (formattedReason ? ` for:\n${formattedReason}` : ".\n*No reason was provided.*")
PineaFan1dee28f2023-01-16 22:09:07 +0000107 )
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 .setStatus("Danger")
109 ],
pineafan62ce1922022-08-25 20:34:45 +0100110 components: []
PineaFan1dee28f2023-01-16 22:09:07 +0000111 };
112 if (config.moderation.ban.text && config.moderation.ban.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000113 messageData.embeds[0]!.setFooter(LinkWarningFooter);
114 messageData.components.push(
115 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
116 new ButtonBuilder()
PineaFan1dee28f2023-01-16 22:09:07 +0000117 .setStyle(ButtonStyle.Link)
118 .setLabel(config.moderation.ban.text)
pineafan1e462ab2023-03-07 21:34:06 +0000119 .setURL(config.moderation.ban.link.replaceAll("{id}", member.id))
Skyler Greyda16adf2023-03-05 10:22:12 +0000120 )
121 );
PineaFan1dee28f2023-01-16 22:09:07 +0000122 }
pineafan6de4da52023-03-07 20:43:44 +0000123 dmMessage = await member.send(messageData);
PineaFan1dee28f2023-01-16 22:09:07 +0000124 dmSent = true;
pineafan4f164f32022-02-26 22:07:12 +0000125 }
PineaFan1dee28f2023-01-16 22:09:07 +0000126 } catch {
127 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100128 }
PineaFan1dee28f2023-01-16 22:09:07 +0000129 try {
Skyler Greyf4f21c42023-03-08 14:36:29 +0000130 await member.ban({
pineafan6de4da52023-03-07 20:43:44 +0000131 deleteMessageSeconds: deleteDays * 24 * 60 * 60,
PineaFan1dee28f2023-01-16 22:09:07 +0000132 reason: reason ?? "*No reason provided*"
133 });
134 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
135 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
136 const data = {
137 meta: {
138 type: "memberBan",
139 displayName: "Member Banned",
140 calculateType: "guildMemberPunish",
141 color: NucleusColors.red,
142 emoji: "PUNISH.BAN.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500143 timestamp: Date.now()
PineaFan1dee28f2023-01-16 22:09:07 +0000144 },
145 list: {
146 memberId: entry(member.user.id, `\`${member.user.id}\``),
147 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500148 banned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan1dee28f2023-01-16 22:09:07 +0000149 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
150 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan0d06edc2023-01-17 22:10:31 +0000151 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
PineaFan1dee28f2023-01-16 22:09:07 +0000152 serverMemberCount: interaction.guild.memberCount
153 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500154 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000155 end:
156 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
157 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500158 },
PineaFan1dee28f2023-01-16 22:09:07 +0000159 hidden: {
160 guild: interaction.guild.id
161 }
162 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000163 await log(data);
PineaFan1dee28f2023-01-16 22:09:07 +0000164 } catch {
165 await interaction.editReply({
166 embeds: [
167 new EmojiEmbed()
168 .setEmoji("PUNISH.BAN.RED")
169 .setTitle("Ban")
170 .setDescription("Something went wrong and the user was not banned")
171 .setStatus("Danger")
172 ],
173 components: []
174 });
175 if (dmSent && dmMessage) await dmMessage.delete();
176 return;
177 }
178 const failed = !dmSent && notify;
179 await interaction.editReply({
180 embeds: [
181 new EmojiEmbed()
182 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
183 .setTitle("Ban")
184 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
185 .setStatus(failed ? "Warning" : "Success")
186 ],
187 components: []
188 });
pineafan63fc5e22022-08-04 22:04:10 +0100189};
pineafan4f164f32022-02-26 22:07:12 +0000190
pineafan6de4da52023-03-07 20:43:44 +0000191const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
PineaFana00db1b2023-01-02 15:32:54 +0000192 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100193 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500194 // Check if the user has ban_members permission
TheCodedProfa38cbb32023-03-11 17:22:25 -0500195 if (!member.permissions.has("BanMembers")) {
196 // if(!partial) client.logger.warn("Missing permissions", "Ban", "User does not have Ban Members permission");
197 return "You do not have the *Ban Members* permission";
198 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000199 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000200 const me = interaction.guild.members.me!;
pineafan6de4da52023-03-07 20:43:44 +0000201 let apply: GuildMember;
202 if (interaction.isButton()) {
203 apply = target!;
204 } else {
205 apply = interaction.options.getMember("user") as GuildMember;
pineafan1e462ab2023-03-07 21:34:06 +0000206 }
pineafan62ce1922022-08-25 20:34:45 +0100207 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
208 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
pineafan6de4da52023-03-07 20:43:44 +0000209 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100210 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000211 if (member.id === interaction.guild.ownerId) return "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000212 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500213 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000214 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000215 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000216 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000217 if (member.id === me.id) return "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000218 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000219 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000220 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500221 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000222 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100223 return true;
224};
pineafan4f164f32022-02-26 22:07:12 +0000225
Skyler Grey75ea9172022-08-06 10:22:23 +0100226export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500227export const metadata = {
Skyler Greyda16adf2023-03-05 10:22:12 +0000228 longDescription:
229 "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.",
230 premiumOnly: true
231};