blob: 9b24b0c7a10add5dc6be58d83a455d33afab9863 [file] [log] [blame]
TheCodedProf21c08592022-09-13 14:14:43 -04001import { 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";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("ban")
12 .setDescription("Bans a user from the server")
Skyler Grey11236ba2022-08-08 21:13:33 +010013 .addUserOption((option) => option.setName("user").setDescription("The user to ban").setRequired(true))
Skyler Grey75ea9172022-08-06 10:22:23 +010014 .addNumberOption((option) =>
15 option
16 .setName("delete")
17 .setDescription("The days of messages to delete | Default: 0")
18 .setMinValue(0)
19 .setMaxValue(7)
20 .setRequired(false)
21 );
pineafan4f164f32022-02-26 22:07:12 +000022
pineafanbd02b4a2022-08-05 22:01:38 +010023const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan63fc5e22022-08-04 22:04:10 +010024 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000025 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010026 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010027 let notify = true;
28 let confirmation;
pineafan62ce1922022-08-25 20:34:45 +010029 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010030 let timedOut = false;
pineafan62ce1922022-08-25 20:34:45 +010031 do {
pineafan73a7c4a2022-07-24 10:38:04 +010032 confirmation = await new confirmationMessage(interaction)
33 .setEmoji("PUNISH.BAN.RED")
34 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010035 .setDescription(
36 keyValueList({
37 user: renderUser(interaction.options.getUser("user")),
pineafan62ce1922022-08-25 20:34:45 +010038 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010039 }) +
40 `The user **will${notify ? "" : " not"}** be notified\n` +
41 `${addPlurals(
pineafan62ce1922022-08-25 20:34:45 +010042 interaction.options.getNumber("delete") ?? 0,
Skyler Grey75ea9172022-08-06 10:22:23 +010043 "day"
44 )} of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010045 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010046 )
pineafan62ce1922022-08-25 20:34:45 +010047 .addCustomBoolean(
48 "notify",
49 "Notify user",
50 false,
51 undefined,
52 null,
53 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
54 notify
55 )
pineafan73a7c4a2022-07-24 10:38:04 +010056 .setColor("Danger")
57 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010058 .send(reason !== null);
59 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010060 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010061 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010062 else if (confirmation.newReason) reason = confirmation.newReason;
pineafan62ce1922022-08-25 20:34:45 +010063 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
64 } while (!timedOut && !chosen)
Skyler Greyad002172022-08-16 18:48:26 +010065 if (timedOut) return;
pineafan62ce1922022-08-25 20:34:45 +010066 if (confirmation.success) {
67 reason = reason.length ? reason : null
68 let dmd = false;
69 let dm;
70 const config = await client.database.guilds.read(interaction.guild!.id);
71 try {
72 if (notify) {
73 dm = await (interaction.options.getMember("user") as GuildMember).send({
74 embeds: [
75 new EmojiEmbed()
76 .setEmoji("PUNISH.BAN.RED")
77 .setTitle("Banned")
78 .setDescription(
79 `You have been banned in ${interaction.guild!.name}` + (reason ? ` for:\n> ${reason}` : ".")
80 )
81 .setStatus("Danger")
82 ],
83 components: [
TheCodedProf21c08592022-09-13 14:14:43 -040084 new ActionRowBuilder().addComponents(
pineafan62ce1922022-08-25 20:34:45 +010085 config.moderation.ban.text
86 ? [
TheCodedProf21c08592022-09-13 14:14:43 -040087 new ButtonBuilder()
88 .setStyle(ButtonStyle.Link)
pineafan62ce1922022-08-25 20:34:45 +010089 .setLabel(config.moderation.ban.text)
90 .setURL(config.moderation.ban.link)
91 ]
92 : []
93 )
94 ]
95 });
96 dmd = true;
97 }
98 } catch {
99 dmd = false;
100 }
101 try {
102 const member = interaction.options.getMember("user") as GuildMember;
103 member.ban({
104 days: Number(interaction.options.getNumber("delete") ?? 0),
105 reason: reason ?? "No reason provided"
106 });
107 await client.database.history.create("ban", interaction.guild!.id, member.user, interaction.user, reason);
108 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
109 const data = {
110 meta: {
111 type: "memberBan",
112 displayName: "Member Banned",
113 calculateType: "guildMemberPunish",
114 color: NucleusColors.red,
115 emoji: "PUNISH.BAN.RED",
116 timestamp: new Date().getTime()
117 },
118 list: {
119 memberId: entry(member.user.id, `\`${member.user.id}\``),
120 name: entry(member.user.id, renderUser(member.user)),
121 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
122 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
123 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
124 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
125 serverMemberCount: interaction.guild!.memberCount
126 },
127 hidden: {
128 guild: interaction.guild!.id
129 }
130 };
131 log(data);
132 } catch {
133 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 embeds: [
135 new EmojiEmbed()
136 .setEmoji("PUNISH.BAN.RED")
pineafan62ce1922022-08-25 20:34:45 +0100137 .setTitle("Ban")
138 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 .setStatus("Danger")
140 ],
pineafan62ce1922022-08-25 20:34:45 +0100141 components: []
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 });
pineafan62ce1922022-08-25 20:34:45 +0100143 if (dmd && dm) await dm.delete();
144 return;
pineafan4f164f32022-02-26 22:07:12 +0000145 }
pineafan62ce1922022-08-25 20:34:45 +0100146 const failed = !dmd && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100147 await interaction.editReply({
148 embeds: [
149 new EmojiEmbed()
pineafan62ce1922022-08-25 20:34:45 +0100150 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
Skyler Greyad002172022-08-16 18:48:26 +0100151 .setTitle("Ban")
pineafan62ce1922022-08-25 20:34:45 +0100152 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
153 .setStatus(failed ? "Warning" : "Success")
Skyler Greyad002172022-08-16 18:48:26 +0100154 ],
155 components: []
156 });
pineafan62ce1922022-08-25 20:34:45 +0100157 } else {
158 await interaction.editReply({
159 embeds: [
160 new EmojiEmbed()
161 .setEmoji("PUNISH.BAN.GREEN")
162 .setTitle("Ban")
163 .setDescription("No changes were made")
164 .setStatus("Success")
165 ],
166 components: []
167 });
Skyler Greyad002172022-08-16 18:48:26 +0100168 }
pineafan63fc5e22022-08-04 22:04:10 +0100169};
pineafan4f164f32022-02-26 22:07:12 +0000170
pineafan62ce1922022-08-25 20:34:45 +0100171const check = async (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100172 const member = interaction.member as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100173 const me = interaction.guild!.me!;
174 let apply = interaction.options.getUser("user") as User | GuildMember;
175 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
176 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
177 let applyPos = 0
178 try {
179 apply = await interaction.guild!.members.fetch(apply.id) as GuildMember
180 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
181 } catch {
182 apply = apply as User
183 }
pineafanc1c18792022-08-03 21:41:36 +0100184 // Do not allow banning the owner
pineafan62ce1922022-08-25 20:34:45 +0100185 if (member.id === interaction.guild!.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000186 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100187 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000188 // Check if Nucleus has permission to ban
pineafan3a02ea32022-08-11 21:35:04 +0100189 if (!me.permissions.has("BAN_MEMBERS")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000190 // Do not allow banning Nucleus
pineafan62ce1922022-08-25 20:34:45 +0100191 if (member.id === interaction.guild!.me!.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000192 // Allow the owner to ban anyone
pineafan62ce1922022-08-25 20:34:45 +0100193 if (member.id === interaction.guild!.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000194 // Check if the user has ban_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100195 if (!member.permissions.has("BAN_MEMBERS")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000196 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100197 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000198 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100199 return true;
200};
pineafan4f164f32022-02-26 22:07:12 +0000201
Skyler Grey75ea9172022-08-06 10:22:23 +0100202export { command, callback, check };