blob: 88f7b395da315ceb2fb2333959f704ebb3526160 [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;
37 deleteDays = (interaction.options.get("delete")?.value as number | null) ?? 0
38 } 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)
Skyler Greyda16adf2023-03-05 10:22:12 +0000118 .setURL(
119 config.moderation.ban.link.replaceAll(
120 "{id}",
pineafan6de4da52023-03-07 20:43:44 +0000121 member.id
Skyler Greyda16adf2023-03-05 10:22:12 +0000122 )
123 )
124 )
125 );
PineaFan1dee28f2023-01-16 22:09:07 +0000126 }
pineafan6de4da52023-03-07 20:43:44 +0000127 dmMessage = await member.send(messageData);
PineaFan1dee28f2023-01-16 22:09:07 +0000128 dmSent = true;
pineafan4f164f32022-02-26 22:07:12 +0000129 }
PineaFan1dee28f2023-01-16 22:09:07 +0000130 } catch {
131 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100132 }
PineaFan1dee28f2023-01-16 22:09:07 +0000133 try {
PineaFan1dee28f2023-01-16 22:09:07 +0000134 member.ban({
pineafan6de4da52023-03-07 20:43:44 +0000135 deleteMessageSeconds: deleteDays * 24 * 60 * 60,
PineaFan1dee28f2023-01-16 22:09:07 +0000136 reason: reason ?? "*No reason provided*"
137 });
138 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
139 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
140 const data = {
141 meta: {
142 type: "memberBan",
143 displayName: "Member Banned",
144 calculateType: "guildMemberPunish",
145 color: NucleusColors.red,
146 emoji: "PUNISH.BAN.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500147 timestamp: Date.now()
PineaFan1dee28f2023-01-16 22:09:07 +0000148 },
149 list: {
150 memberId: entry(member.user.id, `\`${member.user.id}\``),
151 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500152 banned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan1dee28f2023-01-16 22:09:07 +0000153 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
154 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan0d06edc2023-01-17 22:10:31 +0000155 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
PineaFan1dee28f2023-01-16 22:09:07 +0000156 serverMemberCount: interaction.guild.memberCount
157 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500158 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000159 end:
160 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
161 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500162 },
PineaFan1dee28f2023-01-16 22:09:07 +0000163 hidden: {
164 guild: interaction.guild.id
165 }
166 };
167 log(data);
168 } catch {
169 await interaction.editReply({
170 embeds: [
171 new EmojiEmbed()
172 .setEmoji("PUNISH.BAN.RED")
173 .setTitle("Ban")
174 .setDescription("Something went wrong and the user was not banned")
175 .setStatus("Danger")
176 ],
177 components: []
178 });
179 if (dmSent && dmMessage) await dmMessage.delete();
180 return;
181 }
182 const failed = !dmSent && notify;
183 await interaction.editReply({
184 embeds: [
185 new EmojiEmbed()
186 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
187 .setTitle("Ban")
188 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
189 .setStatus(failed ? "Warning" : "Success")
190 ],
191 components: []
192 });
pineafan63fc5e22022-08-04 22:04:10 +0100193};
pineafan4f164f32022-02-26 22:07:12 +0000194
pineafan6de4da52023-03-07 20:43:44 +0000195const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => {
PineaFana00db1b2023-01-02 15:32:54 +0000196 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100197 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500198 // Check if the user has ban_members permission
199 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
Skyler Greyda16adf2023-03-05 10:22:12 +0000200 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000201 const me = interaction.guild.members.me!;
pineafan6de4da52023-03-07 20:43:44 +0000202 let apply: GuildMember;
203 if (interaction.isButton()) {
204 apply = target!;
205 } else {
206 apply = interaction.options.getMember("user") as GuildMember;
207 };
pineafan62ce1922022-08-25 20:34:45 +0100208 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
209 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
pineafan6de4da52023-03-07 20:43:44 +0000210 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100211 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000212 if (member.id === interaction.guild.ownerId) return "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000213 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500214 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000215 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000216 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000217 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000218 if (member.id === me.id) return "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000219 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000220 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000221 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500222 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000223 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100224 return true;
225};
pineafan4f164f32022-02-26 22:07:12 +0000226
Skyler Grey75ea9172022-08-06 10:22:23 +0100227export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500228export const metadata = {
Skyler Greyda16adf2023-03-05 10:22:12 +0000229 longDescription:
230 "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.",
231 premiumOnly: true
232};