blob: 78f936cd58d6b089a640d0dd324de628cc6dd8fd [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) {
Skyler Greyda16adf2023-03-05 10:22:12 +000089 if (reason) {
90 reason = reason
91 .split("\n")
92 .map((line) => "> " + line)
93 .join("\n");
94 }
PineaFan1dee28f2023-01-16 22:09:07 +000095 const messageData: {
96 embeds: EmojiEmbed[];
97 components: ActionRowBuilder<ButtonBuilder>[];
98 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 embeds: [
100 new EmojiEmbed()
101 .setEmoji("PUNISH.BAN.RED")
PineaFan1dee28f2023-01-16 22:09:07 +0000102 .setTitle("Banned")
103 .setDescription(
PineaFan0d06edc2023-01-17 22:10:31 +0000104 `You have been banned from ${interaction.guild.name}` +
PineaFan1dee28f2023-01-16 22:09:07 +0000105 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
106 )
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 .setStatus("Danger")
108 ],
pineafan62ce1922022-08-25 20:34:45 +0100109 components: []
PineaFan1dee28f2023-01-16 22:09:07 +0000110 };
111 if (config.moderation.ban.text && config.moderation.ban.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000112 messageData.embeds[0]!.setFooter(LinkWarningFooter);
113 messageData.components.push(
114 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
115 new ButtonBuilder()
PineaFan1dee28f2023-01-16 22:09:07 +0000116 .setStyle(ButtonStyle.Link)
117 .setLabel(config.moderation.ban.text)
pineafan1e462ab2023-03-07 21:34:06 +0000118 .setURL(config.moderation.ban.link.replaceAll("{id}", member.id))
Skyler Greyda16adf2023-03-05 10:22:12 +0000119 )
120 );
PineaFan1dee28f2023-01-16 22:09:07 +0000121 }
pineafan6de4da52023-03-07 20:43:44 +0000122 dmMessage = await member.send(messageData);
PineaFan1dee28f2023-01-16 22:09:07 +0000123 dmSent = true;
pineafan4f164f32022-02-26 22:07:12 +0000124 }
PineaFan1dee28f2023-01-16 22:09:07 +0000125 } catch {
126 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100127 }
PineaFan1dee28f2023-01-16 22:09:07 +0000128 try {
Skyler Greyf4f21c42023-03-08 14:36:29 +0000129 await member.ban({
pineafan6de4da52023-03-07 20:43:44 +0000130 deleteMessageSeconds: deleteDays * 24 * 60 * 60,
PineaFan1dee28f2023-01-16 22:09:07 +0000131 reason: reason ?? "*No reason provided*"
132 });
133 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
134 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
135 const data = {
136 meta: {
137 type: "memberBan",
138 displayName: "Member Banned",
139 calculateType: "guildMemberPunish",
140 color: NucleusColors.red,
141 emoji: "PUNISH.BAN.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500142 timestamp: Date.now()
PineaFan1dee28f2023-01-16 22:09:07 +0000143 },
144 list: {
145 memberId: entry(member.user.id, `\`${member.user.id}\``),
146 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500147 banned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan1dee28f2023-01-16 22:09:07 +0000148 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
149 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan0d06edc2023-01-17 22:10:31 +0000150 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
PineaFan1dee28f2023-01-16 22:09:07 +0000151 serverMemberCount: interaction.guild.memberCount
152 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500153 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000154 end:
155 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
156 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500157 },
PineaFan1dee28f2023-01-16 22:09:07 +0000158 hidden: {
159 guild: interaction.guild.id
160 }
161 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000162 await log(data);
PineaFan1dee28f2023-01-16 22:09:07 +0000163 } catch {
164 await interaction.editReply({
165 embeds: [
166 new EmojiEmbed()
167 .setEmoji("PUNISH.BAN.RED")
168 .setTitle("Ban")
169 .setDescription("Something went wrong and the user was not banned")
170 .setStatus("Danger")
171 ],
172 components: []
173 });
174 if (dmSent && dmMessage) await dmMessage.delete();
175 return;
176 }
177 const failed = !dmSent && notify;
178 await interaction.editReply({
179 embeds: [
180 new EmojiEmbed()
181 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
182 .setTitle("Ban")
183 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
184 .setStatus(failed ? "Warning" : "Success")
185 ],
186 components: []
187 });
pineafan63fc5e22022-08-04 22:04:10 +0100188};
pineafan4f164f32022-02-26 22:07:12 +0000189
pineafan6de4da52023-03-07 20:43:44 +0000190const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
PineaFana00db1b2023-01-02 15:32:54 +0000191 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500193 // Check if the user has ban_members permission
TheCodedProfa38cbb32023-03-11 17:22:25 -0500194 if (!member.permissions.has("BanMembers")) {
195 // if(!partial) client.logger.warn("Missing permissions", "Ban", "User does not have Ban Members permission");
196 return "You do not have the *Ban Members* permission";
197 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000198 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000199 const me = interaction.guild.members.me!;
pineafan6de4da52023-03-07 20:43:44 +0000200 let apply: GuildMember;
201 if (interaction.isButton()) {
202 apply = target!;
203 } else {
204 apply = interaction.options.getMember("user") as GuildMember;
pineafan1e462ab2023-03-07 21:34:06 +0000205 }
pineafan62ce1922022-08-25 20:34:45 +0100206 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
207 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
pineafan6de4da52023-03-07 20:43:44 +0000208 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100209 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000210 if (member.id === interaction.guild.ownerId) return "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000211 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500212 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000213 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000214 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000215 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000216 if (member.id === me.id) return "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000217 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000218 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000219 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500220 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000221 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100222 return true;
223};
pineafan4f164f32022-02-26 22:07:12 +0000224
Skyler Grey75ea9172022-08-06 10:22:23 +0100225export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500226export const metadata = {
Skyler Greyda16adf2023-03-05 10:22:12 +0000227 longDescription:
228 "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.",
229 premiumOnly: true
230};