blob: 4560c8bd7c2b78bde2df55c03f69ebe19126026e [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001import {
2 CommandInteraction,
3 GuildMember,
4 MessageActionRow,
5 MessageButton
6} from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00007import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan4f164f32022-02-26 22:07:12 +00008import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01009import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +000010import keyValueList from "../../utils/generateKeyValueList.js";
pineafane625d782022-05-09 18:04:32 +010011import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +010012import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +000013
14const command = (builder: SlashCommandSubcommandBuilder) =>
15 builder
pineafan63fc5e22022-08-04 22:04:10 +010016 .setName("ban")
17 .setDescription("Bans a user from the server")
Skyler Grey75ea9172022-08-06 10:22:23 +010018 .addUserOption((option) =>
19 option
20 .setName("user")
21 .setDescription("The user to ban")
22 .setRequired(true)
23 )
24 .addNumberOption((option) =>
25 option
26 .setName("delete")
27 .setDescription("The days of messages to delete | Default: 0")
28 .setMinValue(0)
29 .setMaxValue(7)
30 .setRequired(false)
31 );
pineafan4f164f32022-02-26 22:07:12 +000032
pineafanbd02b4a2022-08-05 22:01:38 +010033const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan63fc5e22022-08-04 22:04:10 +010034 const { renderUser } = client.logger;
pineafan4f164f32022-02-26 22:07:12 +000035 // TODO:[Modals] Replace this with a modal
pineafan63fc5e22022-08-04 22:04:10 +010036 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010037 let notify = true;
38 let confirmation;
pineafan73a7c4a2022-07-24 10:38:04 +010039 while (true) {
40 confirmation = await new confirmationMessage(interaction)
41 .setEmoji("PUNISH.BAN.RED")
42 .setTitle("Ban")
Skyler Grey75ea9172022-08-06 10:22:23 +010043 .setDescription(
44 keyValueList({
45 user: renderUser(interaction.options.getUser("user")),
46 reason: reason
47 ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ")
48 : "*No reason provided*"
49 }) +
50 `The user **will${notify ? "" : " not"}** be notified\n` +
51 `${addPlurals(
52 interaction.options.getInteger("delete")
53 ? interaction.options.getInteger("delete")
54 : 0,
55 "day"
56 )} of messages will be deleted\n\n` +
57 `Are you sure you want to ban <@!${
58 (interaction.options.getMember("user") as GuildMember)
59 .id
60 }>?`
61 )
pineafan73a7c4a2022-07-24 10:38:04 +010062 .setColor("Danger")
63 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010064 .send(reason !== null);
65 reason = reason ?? "";
66 if (confirmation.cancelled) return;
67 if (confirmation.success) break;
68 if (confirmation.newReason) reason = confirmation.newReason;
Skyler Grey75ea9172022-08-06 10:22:23 +010069 if (confirmation.components)
70 notify = confirmation.components.notify.active;
pineafan73a7c4a2022-07-24 10:38:04 +010071 }
pineafan377794f2022-04-18 19:01:01 +010072 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010073 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000074 let dm;
pineafan63fc5e22022-08-04 22:04:10 +010075 const config = await client.database.guilds.read(interaction.guild.id);
pineafan4f164f32022-02-26 22:07:12 +000076 try {
pineafan02ba0232022-07-24 22:16:15 +010077 if (notify) {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 dm = await (
79 interaction.options.getMember("user") as GuildMember
80 ).send({
81 embeds: [
82 new EmojiEmbed()
83 .setEmoji("PUNISH.BAN.RED")
84 .setTitle("Banned")
85 .setDescription(
86 `You have been banned in ${interaction.guild.name}` +
87 (reason ? ` for:\n> ${reason}` : ".")
88 )
89 .setStatus("Danger")
pineafan377794f2022-04-18 19:01:01 +010090 ],
Skyler Grey75ea9172022-08-06 10:22:23 +010091 components: [
92 new MessageActionRow().addComponents(
93 config.moderation.ban.text
94 ? [
95 new MessageButton()
96 .setStyle("LINK")
97 .setLabel(config.moderation.ban.text)
98 .setURL(config.moderation.ban.link)
99 ]
100 : []
101 )
102 ]
pineafan63fc5e22022-08-04 22:04:10 +0100103 });
104 dmd = true;
pineafan4f164f32022-02-26 22:07:12 +0000105 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 } catch {
107 dmd = false;
108 }
pineafan4f164f32022-02-26 22:07:12 +0000109 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 const member = interaction.options.getMember("user") as GuildMember;
pineafane625d782022-05-09 18:04:32 +0100111 member.ban({
pineafan02ba0232022-07-24 22:16:15 +0100112 days: Number(interaction.options.getNumber("delete") ?? 0),
pineafan4edb7762022-06-26 19:21:04 +0100113 reason: reason ?? "No reason provided"
pineafan63fc5e22022-08-04 22:04:10 +0100114 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 await client.database.history.create(
116 "ban",
117 interaction.guild.id,
118 member.user,
119 interaction.user,
120 reason
121 );
122 const { log, NucleusColors, entry, renderUser, renderDelta } =
123 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +0100124 const data = {
pineafane625d782022-05-09 18:04:32 +0100125 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100126 type: "memberBan",
127 displayName: "Member Banned",
128 calculateType: "guildMemberPunish",
pineafane625d782022-05-09 18:04:32 +0100129 color: NucleusColors.red,
130 emoji: "PUNISH.BAN.RED",
131 timestamp: new Date().getTime()
132 },
133 list: {
pineafanda6e5342022-07-03 10:03:16 +0100134 memberId: entry(member.user.id, `\`${member.user.id}\``),
pineafane625d782022-05-09 18:04:32 +0100135 name: entry(member.user.id, renderUser(member.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 banned: entry(
137 new Date().getTime(),
138 renderDelta(new Date().getTime())
139 ),
140 bannedBy: entry(
141 interaction.user.id,
142 renderUser(interaction.user)
143 ),
144 reason: entry(
145 reason,
146 reason ? `\n> ${reason}` : "*No reason provided.*"
147 ),
148 accountCreated: entry(
149 member.user.createdAt,
150 renderDelta(member.user.createdAt)
151 ),
pineafan63fc5e22022-08-04 22:04:10 +0100152 serverMemberCount: interaction.guild.memberCount
pineafane625d782022-05-09 18:04:32 +0100153 },
154 hidden: {
155 guild: interaction.guild.id
156 }
pineafan63fc5e22022-08-04 22:04:10 +0100157 };
pineafan4edb7762022-06-26 19:21:04 +0100158 log(data);
pineafan4f164f32022-02-26 22:07:12 +0000159 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 await interaction.editReply({
161 embeds: [
162 new EmojiEmbed()
163 .setEmoji("PUNISH.BAN.RED")
164 .setTitle("Ban")
165 .setDescription(
166 "Something went wrong and the user was not banned"
167 )
168 .setStatus("Danger")
169 ],
170 components: []
171 });
pineafan63fc5e22022-08-04 22:04:10 +0100172 if (dmd) await dm.delete();
173 return;
pineafan4f164f32022-02-26 22:07:12 +0000174 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100175 const failed = !dmd && notify;
176 await interaction.editReply({
177 embeds: [
178 new EmojiEmbed()
179 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
180 .setTitle("Ban")
181 .setDescription(
182 "The member was banned" +
183 (failed ? ", but could not be notified" : "")
184 )
185 .setStatus(failed ? "Warning" : "Success")
186 ],
187 components: []
188 });
pineafan4f164f32022-02-26 22:07:12 +0000189 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100190 await interaction.editReply({
191 embeds: [
192 new EmojiEmbed()
193 .setEmoji("PUNISH.BAN.GREEN")
194 .setTitle("Ban")
195 .setDescription("No changes were made")
196 .setStatus("Success")
197 ],
198 components: []
199 });
pineafan4f164f32022-02-26 22:07:12 +0000200 }
pineafan63fc5e22022-08-04 22:04:10 +0100201};
pineafan4f164f32022-02-26 22:07:12 +0000202
pineafanbd02b4a2022-08-05 22:01:38 +0100203const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100204 const member = interaction.member as GuildMember;
205 const me = interaction.guild.me!;
206 const apply = interaction.options.getMember("user") as GuildMember;
207 if (member === null || me === null || apply === null)
208 throw "That member is not in the server";
pineafan63fc5e22022-08-04 22:04:10 +0100209 const memberPos = member.roles ? member.roles.highest.position : 0;
210 const mePos = me.roles ? me.roles.highest.position : 0;
211 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100212 // Do not allow banning the owner
Skyler Grey75ea9172022-08-06 10:22:23 +0100213 if (member.id === interaction.guild.ownerId)
214 throw "You cannot ban the owner of the server";
pineafan4f164f32022-02-26 22:07:12 +0000215 // Check if Nucleus can ban the member
Skyler Grey75ea9172022-08-06 10:22:23 +0100216 if (!(mePos > applyPos))
217 throw "I do not have a role higher than that member";
pineafan4f164f32022-02-26 22:07:12 +0000218 // Check if Nucleus has permission to ban
Skyler Grey75ea9172022-08-06 10:22:23 +0100219 if (!me.permissions.has("BAN_MEMBERS"))
220 throw "I do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000221 // Do not allow banning Nucleus
pineafan63fc5e22022-08-04 22:04:10 +0100222 if (member.id === interaction.guild.me.id) throw "I cannot ban myself";
pineafan4f164f32022-02-26 22:07:12 +0000223 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100224 if (member.id === interaction.guild.ownerId) return true;
pineafan4f164f32022-02-26 22:07:12 +0000225 // Check if the user has ban_members permission
Skyler Grey75ea9172022-08-06 10:22:23 +0100226 if (!member.permissions.has("BAN_MEMBERS"))
227 throw "You do not have the *Ban Members* permission";
pineafan4f164f32022-02-26 22:07:12 +0000228 // Check if the user is below on the role list
Skyler Grey75ea9172022-08-06 10:22:23 +0100229 if (!(memberPos > applyPos))
230 throw "You do not have a role higher than that member";
pineafan4f164f32022-02-26 22:07:12 +0000231 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100232 return true;
233};
pineafan4f164f32022-02-26 22:07:12 +0000234
Skyler Grey75ea9172022-08-06 10:22:23 +0100235export { command, callback, check };