blob: 1b404c986d3ee8737d74227a01ec18e4c18fbc29 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import Discord, { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, User, ButtonStyle } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05002import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan8b4b17f2022-02-27 20:42:52 +00003import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00005import keyValueList from "../../utils/generateKeyValueList.js";
PineaFan0d06edc2023-01-17 22:10:31 +00006import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
PineaFan0d06edc2023-01-17 22:10:31 +00008import { LinkWarningFooter } from "../../utils/defaults.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -05009import getEmojiByName from "../../utils/getEmojiByName.js";
PineaFan0d06edc2023-01-17 22:10:31 +000010
pineafan4f164f32022-02-26 22:07:12 +000011
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan63fc5e22022-08-04 22:04:10 +010014 .setName("softban")
15 .setDescription("Kicks a user and deletes their messages")
Skyler Grey11236ba2022-08-08 21:13:33 +010016 .addUserOption((option) => option.setName("user").setDescription("The user to softban").setRequired(true))
PineaFan0d06edc2023-01-17 22:10:31 +000017 .addNumberOption((option) =>
Skyler Grey75ea9172022-08-06 10:22:23 +010018 option
19 .setName("delete")
PineaFan0d06edc2023-01-17 22:10:31 +000020 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010021 .setMinValue(0)
22 .setMaxValue(7)
23 .setRequired(false)
24 );
pineafan4f164f32022-02-26 22:07:12 +000025
PineaFan0d06edc2023-01-17 22:10:31 +000026
27const callback = async (interaction: CommandInteraction): Promise<void> => {
28 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010029 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000030 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010031 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010032 let notify = true;
pineafan73a7c4a2022-07-24 10:38:04 +010033 let confirmation;
PineaFan0d06edc2023-01-17 22:10:31 +000034 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010035 let timedOut = false;
PineaFan0d06edc2023-01-17 22:10:31 +000036 do {
37 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010038 .setEmoji("PUNISH.BAN.RED")
39 .setTitle("Softban")
Skyler Grey75ea9172022-08-06 10:22:23 +010040 .setDescription(
41 keyValueList({
PineaFan0d06edc2023-01-17 22:10:31 +000042 user: renderUser(interaction.options.getUser("user")!),
43 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010044 }) +
45 `The user **will${notify ? "" : " not"}** be notified\n` +
PineaFan0d06edc2023-01-17 22:10:31 +000046 `${addPlurals(
47 (interaction.options.get("delete")?.value as number | null) ?? 0, "day")
48 } of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010049 `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010050 )
Skyler Grey75ea9172022-08-06 10:22:23 +010051 .addCustomBoolean(
52 "notify",
53 "Notify user",
54 false,
PineaFan0d06edc2023-01-17 22:10:31 +000055 undefined,
56 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000057 null,
Skyler Grey75ea9172022-08-06 10:22:23 +010058 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
59 notify
60 )
PineaFan0d06edc2023-01-17 22:10:31 +000061 .setColor("Danger")
pineafan73a7c4a2022-07-24 10:38:04 +010062 .addReasonButton(reason ?? "")
PineaFan0d06edc2023-01-17 22:10:31 +000063 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010064 .send(reason !== null);
65 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010066 if (confirmation.cancelled) timedOut = true;
PineaFan0d06edc2023-01-17 22:10:31 +000067 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010068 else if (confirmation.newReason) reason = confirmation.newReason;
PineaFan0d06edc2023-01-17 22:10:31 +000069 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
70 } while (!timedOut && !chosen)
71 if (timedOut || !confirmation.success) return;
72 reason = reason.length ? reason : null
73 let dmSent = false;
74 let dmMessage;
75 const config = await client.database.guilds.read(interaction.guild.id);
76 try {
77 if (notify) {
78 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
79 const messageData: {
80 embeds: EmojiEmbed[];
81 components: ActionRowBuilder<ButtonBuilder>[];
82 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 embeds: [
84 new EmojiEmbed()
85 .setEmoji("PUNISH.BAN.RED")
86 .setTitle("Softban")
PineaFan0d06edc2023-01-17 22:10:31 +000087 .setDescription(
88 `You have been softbanned from ${interaction.guild.name}` +
89 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
90 )
Skyler Grey75ea9172022-08-06 10:22:23 +010091 .setStatus("Danger")
92 ],
93 components: []
PineaFan0d06edc2023-01-17 22:10:31 +000094 };
95 if (config.moderation.softban.text && config.moderation.softban.link) {
96 messageData.embeds[0]!.setFooter(LinkWarningFooter)
97 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
98 .addComponents(new ButtonBuilder()
99 .setStyle(ButtonStyle.Link)
100 .setLabel(config.moderation.softban.text)
PineaFan9b2ac4d2023-01-18 14:41:07 +0000101 .setURL(config.moderation.softban.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
PineaFan0d06edc2023-01-17 22:10:31 +0000102 )
103 )
104 }
105 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
106 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000107 }
PineaFan0d06edc2023-01-17 22:10:31 +0000108 } catch {
109 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000110 }
PineaFan0d06edc2023-01-17 22:10:31 +0000111 try {
112 const member = interaction.options.getMember("user") as GuildMember;
113 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
114 member.ban({
115 deleteMessageSeconds: days * 24 * 60 * 60,
116 reason: reason ?? "*No reason provided*"
117 });
118 await interaction.guild.members.unban(member, "Softban");
119 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
120 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
121 const data = {
122 meta: {
123 type: "memberSoftban",
124 displayName: "Member Softbanned",
125 calculateType: "guildMemberPunish",
126 color: NucleusColors.yellow,
127 emoji: "PUNISH.BAN.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500128 timestamp: Date.now()
PineaFan0d06edc2023-01-17 22:10:31 +0000129 },
130 list: {
131 memberId: entry(member.user.id, `\`${member.user.id}\``),
132 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500133 softbanned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan0d06edc2023-01-17 22:10:31 +0000134 softbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
135 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
136 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
137 serverMemberCount: interaction.guild.memberCount
138 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500139 separate: {
140 end: getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + ` The user was ${notify ? "" : "not "}notified`
141 },
PineaFan0d06edc2023-01-17 22:10:31 +0000142 hidden: {
143 guild: interaction.guild.id
144 }
145 };
146 log(data);
147 } catch {
148 await interaction.editReply({
149 embeds: [
150 new EmojiEmbed()
151 .setEmoji("PUNISH.BAN.RED")
152 .setTitle("Softban")
153 .setDescription("Something went wrong and the user was not softbanned")
154 .setStatus("Danger")
155 ],
156 components: []
157 });
158 if (dmSent && dmMessage) await dmMessage.delete();
159 return;
160 }
161 const failed = !dmSent && notify;
162 await interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
166 .setTitle("Softban")
167 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
168 .setStatus(failed ? "Warning" : "Success")
169 ],
170 components: []
171 });
pineafan63fc5e22022-08-04 22:04:10 +0100172};
pineafan4f164f32022-02-26 22:07:12 +0000173
TheCodedProff86ba092023-01-27 17:10:07 -0500174const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFan0d06edc2023-01-17 22:10:31 +0000175 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500177 // Check if the user has ban_members permission
178 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
179 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +0000180 const me = interaction.guild.members.me!;
181 let apply = interaction.options.getUser("user") as User | GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100182 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
183 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
PineaFan0d06edc2023-01-17 22:10:31 +0000184 let applyPos = 0
185 try {
186 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
187 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
188 } catch {
189 apply = apply as User
190 }
191 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000192 if (member.id === interaction.guild.ownerId) return "You cannot softban the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000193 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500194 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000195 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000196 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
PineaFan0d06edc2023-01-17 22:10:31 +0000197 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000198 if (member.id === me.id) return "I cannot softban myself";
PineaFan0d06edc2023-01-17 22:10:31 +0000199 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100200 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000201 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500202 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
PineaFan0d06edc2023-01-17 22:10:31 +0000203 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100204 return true;
205};
pineafan4f164f32022-02-26 22:07:12 +0000206
Skyler Grey75ea9172022-08-06 10:22:23 +0100207export { command, callback, check };