blob: 6deb4adc68c309d5dbefe2137f9614aefb0ed369 [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> => {
pineafan63fc5e22022-08-04 22:04:10 +010027 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000028 // TODO:[Modals] Replace this 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",
pineafan62ce1922022-08-25 20:34:45 +010055 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
56 notify
57 )
pineafan73a7c4a2022-07-24 10:38:04 +010058 .setColor("Danger")
59 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010060 .send(reason !== null);
61 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010062 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010063 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010064 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010065 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
66 } while (!timedOut && !chosen)
Skyler Greyad002172022-08-16 18:48:26 +010067 if (timedOut) return;
pineafan62ce1922022-08-25 20:34:45 +010068 if (confirmation.success) {
69 reason = reason.length ? reason : null
PineaFan100df682023-01-02 13:26:08 +000070 let dmSent = false;
71 let dmMessage;
pineafan62ce1922022-08-25 20:34:45 +010072 const config = await client.database.guilds.read(interaction.guild!.id);
73 try {
74 if (notify) {
PineaFan100df682023-01-02 13:26:08 +000075 const messageData: {
76 embeds: EmojiEmbed[];
77 components: ActionRowBuilder<ButtonBuilder>[];
78 } = {
pineafan62ce1922022-08-25 20:34:45 +010079 embeds: [
80 new EmojiEmbed()
81 .setEmoji("PUNISH.BAN.RED")
82 .setTitle("Banned")
83 .setDescription(
84 `You have been banned in ${interaction.guild!.name}` + (reason ? ` for:\n> ${reason}` : ".")
85 )
86 .setStatus("Danger")
87 ],
PineaFan100df682023-01-02 13:26:08 +000088 components: []
89 };
90 if (config.moderation.ban.text && config.moderation.ban.link) {
91 messageData.embeds[0]!.setFooter(LinkWarningFooter)
92 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
93 .addComponents(new ButtonBuilder()
PineaFan64486c42022-12-28 09:21:04 +000094 .setStyle(ButtonStyle.Link)
95 .setLabel(config.moderation.ban.text)
PineaFan100df682023-01-02 13:26:08 +000096 .setURL(config.moderation.ban.link)
97 )
98 )
99 }
100 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
101 dmSent = true;
pineafan62ce1922022-08-25 20:34:45 +0100102 }
103 } catch {
PineaFan100df682023-01-02 13:26:08 +0000104 dmSent = false;
pineafan62ce1922022-08-25 20:34:45 +0100105 }
106 try {
107 const member = interaction.options.getMember("user") as GuildMember;
PineaFan100df682023-01-02 13:26:08 +0000108 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
pineafan62ce1922022-08-25 20:34:45 +0100109 member.ban({
PineaFan100df682023-01-02 13:26:08 +0000110 deleteMessageSeconds: days * 24 * 60 * 60,
111 reason: reason ?? "*No reason provided*"
pineafan62ce1922022-08-25 20:34:45 +0100112 });
113 await client.database.history.create("ban", interaction.guild!.id, member.user, interaction.user, reason);
114 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
115 const data = {
116 meta: {
117 type: "memberBan",
118 displayName: "Member Banned",
119 calculateType: "guildMemberPunish",
120 color: NucleusColors.red,
121 emoji: "PUNISH.BAN.RED",
122 timestamp: new Date().getTime()
123 },
124 list: {
125 memberId: entry(member.user.id, `\`${member.user.id}\``),
126 name: entry(member.user.id, renderUser(member.user)),
PineaFan100df682023-01-02 13:26:08 +0000127 banned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
pineafan62ce1922022-08-25 20:34:45 +0100128 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
129 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
PineaFan100df682023-01-02 13:26:08 +0000130 accountCreated: entry(member.user.createdAt.toString(), renderDelta(member.user.createdAt.getTime())),
pineafan62ce1922022-08-25 20:34:45 +0100131 serverMemberCount: interaction.guild!.memberCount
132 },
133 hidden: {
134 guild: interaction.guild!.id
135 }
136 };
137 log(data);
138 } catch {
139 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 embeds: [
141 new EmojiEmbed()
142 .setEmoji("PUNISH.BAN.RED")
pineafan62ce1922022-08-25 20:34:45 +0100143 .setTitle("Ban")
144 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 .setStatus("Danger")
146 ],
pineafan62ce1922022-08-25 20:34:45 +0100147 components: []
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 });
PineaFan100df682023-01-02 13:26:08 +0000149 if (dmSent && dmMessage) await dmMessage.delete();
pineafan62ce1922022-08-25 20:34:45 +0100150 return;
pineafan4f164f32022-02-26 22:07:12 +0000151 }
PineaFan100df682023-01-02 13:26:08 +0000152 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100153 await interaction.editReply({
154 embeds: [
155 new EmojiEmbed()
pineafan62ce1922022-08-25 20:34:45 +0100156 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
Skyler Greyad002172022-08-16 18:48:26 +0100157 .setTitle("Ban")
pineafan62ce1922022-08-25 20:34:45 +0100158 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
159 .setStatus(failed ? "Warning" : "Success")
Skyler Greyad002172022-08-16 18:48:26 +0100160 ],
161 components: []
162 });
pineafan62ce1922022-08-25 20:34:45 +0100163 } else {
164 await interaction.editReply({
165 embeds: [
166 new EmojiEmbed()
167 .setEmoji("PUNISH.BAN.GREEN")
168 .setTitle("Ban")
169 .setDescription("No changes were made")
170 .setStatus("Success")
171 ],
172 components: []
173 });
Skyler Greyad002172022-08-16 18:48:26 +0100174 }
pineafan63fc5e22022-08-04 22:04:10 +0100175};
pineafan4f164f32022-02-26 22:07:12 +0000176
pineafan62ce1922022-08-25 20:34:45 +0100177const check = async (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 const member = interaction.member as GuildMember;
PineaFan100df682023-01-02 13:26:08 +0000179 const me = interaction.guild!.members.me!;
pineafan62ce1922022-08-25 20:34:45 +0100180 let apply = interaction.options.getUser("user") as User | GuildMember;
181 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
182 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
183 let applyPos = 0
184 try {
185 apply = await interaction.guild!.members.fetch(apply.id) as GuildMember
186 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
187 } catch {
188 apply = apply as User
189 }
pineafanc1c18792022-08-03 21:41:36 +0100190 // Do not allow banning the owner
pineafan62ce1922022-08-25 20:34:45 +0100191 if (member.id === interaction.guild!.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000192 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100193 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000194 // Check if Nucleus has permission to ban
PineaFan100df682023-01-02 13:26:08 +0000195 if (!me.permissions.has("BanMembers")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000196 // Do not allow banning Nucleus
PineaFan100df682023-01-02 13:26:08 +0000197 if (member.id === me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000198 // Allow the owner to ban anyone
pineafan62ce1922022-08-25 20:34:45 +0100199 if (member.id === interaction.guild!.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000200 // Check if the user has ban_members permission
PineaFan100df682023-01-02 13:26:08 +0000201 if (!member.permissions.has("BanMembers")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000202 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100203 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000204 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100205 return true;
206};
pineafan4f164f32022-02-26 22:07:12 +0000207
Skyler Grey75ea9172022-08-06 10:22:23 +0100208export { command, callback, check };