blob: e32a72095650a6014420371249f7b5e48db0fe2f [file] [log] [blame]
PineaFan100df682023-01-02 13:26:08 +00001import Discord, { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, User, ButtonStyle } from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01002import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan4f164f32022-02-26 22:07:12 +00003import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00005import keyValueList from "../../utils/generateKeyValueList.js";
pineafane625d782022-05-09 18:04:32 +01006import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
PineaFan100df682023-01-02 13:26:08 +00008import { LinkWarningFooter } from "../../utils/defaultEmbeds.js";
pineafan4f164f32022-02-26 22:07:12 +00009
PineaFan64486c42022-12-28 09:21:04 +000010
pineafan4f164f32022-02-26 22:07:12 +000011const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("ban")
14 .setDescription("Bans a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010015 .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true))
Skyler Grey75ea9172022-08-06 10:22:23 +010016 .addNumberOption((option) =>
17 option
18 .setName("delete")
PineaFan100df682023-01-02 13:26:08 +000019 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010020 .setMinValue(0)
21 .setMaxValue(7)
22 .setRequired(false)
23 );
pineafan4f164f32022-02-26 22:07:12 +000024
PineaFan64486c42022-12-28 09:21:04 +000025
pineafanbd02b4a2022-08-05 22:01:38 +010026const callback = async (interaction: CommandInteraction): Promise<void> => {
PineaFana00db1b2023-01-02 15:32:54 +000027 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010028 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000029 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010030 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010031 let notify = true;
32 let confirmation;
pineafan62ce1922022-08-25 20:34:45 +010033 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010034 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010035 do {
pineafan73a7c4a2022-07-24 10:38:04 +010036 confirmation = await new confirmationMessage(interaction)
37 .setEmoji("PUNISH.BAN.RED")
38 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010039 .setDescription(
40 keyValueList({
PineaFan64486c42022-12-28 09:21:04 +000041 user: renderUser(interaction.options.getUser("user")!),
pineafan62ce1922022-08-25 20:34:45 +010042 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010043 }) +
44 `The user **will${notify ? "" : " not"}** be notified\n` +
45 `${addPlurals(
PineaFan100df682023-01-02 13:26:08 +000046 (interaction.options.get("delete")?.value as number | null) ?? 0, "day")
47 } of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010048 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010049 )
pineafan62ce1922022-08-25 20:34:45 +010050 .addCustomBoolean(
51 "notify",
52 "Notify user",
53 false,
54 undefined,
PineaFan100df682023-01-02 13:26:08 +000055 "The user will be sent a DM",
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 ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010061 .send(reason !== null);
62 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010063 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010064 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010065 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010066 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
67 } while (!timedOut && !chosen)
Skyler Greyad002172022-08-16 18:48:26 +010068 if (timedOut) return;
pineafan62ce1922022-08-25 20:34:45 +010069 if (confirmation.success) {
70 reason = reason.length ? reason : null
PineaFan100df682023-01-02 13:26:08 +000071 let dmSent = false;
72 let dmMessage;
PineaFana00db1b2023-01-02 15:32:54 +000073 const config = await client.database.guilds.read(interaction.guild.id);
pineafan62ce1922022-08-25 20:34:45 +010074 try {
75 if (notify) {
PineaFan100df682023-01-02 13:26:08 +000076 const messageData: {
77 embeds: EmojiEmbed[];
78 components: ActionRowBuilder<ButtonBuilder>[];
79 } = {
pineafan62ce1922022-08-25 20:34:45 +010080 embeds: [
81 new EmojiEmbed()
82 .setEmoji("PUNISH.BAN.RED")
83 .setTitle("Banned")
84 .setDescription(
PineaFana00db1b2023-01-02 15:32:54 +000085 `You have been banned in ${interaction.guild.name}` + (reason ? ` for:\n> ${reason}` : ".")
pineafan62ce1922022-08-25 20:34:45 +010086 )
87 .setStatus("Danger")
88 ],
PineaFan100df682023-01-02 13:26:08 +000089 components: []
90 };
91 if (config.moderation.ban.text && config.moderation.ban.link) {
92 messageData.embeds[0]!.setFooter(LinkWarningFooter)
93 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
94 .addComponents(new ButtonBuilder()
PineaFan64486c42022-12-28 09:21:04 +000095 .setStyle(ButtonStyle.Link)
96 .setLabel(config.moderation.ban.text)
PineaFan100df682023-01-02 13:26:08 +000097 .setURL(config.moderation.ban.link)
98 )
99 )
100 }
101 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
102 dmSent = true;
pineafan62ce1922022-08-25 20:34:45 +0100103 }
104 } catch {
PineaFan100df682023-01-02 13:26:08 +0000105 dmSent = false;
pineafan62ce1922022-08-25 20:34:45 +0100106 }
107 try {
108 const member = interaction.options.getMember("user") as GuildMember;
PineaFan100df682023-01-02 13:26:08 +0000109 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
pineafan62ce1922022-08-25 20:34:45 +0100110 member.ban({
PineaFan100df682023-01-02 13:26:08 +0000111 deleteMessageSeconds: days * 24 * 60 * 60,
112 reason: reason ?? "*No reason provided*"
pineafan62ce1922022-08-25 20:34:45 +0100113 });
PineaFana00db1b2023-01-02 15:32:54 +0000114 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
pineafan62ce1922022-08-25 20:34:45 +0100115 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
116 const data = {
117 meta: {
118 type: "memberBan",
119 displayName: "Member Banned",
120 calculateType: "guildMemberPunish",
121 color: NucleusColors.red,
122 emoji: "PUNISH.BAN.RED",
123 timestamp: new Date().getTime()
124 },
125 list: {
126 memberId: entry(member.user.id, `\`${member.user.id}\``),
127 name: entry(member.user.id, renderUser(member.user)),
PineaFan100df682023-01-02 13:26:08 +0000128 banned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
pineafan62ce1922022-08-25 20:34:45 +0100129 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
130 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan100df682023-01-02 13:26:08 +0000131 accountCreated: entry(member.user.createdAt.toString(), renderDelta(member.user.createdAt.getTime())),
PineaFana00db1b2023-01-02 15:32:54 +0000132 serverMemberCount: interaction.guild.memberCount
pineafan62ce1922022-08-25 20:34:45 +0100133 },
134 hidden: {
PineaFana00db1b2023-01-02 15:32:54 +0000135 guild: interaction.guild.id
pineafan62ce1922022-08-25 20:34:45 +0100136 }
137 };
138 log(data);
139 } catch {
140 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 embeds: [
142 new EmojiEmbed()
143 .setEmoji("PUNISH.BAN.RED")
pineafan62ce1922022-08-25 20:34:45 +0100144 .setTitle("Ban")
145 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 .setStatus("Danger")
147 ],
pineafan62ce1922022-08-25 20:34:45 +0100148 components: []
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 });
PineaFan100df682023-01-02 13:26:08 +0000150 if (dmSent && dmMessage) await dmMessage.delete();
pineafan62ce1922022-08-25 20:34:45 +0100151 return;
pineafan4f164f32022-02-26 22:07:12 +0000152 }
PineaFan100df682023-01-02 13:26:08 +0000153 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100154 await interaction.editReply({
155 embeds: [
156 new EmojiEmbed()
pineafan62ce1922022-08-25 20:34:45 +0100157 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
Skyler Greyad002172022-08-16 18:48:26 +0100158 .setTitle("Ban")
pineafan62ce1922022-08-25 20:34:45 +0100159 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
160 .setStatus(failed ? "Warning" : "Success")
Skyler Greyad002172022-08-16 18:48:26 +0100161 ],
162 components: []
163 });
pineafan62ce1922022-08-25 20:34:45 +0100164 } else {
165 await interaction.editReply({
166 embeds: [
167 new EmojiEmbed()
168 .setEmoji("PUNISH.BAN.GREEN")
169 .setTitle("Ban")
170 .setDescription("No changes were made")
171 .setStatus("Success")
172 ],
173 components: []
174 });
Skyler Greyad002172022-08-16 18:48:26 +0100175 }
pineafan63fc5e22022-08-04 22:04:10 +0100176};
pineafan4f164f32022-02-26 22:07:12 +0000177
pineafan62ce1922022-08-25 20:34:45 +0100178const check = async (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000179 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000181 const me = interaction.guild.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100182 let apply = interaction.options.getUser("user") as User | GuildMember;
183 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
184 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
185 let applyPos = 0
186 try {
PineaFana00db1b2023-01-02 15:32:54 +0000187 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
pineafan62ce1922022-08-25 20:34:45 +0100188 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
189 } catch {
190 apply = apply as User
191 }
pineafanc1c18792022-08-03 21:41:36 +0100192 // Do not allow banning the owner
PineaFana00db1b2023-01-02 15:32:54 +0000193 if (member.id === interaction.guild.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000194 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100195 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000196 // Check if Nucleus has permission to ban
PineaFan100df682023-01-02 13:26:08 +0000197 if (!me.permissions.has("BanMembers")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000198 // Do not allow banning Nucleus
PineaFan100df682023-01-02 13:26:08 +0000199 if (member.id === me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000200 // Allow the owner to ban anyone
PineaFana00db1b2023-01-02 15:32:54 +0000201 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000202 // Check if the user has ban_members permission
PineaFan100df682023-01-02 13:26:08 +0000203 if (!member.permissions.has("BanMembers")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000204 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100205 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000206 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100207 return true;
208};
pineafan4f164f32022-02-26 22:07:12 +0000209
Skyler Grey75ea9172022-08-06 10:22:23 +0100210export { command, callback, check };