blob: 4f2d4d751991aa38f84dc68db66be55f1191fa3d [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } 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;
pineafan73a7c4a2022-07-24 10:38:04 +010029 while (true) {
30 confirmation = await new confirmationMessage(interaction)
31 .setEmoji("PUNISH.BAN.RED")
32 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010033 .setDescription(
34 keyValueList({
35 user: renderUser(interaction.options.getUser("user")),
Skyler Grey11236ba2022-08-08 21:13:33 +010036 reason: reason ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010037 }) +
38 `The user **will${notify ? "" : " not"}** be notified\n` +
39 `${addPlurals(
Skyler Grey11236ba2022-08-08 21:13:33 +010040 interaction.options.getInteger("delete") ? interaction.options.getInteger("delete") : 0,
Skyler Grey75ea9172022-08-06 10:22:23 +010041 "day"
42 )} of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010043 `Are you sure you want to ban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010044 )
pineafan73a7c4a2022-07-24 10:38:04 +010045 .setColor("Danger")
46 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010047 .send(reason !== null);
48 reason = reason ?? "";
49 if (confirmation.cancelled) return;
50 if (confirmation.success) break;
51 if (confirmation.newReason) reason = confirmation.newReason;
Skyler Grey11236ba2022-08-08 21:13:33 +010052 if (confirmation.components) notify = confirmation.components.notify.active;
pineafan73a7c4a2022-07-24 10:38:04 +010053 }
pineafan377794f2022-04-18 19:01:01 +010054 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010055 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000056 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010057 const config = await client.database.guilds.read(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000058 try {
pineafan02ba0232022-07-24 22:16:15 +010059 if (notify) {
Skyler Grey11236ba2022-08-08 21:13:33 +010060 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010061 embeds: [
62 new EmojiEmbed()
63 .setEmoji("PUNISH.BAN.RED")
64 .setTitle("Banned")
65 .setDescription(
66 `You have been banned in ${interaction.guild.name}` +
67 (reason ? ` for:\n> ${reason}` : ".")
68 )
69 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010070 ],
Skyler Grey75ea9172022-08-06 10:22:23 +010071 components: [
72 new MessageActionRow().addComponents(
73 config.moderation.ban.text
74 ? [
75 new MessageButton()
76 .setStyle("LINK")
77 .setLabel(config.moderation.ban.text)
78 .setURL(config.moderation.ban.link)
79 ]
80 : []
81 )
82 ]
pineafan63fc5e22022-08-04 22:04:10 +010083 });
84 dmd = true;
pineafan4f164f32022-02-26 22:07:12 +000085 }
Skyler Grey75ea9172022-08-06 10:22:23 +010086 } catch {
87 dmd = false;
88 }
pineafan4f164f32022-02-26 22:07:12 +000089 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010090 const member = interaction.options.getMember("user") as GuildMember;
pineafane625d782022-05-09 18:04:32 +010091 member.ban({
pineafan02ba0232022-07-24 22:16:15 +010092 days: Number(interaction.options.getNumber("delete") ?? 0),
pineafan4edb7762022-06-26 19:21:04 +010093 reason: reason ?? "No reason provided"
pineafan63fc5e22022-08-04 22:04:10 +010094 });
Skyler Grey11236ba2022-08-08 21:13:33 +010095 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
96 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010097 const data = {
pineafane625d782022-05-09 18:04:32 +010098 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010099 type: "memberBan",
100 displayName: "Member Banned",
101 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +0100102 color: NucleusColors.red,
103 emoji: "PUNISH.BAN.RED",
104 timestamp: new Date().getTime()
105 },
106 list: {
pineafanda6e5342022-07-03 10:03:16 +0100107 memberId: entry(member.user.id, `\`${member.user.id}\``),
pineafane625d782022-05-09 18:04:32 +0100108 name: entry(member.user.id, renderUser(member.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100109 banned: entry(new Date().getTime(), renderDelta(new Date().getTime())),
110 bannedBy: entry(interaction.user.id, renderUser(interaction.user)),
111 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
112 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
pineafan63fc5e22022-08-04 22:04:10 +0100113 serverMemberCount: interaction.guild.memberCount
pineafane625d782022-05-09 18:04:32 +0100114 },
115 hidden: {
116 guild: interaction.guild.id
117 }
pineafan63fc5e22022-08-04 22:04:10 +0100118 };
pineafan4edb7762022-06-26 19:21:04 +0100119 log(data);
pineafan4f164f32022-02-26 22:07:12 +0000120 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 await interaction.editReply({
122 embeds: [
123 new EmojiEmbed()
124 .setEmoji("PUNISH.BAN.RED")
125 .setTitle("Ban")
Skyler Grey11236ba2022-08-08 21:13:33 +0100126 .setDescription("Something went wrong and the user was not banned")
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 .setStatus("Danger")
128 ],
129 components: []
130 });
pineafan63fc5e22022-08-04 22:04:10 +0100131 if (dmd) await dm.delete();
132 return;
pineafan4f164f32022-02-26 22:07:12 +0000133 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 const failed = !dmd && notify;
135 await interaction.editReply({
136 embeds: [
137 new EmojiEmbed()
138 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
139 .setTitle("Ban")
Skyler Grey11236ba2022-08-08 21:13:33 +0100140 .setDescription("The member was banned" + (failed ? ", but could not be notified" : ""))
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 .setStatus(failed ? "Warning" : "Success")
142 ],
143 components: []
144 });
pineafan4f164f32022-02-26 22:07:12 +0000145 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 await interaction.editReply({
147 embeds: [
148 new EmojiEmbed()
149 .setEmoji("PUNISH.BAN.GREEN")
150 .setTitle("Ban")
151 .setDescription("No changes were made")
152 .setStatus("Success")
153 ],
154 components: []
155 });
pineafan4f164f32022-02-26 22:07:12 +0000156 }
pineafan63fc5e22022-08-04 22:04:10 +0100157};
pineafan4f164f32022-02-26 22:07:12 +0000158
pineafanbd02b4a2022-08-05 22:01:38 +0100159const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 const member = interaction.member as GuildMember;
161 const me = interaction.guild.me!;
162 const apply = interaction.options.getMember("user") as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100163 if (member === null || me === null || apply === null) throw new Error("That member is not in the server");
pineafan63fc5e22022-08-04 22:04:10 +0100164 const memberPos = member.roles ? member.roles.highest.position : 0;
165 const mePos = me.roles ? me.roles.highest.position : 0;
166 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100167 // Do not allow banning the owner
pineafan3a02ea32022-08-11 21:35:04 +0100168 if (member.id === interaction.guild.ownerId) throw new Error("You cannot ban the owner of the server");
pineafan4f164f32022-02-26 22:07:12 +0000169 // Check if Nucleus can ban the member
pineafan3a02ea32022-08-11 21:35:04 +0100170 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000171 // Check if Nucleus has permission to ban
pineafan3a02ea32022-08-11 21:35:04 +0100172 if (!me.permissions.has("BAN_MEMBERS")) throw new Error("I do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000173 // Do not allow banning Nucleus
pineafan3a02ea32022-08-11 21:35:04 +0100174 if (member.id === interaction.guild.me.id) throw new Error("I cannot ban myself");
pineafan4f164f32022-02-26 22:07:12 +0000175 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100176 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000177 // Check if the user has ban_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100178 if (!member.permissions.has("BAN_MEMBERS")) throw new Error("You do not have the *Ban Members* permission");
pineafan4f164f32022-02-26 22:07:12 +0000179 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100180 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan4f164f32022-02-26 22:07:12 +0000181 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100182 return true;
183};
pineafan4f164f32022-02-26 22:07:12 +0000184
Skyler Grey75ea9172022-08-06 10:22:23 +0100185export { command, callback, check };