blob: 847e66cefc676eb71e139e786759b54f0ec12240 [file] [log] [blame]
TheCodedProff86ba092023-01-27 17:10:07 -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";
pineafan4f164f32022-02-26 22:07:12 +00008
PineaFan64486c42022-12-28 09:21:04 +00009
pineafan4f164f32022-02-26 22:07:12 +000010const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("ban")
13 .setDescription("Bans a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010014 .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true))
Skyler Grey75ea9172022-08-06 10:22:23 +010015 .addNumberOption((option) =>
16 option
17 .setName("delete")
PineaFan100df682023-01-02 13:26:08 +000018 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010019 .setMinValue(0)
20 .setMaxValue(7)
21 .setRequired(false)
22 );
pineafan4f164f32022-02-26 22:07:12 +000023
PineaFan64486c42022-12-28 09:21:04 +000024
pineafanbd02b4a2022-08-05 22:01:38 +010025const callback = async (interaction: CommandInteraction): Promise<void> => {
PineaFana00db1b2023-01-02 15:32:54 +000026 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010027 const { renderUser } = client.logger;
TheCodedProf2e54a772023-02-14 16:26:47 -050028 // TODO:[Modals] Replace the command arguments with a modal
pineafan63fc5e22022-08-04 22:04:10 +010029 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010030 let notify = true;
31 let confirmation;
pineafan62ce1922022-08-25 20:34:45 +010032 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010033 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010034 do {
pineafan73a7c4a2022-07-24 10:38:04 +010035 confirmation = await new confirmationMessage(interaction)
36 .setEmoji("PUNISH.BAN.RED")
37 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010038 .setDescription(
39 keyValueList({
PineaFan64486c42022-12-28 09:21:04 +000040 user: renderUser(interaction.options.getUser("user")!),
pineafan62ce1922022-08-25 20:34:45 +010041 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010042 }) +
43 `The user **will${notify ? "" : " not"}** be notified\n` +
44 `${addPlurals(
PineaFan100df682023-01-02 13:26:08 +000045 (interaction.options.get("delete")?.value as number | null) ?? 0, "day")
46 } of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010047 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010048 )
pineafan62ce1922022-08-25 20:34:45 +010049 .addCustomBoolean(
50 "notify",
51 "Notify user",
52 false,
53 undefined,
PineaFan100df682023-01-02 13:26:08 +000054 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000055 null,
pineafan62ce1922022-08-25 20:34:45 +010056 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
57 notify
58 )
pineafan73a7c4a2022-07-24 10:38:04 +010059 .setColor("Danger")
60 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000061 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010062 .send(reason !== null);
63 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010064 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010065 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010066 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010067 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
68 } while (!timedOut && !chosen)
PineaFan1dee28f2023-01-16 22:09:07 +000069 if (timedOut || !confirmation.success) return;
70 reason = reason.length ? reason : null
71 let dmSent = false;
72 let dmMessage;
73 const config = await client.database.guilds.read(interaction.guild.id);
74 try {
75 if (notify) {
76 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
77 const messageData: {
78 embeds: EmojiEmbed[];
79 components: ActionRowBuilder<ButtonBuilder>[];
80 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 embeds: [
82 new EmojiEmbed()
83 .setEmoji("PUNISH.BAN.RED")
PineaFan1dee28f2023-01-16 22:09:07 +000084 .setTitle("Banned")
85 .setDescription(
PineaFan0d06edc2023-01-17 22:10:31 +000086 `You have been banned from ${interaction.guild.name}` +
PineaFan1dee28f2023-01-16 22:09:07 +000087 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
88 )
Skyler Grey75ea9172022-08-06 10:22:23 +010089 .setStatus("Danger")
90 ],
pineafan62ce1922022-08-25 20:34:45 +010091 components: []
PineaFan1dee28f2023-01-16 22:09:07 +000092 };
93 if (config.moderation.ban.text && config.moderation.ban.link) {
94 messageData.embeds[0]!.setFooter(LinkWarningFooter)
95 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
96 .addComponents(new ButtonBuilder()
97 .setStyle(ButtonStyle.Link)
98 .setLabel(config.moderation.ban.text)
PineaFan9b2ac4d2023-01-18 14:41:07 +000099 .setURL(config.moderation.ban.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
PineaFan1dee28f2023-01-16 22:09:07 +0000100 )
101 )
102 }
103 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
104 dmSent = true;
pineafan4f164f32022-02-26 22:07:12 +0000105 }
PineaFan1dee28f2023-01-16 22:09:07 +0000106 } catch {
107 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100108 }
PineaFan1dee28f2023-01-16 22:09:07 +0000109 try {
110 const member = interaction.options.getMember("user") as GuildMember;
111 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
112 member.ban({
113 deleteMessageSeconds: days * 24 * 60 * 60,
114 reason: reason ?? "*No reason provided*"
115 });
116 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
117 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
118 const data = {
119 meta: {
120 type: "memberBan",
121 displayName: "Member Banned",
122 calculateType: "guildMemberPunish",
123 color: NucleusColors.red,
124 emoji: "PUNISH.BAN.RED",
125 timestamp: new Date().getTime()
126 },
127 list: {
128 memberId: entry(member.user.id, `\`${member.user.id}\``),
129 name: entry(member.user.id, renderUser(member.user)),
130 banned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
131 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
132 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan0d06edc2023-01-17 22:10:31 +0000133 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
PineaFan1dee28f2023-01-16 22:09:07 +0000134 serverMemberCount: interaction.guild.memberCount
135 },
136 hidden: {
137 guild: interaction.guild.id
138 }
139 };
140 log(data);
141 } catch {
142 await interaction.editReply({
143 embeds: [
144 new EmojiEmbed()
145 .setEmoji("PUNISH.BAN.RED")
146 .setTitle("Ban")
147 .setDescription("Something went wrong and the user was not banned")
148 .setStatus("Danger")
149 ],
150 components: []
151 });
152 if (dmSent && dmMessage) await dmMessage.delete();
153 return;
154 }
155 const failed = !dmSent && notify;
156 await interaction.editReply({
157 embeds: [
158 new EmojiEmbed()
159 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
160 .setTitle("Ban")
161 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
162 .setStatus(failed ? "Warning" : "Success")
163 ],
164 components: []
165 });
pineafan63fc5e22022-08-04 22:04:10 +0100166};
pineafan4f164f32022-02-26 22:07:12 +0000167
TheCodedProff86ba092023-01-27 17:10:07 -0500168const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000169 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100170 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500171 // Check if the user has ban_members permission
172 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
173 if(partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000174 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100175 let apply = interaction.options.getUser("user") as User | GuildMember;
176 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
177 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
178 let applyPos = 0
179 try {
PineaFana00db1b2023-01-02 15:32:54 +0000180 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
pineafan62ce1922022-08-25 20:34:45 +0100181 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
182 } catch {
183 apply = apply as User
184 }
pineafanc1c18792022-08-03 21:41:36 +0100185 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000186 if (member.id === interaction.guild.ownerId) return "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000187 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500188 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan4f164f32022-02-26 22:07:12 +0000189 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000190 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000191 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000192 if (member.id === me.id) return "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000193 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000194 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000195 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500196 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
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 };
TheCodedProfa112f612023-01-28 18:06:45 -0500202export const metadata = {
203 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.",
204 premiumOnly: true,
205}