blob: 628b607a651b53495d3bc204b5902cc19bca6c04 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import Discord, {
2 CommandInteraction,
3 GuildMember,
4 ActionRowBuilder,
5 ButtonBuilder,
6 User,
7 ButtonStyle,
8 SlashCommandSubcommandBuilder
9} 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
pineafanbd02b4a2022-08-05 22:01:38 +010032const callback = async (interaction: CommandInteraction): Promise<void> => {
PineaFana00db1b2023-01-02 15:32:54 +000033 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010034 const { renderUser } = client.logger;
TheCodedProf2e54a772023-02-14 16:26:47 -050035 // TODO:[Modals] Replace the command arguments with a modal
pineafan63fc5e22022-08-04 22:04:10 +010036 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010037 let notify = true;
38 let confirmation;
pineafan62ce1922022-08-25 20:34:45 +010039 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010040 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010041 do {
pineafan73a7c4a2022-07-24 10:38:04 +010042 confirmation = await new confirmationMessage(interaction)
43 .setEmoji("PUNISH.BAN.RED")
44 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010045 .setDescription(
46 keyValueList({
PineaFan64486c42022-12-28 09:21:04 +000047 user: renderUser(interaction.options.getUser("user")!),
Skyler Greyda16adf2023-03-05 10:22:12 +000048 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010049 }) +
50 `The user **will${notify ? "" : " not"}** be notified\n` +
51 `${addPlurals(
Skyler Greyda16adf2023-03-05 10:22:12 +000052 (interaction.options.get("delete")?.value as number | null) ?? 0,
53 "day"
54 )} of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010055 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010056 )
pineafan62ce1922022-08-25 20:34:45 +010057 .addCustomBoolean(
58 "notify",
59 "Notify user",
60 false,
61 undefined,
PineaFan100df682023-01-02 13:26:08 +000062 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000063 null,
pineafan62ce1922022-08-25 20:34:45 +010064 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
65 notify
66 )
pineafan73a7c4a2022-07-24 10:38:04 +010067 .setColor("Danger")
68 .addReasonButton(reason ?? "")
PineaFan1dee28f2023-01-16 22:09:07 +000069 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010070 .send(reason !== null);
71 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010072 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010073 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010074 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010075 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
Skyler Greyda16adf2023-03-05 10:22:12 +000076 } while (!timedOut && !chosen);
PineaFan1dee28f2023-01-16 22:09:07 +000077 if (timedOut || !confirmation.success) return;
Skyler Greyda16adf2023-03-05 10:22:12 +000078 reason = reason.length ? reason : null;
PineaFan1dee28f2023-01-16 22:09:07 +000079 let dmSent = false;
80 let dmMessage;
81 const config = await client.database.guilds.read(interaction.guild.id);
82 try {
83 if (notify) {
Skyler Greyda16adf2023-03-05 10:22:12 +000084 if (reason) {
85 reason = reason
86 .split("\n")
87 .map((line) => "> " + line)
88 .join("\n");
89 }
PineaFan1dee28f2023-01-16 22:09:07 +000090 const messageData: {
91 embeds: EmojiEmbed[];
92 components: ActionRowBuilder<ButtonBuilder>[];
93 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 embeds: [
95 new EmojiEmbed()
96 .setEmoji("PUNISH.BAN.RED")
PineaFan1dee28f2023-01-16 22:09:07 +000097 .setTitle("Banned")
98 .setDescription(
PineaFan0d06edc2023-01-17 22:10:31 +000099 `You have been banned from ${interaction.guild.name}` +
PineaFan1dee28f2023-01-16 22:09:07 +0000100 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
101 )
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 .setStatus("Danger")
103 ],
pineafan62ce1922022-08-25 20:34:45 +0100104 components: []
PineaFan1dee28f2023-01-16 22:09:07 +0000105 };
106 if (config.moderation.ban.text && config.moderation.ban.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000107 messageData.embeds[0]!.setFooter(LinkWarningFooter);
108 messageData.components.push(
109 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
110 new ButtonBuilder()
PineaFan1dee28f2023-01-16 22:09:07 +0000111 .setStyle(ButtonStyle.Link)
112 .setLabel(config.moderation.ban.text)
Skyler Greyda16adf2023-03-05 10:22:12 +0000113 .setURL(
114 config.moderation.ban.link.replaceAll(
115 "{id}",
116 (interaction.options.getMember("user") as GuildMember).id
117 )
118 )
119 )
120 );
PineaFan1dee28f2023-01-16 22:09:07 +0000121 }
122 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
123 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 {
129 const member = interaction.options.getMember("user") as GuildMember;
Skyler Greyda16adf2023-03-05 10:22:12 +0000130 const days: number = (interaction.options.get("delete")?.value as number | null) ?? 0;
PineaFan1dee28f2023-01-16 22:09:07 +0000131 member.ban({
132 deleteMessageSeconds: days * 24 * 60 * 60,
133 reason: reason ?? "*No reason provided*"
134 });
135 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
136 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
137 const data = {
138 meta: {
139 type: "memberBan",
140 displayName: "Member Banned",
141 calculateType: "guildMemberPunish",
142 color: NucleusColors.red,
143 emoji: "PUNISH.BAN.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500144 timestamp: Date.now()
PineaFan1dee28f2023-01-16 22:09:07 +0000145 },
146 list: {
147 memberId: entry(member.user.id, `\`${member.user.id}\``),
148 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500149 banned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan1dee28f2023-01-16 22:09:07 +0000150 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
151 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan0d06edc2023-01-17 22:10:31 +0000152 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
PineaFan1dee28f2023-01-16 22:09:07 +0000153 serverMemberCount: interaction.guild.memberCount
154 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500155 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000156 end:
157 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
158 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500159 },
PineaFan1dee28f2023-01-16 22:09:07 +0000160 hidden: {
161 guild: interaction.guild.id
162 }
163 };
164 log(data);
165 } catch {
166 await interaction.editReply({
167 embeds: [
168 new EmojiEmbed()
169 .setEmoji("PUNISH.BAN.RED")
170 .setTitle("Ban")
171 .setDescription("Something went wrong and the user was not banned")
172 .setStatus("Danger")
173 ],
174 components: []
175 });
176 if (dmSent && dmMessage) await dmMessage.delete();
177 return;
178 }
179 const failed = !dmSent && notify;
180 await interaction.editReply({
181 embeds: [
182 new EmojiEmbed()
183 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
184 .setTitle("Ban")
185 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
186 .setStatus(failed ? "Warning" : "Success")
187 ],
188 components: []
189 });
pineafan63fc5e22022-08-04 22:04:10 +0100190};
pineafan4f164f32022-02-26 22:07:12 +0000191
TheCodedProff86ba092023-01-27 17:10:07 -0500192const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000193 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100194 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500195 // Check if the user has ban_members permission
196 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
Skyler Greyda16adf2023-03-05 10:22:12 +0000197 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000198 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100199 let apply = interaction.options.getUser("user") as User | GuildMember;
200 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
201 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
Skyler Greyda16adf2023-03-05 10:22:12 +0000202 let applyPos = 0;
pineafan62ce1922022-08-25 20:34:45 +0100203 try {
Skyler Greyda16adf2023-03-05 10:22:12 +0000204 apply = (await interaction.guild.members.fetch(apply.id)) as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100205 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
206 } catch {
Skyler Greyda16adf2023-03-05 10:22:12 +0000207 apply = apply as User;
pineafan62ce1922022-08-25 20:34:45 +0100208 }
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};