blob: bd940fae0c116a29a2be8defa3f82b906ba743ed [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";
9
pineafan4f164f32022-02-26 22:07:12 +000010
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("softban")
14 .setDescription("Kicks a user and deletes their messages")
Skyler Grey11236ba2022-08-08 21:13:33 +010015 .addUserOption((option) => option.setName("user").setDescription("The user to softban").setRequired(true))
PineaFan0d06edc2023-01-17 22:10:31 +000016 .addNumberOption((option) =>
Skyler Grey75ea9172022-08-06 10:22:23 +010017 option
18 .setName("delete")
PineaFan0d06edc2023-01-17 22:10:31 +000019 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010020 .setMinValue(0)
21 .setMaxValue(7)
22 .setRequired(false)
23 );
pineafan4f164f32022-02-26 22:07:12 +000024
PineaFan0d06edc2023-01-17 22:10:31 +000025
26const callback = async (interaction: CommandInteraction): Promise<void> => {
27 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010028 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000029 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010030 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010031 let notify = true;
pineafan73a7c4a2022-07-24 10:38:04 +010032 let confirmation;
PineaFan0d06edc2023-01-17 22:10:31 +000033 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010034 let timedOut = false;
PineaFan0d06edc2023-01-17 22:10:31 +000035 do {
36 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010037 .setEmoji("PUNISH.BAN.RED")
38 .setTitle("Softban")
Skyler Grey75ea9172022-08-06 10:22:23 +010039 .setDescription(
40 keyValueList({
PineaFan0d06edc2023-01-17 22:10:31 +000041 user: renderUser(interaction.options.getUser("user")!),
42 reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010043 }) +
44 `The user **will${notify ? "" : " not"}** be notified\n` +
PineaFan0d06edc2023-01-17 22:10:31 +000045 `${addPlurals(
46 (interaction.options.get("delete")?.value as number | null) ?? 0, "day")
47 } of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010048 `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010049 )
Skyler Grey75ea9172022-08-06 10:22:23 +010050 .addCustomBoolean(
51 "notify",
52 "Notify user",
53 false,
PineaFan0d06edc2023-01-17 22:10:31 +000054 undefined,
55 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000056 null,
Skyler Grey75ea9172022-08-06 10:22:23 +010057 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
58 notify
59 )
PineaFan0d06edc2023-01-17 22:10:31 +000060 .setColor("Danger")
pineafan73a7c4a2022-07-24 10:38:04 +010061 .addReasonButton(reason ?? "")
PineaFan0d06edc2023-01-17 22:10:31 +000062 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010063 .send(reason !== null);
64 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010065 if (confirmation.cancelled) timedOut = true;
PineaFan0d06edc2023-01-17 22:10:31 +000066 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010067 else if (confirmation.newReason) reason = confirmation.newReason;
PineaFan0d06edc2023-01-17 22:10:31 +000068 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
69 } while (!timedOut && !chosen)
70 if (timedOut || !confirmation.success) return;
71 reason = reason.length ? reason : null
72 let dmSent = false;
73 let dmMessage;
74 const config = await client.database.guilds.read(interaction.guild.id);
75 try {
76 if (notify) {
77 if (reason) { reason = reason.split("\n").map((line) => "> " + line).join("\n") }
78 const messageData: {
79 embeds: EmojiEmbed[];
80 components: ActionRowBuilder<ButtonBuilder>[];
81 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.BAN.RED")
85 .setTitle("Softban")
PineaFan0d06edc2023-01-17 22:10:31 +000086 .setDescription(
87 `You have been softbanned from ${interaction.guild.name}` +
88 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
89 )
Skyler Grey75ea9172022-08-06 10:22:23 +010090 .setStatus("Danger")
91 ],
92 components: []
PineaFan0d06edc2023-01-17 22:10:31 +000093 };
94 if (config.moderation.softban.text && config.moderation.softban.link) {
95 messageData.embeds[0]!.setFooter(LinkWarningFooter)
96 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
97 .addComponents(new ButtonBuilder()
98 .setStyle(ButtonStyle.Link)
99 .setLabel(config.moderation.softban.text)
PineaFan9b2ac4d2023-01-18 14:41:07 +0000100 .setURL(config.moderation.softban.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
PineaFan0d06edc2023-01-17 22:10:31 +0000101 )
102 )
103 }
104 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
105 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000106 }
PineaFan0d06edc2023-01-17 22:10:31 +0000107 } catch {
108 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000109 }
PineaFan0d06edc2023-01-17 22:10:31 +0000110 try {
111 const member = interaction.options.getMember("user") as GuildMember;
112 const days: number = interaction.options.get("delete")?.value as number | null ?? 0;
113 member.ban({
114 deleteMessageSeconds: days * 24 * 60 * 60,
115 reason: reason ?? "*No reason provided*"
116 });
117 await interaction.guild.members.unban(member, "Softban");
118 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
119 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
120 const data = {
121 meta: {
122 type: "memberSoftban",
123 displayName: "Member Softbanned",
124 calculateType: "guildMemberPunish",
125 color: NucleusColors.yellow,
126 emoji: "PUNISH.BAN.YELLOW",
127 timestamp: new Date().getTime()
128 },
129 list: {
130 memberId: entry(member.user.id, `\`${member.user.id}\``),
131 name: entry(member.user.id, renderUser(member.user)),
132 softbanned: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
133 softbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
134 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
135 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
136 serverMemberCount: interaction.guild.memberCount
137 },
138 hidden: {
139 guild: interaction.guild.id
140 }
141 };
142 log(data);
143 } catch {
144 await interaction.editReply({
145 embeds: [
146 new EmojiEmbed()
147 .setEmoji("PUNISH.BAN.RED")
148 .setTitle("Softban")
149 .setDescription("Something went wrong and the user was not softbanned")
150 .setStatus("Danger")
151 ],
152 components: []
153 });
154 if (dmSent && dmMessage) await dmMessage.delete();
155 return;
156 }
157 const failed = !dmSent && notify;
158 await interaction.editReply({
159 embeds: [
160 new EmojiEmbed()
161 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
162 .setTitle("Softban")
163 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
164 .setStatus(failed ? "Warning" : "Success")
165 ],
166 components: []
167 });
pineafan63fc5e22022-08-04 22:04:10 +0100168};
pineafan4f164f32022-02-26 22:07:12 +0000169
TheCodedProff86ba092023-01-27 17:10:07 -0500170const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFan0d06edc2023-01-17 22:10:31 +0000171 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100172 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500173 // Check if the user has ban_members permission
174 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
175 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +0000176 const me = interaction.guild.members.me!;
177 let apply = interaction.options.getUser("user") as User | GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100178 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
179 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
PineaFan0d06edc2023-01-17 22:10:31 +0000180 let applyPos = 0
181 try {
182 apply = await interaction.guild.members.fetch(apply.id) as GuildMember
183 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
184 } catch {
185 apply = apply as User
186 }
187 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000188 if (member.id === interaction.guild.ownerId) return "You cannot softban the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000189 // Check if Nucleus can ban the member
PineaFan5d98a4b2023-01-19 16:15:47 +0000190 if (!(mePos > applyPos)) return "I do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000191 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000192 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
PineaFan0d06edc2023-01-17 22:10:31 +0000193 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000194 if (member.id === me.id) return "I cannot softban myself";
PineaFan0d06edc2023-01-17 22:10:31 +0000195 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100196 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000197 // Check if the user is below on the role list
PineaFan5d98a4b2023-01-19 16:15:47 +0000198 if (!(memberPos > applyPos)) return "You do not have a role higher than that member";
PineaFan0d06edc2023-01-17 22:10:31 +0000199 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100200 return true;
201};
pineafan4f164f32022-02-26 22:07:12 +0000202
Skyler Grey75ea9172022-08-06 10:22:23 +0100203export { command, callback, check };