blob: 5f4f41cc55ed49b4094da7906ecd47fbddcbf555 [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
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))
PineaFan64486c42022-12-28 09:21:04 +000015 .addStringOption((option) => option.setName("reason").setDescription("The reason for the ban").setRequired(false))
Skyler Grey75ea9172022-08-06 10:22:23 +010016 .addNumberOption((option) =>
17 option
18 .setName("delete")
19 .setDescription("The days of messages to delete | Default: 0")
20 .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(
PineaFan64486c42022-12-28 09:21:04 +000045 (interaction.options.get("delete")?.value as number | null) ?? 0,
Skyler Grey75ea9172022-08-06 10:22:23 +010046 "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,
55 null,
56 "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
71 let dmd = false;
72 let dm;
73 const config = await client.database.guilds.read(interaction.guild!.id);
74 try {
75 if (notify) {
76 dm = await (interaction.options.getMember("user") as GuildMember).send({
77 embeds: [
78 new EmojiEmbed()
79 .setEmoji("PUNISH.BAN.RED")
80 .setTitle("Banned")
81 .setDescription(
82 `You have been banned in ${interaction.guild!.name}` + (reason ? ` for:\n> ${reason}` : ".")
83 )
84 .setStatus("Danger")
85 ],
PineaFan64486c42022-12-28 09:21:04 +000086 components: config.moderation.ban.text ? [
87 new ActionRowBuilder().addComponents([
88 new ButtonBuilder()
89 .setStyle(ButtonStyle.Link)
90 .setLabel(config.moderation.ban.text)
91 .setURL(config.moderation.ban.link!)
92 ])
93 ] : []
pineafan62ce1922022-08-25 20:34:45 +010094 });
95 dmd = true;
96 }
97 } catch {
98 dmd = false;
99 }
100 try {
101 const member = interaction.options.getMember("user") as GuildMember;
102 member.ban({
103 days: Number(interaction.options.getNumber("delete") ?? 0),
104 reason: reason ?? "No reason provided"
105 });
106 await client.database.history.create("ban", interaction.guild!.id, member.user, interaction.user, reason);
107 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
108 const data = {
109 meta: {
110 type: "memberBan",
111 displayName: "Member Banned",
112 calculateType: "guildMemberPunish",
113 color: NucleusColors.red,
114 emoji: "PUNISH.BAN.RED",
115 timestamp: new Date().getTime()
116 },
117 list: {
118 memberId: entry(member.user.id, `\`${member.user.id}\``),
119 name: entry(member.user.id, renderUser(member.user)),
120 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
121 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
122 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
123 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
124 serverMemberCount: interaction.guild!.memberCount
125 },
126 hidden: {
127 guild: interaction.guild!.id
128 }
129 };
130 log(data);
131 } catch {
132 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 embeds: [
134 new EmojiEmbed()
135 .setEmoji("PUNISH.BAN.RED")
pineafan62ce1922022-08-25 20:34:45 +0100136 .setTitle("Ban")
137 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 .setStatus("Danger")
139 ],
pineafan62ce1922022-08-25 20:34:45 +0100140 components: []
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 });
pineafan62ce1922022-08-25 20:34:45 +0100142 if (dmd && dm) await dm.delete();
143 return;
pineafan4f164f32022-02-26 22:07:12 +0000144 }
pineafan62ce1922022-08-25 20:34:45 +0100145 const failed = !dmd && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100146 await interaction.editReply({
147 embeds: [
148 new EmojiEmbed()
pineafan62ce1922022-08-25 20:34:45 +0100149 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
Skyler Greyad002172022-08-16 18:48:26 +0100150 .setTitle("Ban")
pineafan62ce1922022-08-25 20:34:45 +0100151 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
152 .setStatus(failed ? "Warning" : "Success")
Skyler Greyad002172022-08-16 18:48:26 +0100153 ],
154 components: []
155 });
pineafan62ce1922022-08-25 20:34:45 +0100156 } else {
157 await interaction.editReply({
158 embeds: [
159 new EmojiEmbed()
160 .setEmoji("PUNISH.BAN.GREEN")
161 .setTitle("Ban")
162 .setDescription("No changes were made")
163 .setStatus("Success")
164 ],
165 components: []
166 });
Skyler Greyad002172022-08-16 18:48:26 +0100167 }
pineafan63fc5e22022-08-04 22:04:10 +0100168};
pineafan4f164f32022-02-26 22:07:12 +0000169
pineafan62ce1922022-08-25 20:34:45 +0100170const check = async (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100171 const member = interaction.member as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100172 const me = interaction.guild!.me!;
173 let apply = interaction.options.getUser("user") as User | GuildMember;
174 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
175 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
176 let applyPos = 0
177 try {
178 apply = await interaction.guild!.members.fetch(apply.id) as GuildMember
179 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
180 } catch {
181 apply = apply as User
182 }
pineafanc1c18792022-08-03 21:41:36 +0100183 // Do not allow banning the owner
pineafan62ce1922022-08-25 20:34:45 +0100184 if (member.id === interaction.guild!.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000185 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100186 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000187 // Check if Nucleus has permission to ban
pineafan3a02ea32022-08-11 21:35:04 +0100188 if (!me.permissions.has("BAN_MEMBERS")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000189 // Do not allow banning Nucleus
pineafan62ce1922022-08-25 20:34:45 +0100190 if (member.id === interaction.guild!.me!.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000191 // Allow the owner to ban anyone
pineafan62ce1922022-08-25 20:34:45 +0100192 if (member.id === interaction.guild!.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000193 // Check if the user has ban_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100194 if (!member.permissions.has("BAN_MEMBERS")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000195 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100196 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
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 };