blob: 984bb6b4bc7979673d77ae46754cbd0244621604 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import Discord, {
2 CommandInteraction,
3 GuildMember,
4 ActionRowBuilder,
5 ButtonBuilder,
6 User,
7 ButtonStyle
8} from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05009import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan8b4b17f2022-02-27 20:42:52 +000010import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +010011import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +000012import keyValueList from "../../utils/generateKeyValueList.js";
PineaFan0d06edc2023-01-17 22:10:31 +000013import addPlurals from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +010014import client from "../../utils/client.js";
PineaFan0d06edc2023-01-17 22:10:31 +000015import { LinkWarningFooter } from "../../utils/defaults.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -050016import getEmojiByName from "../../utils/getEmojiByName.js";
PineaFan0d06edc2023-01-17 22:10:31 +000017
pineafan4f164f32022-02-26 22:07:12 +000018const command = (builder: SlashCommandSubcommandBuilder) =>
19 builder
pineafan63fc5e22022-08-04 22:04:10 +010020 .setName("softban")
21 .setDescription("Kicks a user and deletes their messages")
Skyler Grey11236ba2022-08-08 21:13:33 +010022 .addUserOption((option) => option.setName("user").setDescription("The user to softban").setRequired(true))
PineaFan0d06edc2023-01-17 22:10:31 +000023 .addNumberOption((option) =>
Skyler Grey75ea9172022-08-06 10:22:23 +010024 option
25 .setName("delete")
PineaFan0d06edc2023-01-17 22:10:31 +000026 .setDescription("Delete this number of days of messages from the user | Default: 0")
Skyler Grey75ea9172022-08-06 10:22:23 +010027 .setMinValue(0)
28 .setMaxValue(7)
29 .setRequired(false)
30 );
pineafan4f164f32022-02-26 22:07:12 +000031
PineaFan0d06edc2023-01-17 22:10:31 +000032const callback = async (interaction: CommandInteraction): Promise<void> => {
33 if (!interaction.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010034 const { renderUser } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000035 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010036 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010037 let notify = true;
pineafan73a7c4a2022-07-24 10:38:04 +010038 let confirmation;
PineaFan0d06edc2023-01-17 22:10:31 +000039 let chosen = false;
Skyler Greyad002172022-08-16 18:48:26 +010040 let timedOut = false;
PineaFan0d06edc2023-01-17 22:10:31 +000041 do {
42 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010043 .setEmoji("PUNISH.BAN.RED")
44 .setTitle("Softban")
Skyler Grey75ea9172022-08-06 10:22:23 +010045 .setDescription(
46 keyValueList({
PineaFan0d06edc2023-01-17 22:10:31 +000047 user: renderUser(interaction.options.getUser("user")!),
Skyler Greyda16adf2023-03-05 10:22:12 +000048 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010049 }) +
50 `The user **will${notify ? "" : " not"}** be notified\n` +
PineaFan0d06edc2023-01-17 22:10:31 +000051 `${addPlurals(
Skyler Greyda16adf2023-03-05 10:22:12 +000052 (interaction.options.get("delete")?.value as number | null) ?? 0,
53 "day"
54 )} of messages will be deleted\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010055 `Are you sure you want to softban <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010056 )
Skyler Grey75ea9172022-08-06 10:22:23 +010057 .addCustomBoolean(
58 "notify",
59 "Notify user",
60 false,
PineaFan0d06edc2023-01-17 22:10:31 +000061 undefined,
62 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000063 null,
Skyler Grey75ea9172022-08-06 10:22:23 +010064 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
65 notify
66 )
PineaFan0d06edc2023-01-17 22:10:31 +000067 .setColor("Danger")
pineafan73a7c4a2022-07-24 10:38:04 +010068 .addReasonButton(reason ?? "")
PineaFan0d06edc2023-01-17 22:10:31 +000069 .setFailedMessage("No changes were made", "Success", "PUNISH.BAN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010070 .send(reason !== null);
71 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010072 if (confirmation.cancelled) timedOut = true;
PineaFan0d06edc2023-01-17 22:10:31 +000073 else if (confirmation.success !== undefined) chosen = true;
Skyler Greyad002172022-08-16 18:48:26 +010074 else if (confirmation.newReason) reason = confirmation.newReason;
PineaFan0d06edc2023-01-17 22:10:31 +000075 else if (confirmation.components) notify = confirmation.components["notify"]!.active;
Skyler Greyda16adf2023-03-05 10:22:12 +000076 } while (!timedOut && !chosen);
PineaFan0d06edc2023-01-17 22:10:31 +000077 if (timedOut || !confirmation.success) return;
Skyler Greyda16adf2023-03-05 10:22:12 +000078 reason = reason.length ? reason : null;
PineaFan0d06edc2023-01-17 22:10:31 +000079 let dmSent = false;
80 let dmMessage;
81 const config = await client.database.guilds.read(interaction.guild.id);
82 try {
83 if (notify) {
Skyler Greyda16adf2023-03-05 10:22:12 +000084 if (reason) {
85 reason = reason
86 .split("\n")
87 .map((line) => "> " + line)
88 .join("\n");
89 }
PineaFan0d06edc2023-01-17 22:10:31 +000090 const messageData: {
91 embeds: EmojiEmbed[];
92 components: ActionRowBuilder<ButtonBuilder>[];
93 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 embeds: [
95 new EmojiEmbed()
96 .setEmoji("PUNISH.BAN.RED")
97 .setTitle("Softban")
PineaFan0d06edc2023-01-17 22:10:31 +000098 .setDescription(
99 `You have been softbanned from ${interaction.guild.name}` +
100 (reason ? ` for:\n${reason}` : ".\n*No reason was provided.*")
101 )
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 .setStatus("Danger")
103 ],
104 components: []
PineaFan0d06edc2023-01-17 22:10:31 +0000105 };
106 if (config.moderation.softban.text && config.moderation.softban.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000107 messageData.embeds[0]!.setFooter(LinkWarningFooter);
108 messageData.components.push(
109 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
110 new ButtonBuilder()
PineaFan0d06edc2023-01-17 22:10:31 +0000111 .setStyle(ButtonStyle.Link)
112 .setLabel(config.moderation.softban.text)
Skyler Greyda16adf2023-03-05 10:22:12 +0000113 .setURL(
114 config.moderation.softban.link.replaceAll(
115 "{id}",
116 (interaction.options.getMember("user") as GuildMember).id
117 )
118 )
119 )
120 );
PineaFan0d06edc2023-01-17 22:10:31 +0000121 }
122 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
123 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000124 }
PineaFan0d06edc2023-01-17 22:10:31 +0000125 } catch {
126 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000127 }
PineaFan0d06edc2023-01-17 22:10:31 +0000128 try {
129 const member = interaction.options.getMember("user") as GuildMember;
Skyler Greyda16adf2023-03-05 10:22:12 +0000130 const days: number = (interaction.options.get("delete")?.value as number | null) ?? 0;
Skyler Greyf4f21c42023-03-08 14:36:29 +0000131 await member.ban({
PineaFan0d06edc2023-01-17 22:10:31 +0000132 deleteMessageSeconds: days * 24 * 60 * 60,
133 reason: reason ?? "*No reason provided*"
134 });
135 await interaction.guild.members.unban(member, "Softban");
136 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
137 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
138 const data = {
139 meta: {
140 type: "memberSoftban",
141 displayName: "Member Softbanned",
142 calculateType: "guildMemberPunish",
143 color: NucleusColors.yellow,
144 emoji: "PUNISH.BAN.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500145 timestamp: Date.now()
PineaFan0d06edc2023-01-17 22:10:31 +0000146 },
147 list: {
148 memberId: entry(member.user.id, `\`${member.user.id}\``),
149 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500150 softbanned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan0d06edc2023-01-17 22:10:31 +0000151 softbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
152 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
153 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
154 serverMemberCount: interaction.guild.memberCount
155 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500156 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000157 end:
158 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
159 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500160 },
PineaFan0d06edc2023-01-17 22:10:31 +0000161 hidden: {
162 guild: interaction.guild.id
163 }
164 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000165 await log(data);
PineaFan0d06edc2023-01-17 22:10:31 +0000166 } catch {
167 await interaction.editReply({
168 embeds: [
169 new EmojiEmbed()
170 .setEmoji("PUNISH.BAN.RED")
171 .setTitle("Softban")
172 .setDescription("Something went wrong and the user was not softbanned")
173 .setStatus("Danger")
174 ],
175 components: []
176 });
177 if (dmSent && dmMessage) await dmMessage.delete();
178 return;
179 }
180 const failed = !dmSent && notify;
181 await interaction.editReply({
182 embeds: [
183 new EmojiEmbed()
184 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
185 .setTitle("Softban")
186 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
187 .setStatus(failed ? "Warning" : "Success")
188 ],
189 components: []
190 });
pineafan63fc5e22022-08-04 22:04:10 +0100191};
pineafan4f164f32022-02-26 22:07:12 +0000192
TheCodedProff86ba092023-01-27 17:10:07 -0500193const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFan0d06edc2023-01-17 22:10:31 +0000194 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500196 // Check if the user has ban_members permission
197 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
198 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +0000199 const me = interaction.guild.members.me!;
200 let apply = interaction.options.getUser("user") as User | GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100201 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
202 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
Skyler Greyda16adf2023-03-05 10:22:12 +0000203 let applyPos = 0;
PineaFan0d06edc2023-01-17 22:10:31 +0000204 try {
Skyler Greyda16adf2023-03-05 10:22:12 +0000205 apply = (await interaction.guild.members.fetch(apply.id)) as GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000206 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
207 } catch {
Skyler Greyda16adf2023-03-05 10:22:12 +0000208 apply = apply as User;
PineaFan0d06edc2023-01-17 22:10:31 +0000209 }
210 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000211 if (member.id === interaction.guild.ownerId) return "You cannot softban the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000212 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500213 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000214 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000215 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
PineaFan0d06edc2023-01-17 22:10:31 +0000216 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000217 if (member.id === me.id) return "I cannot softban myself";
PineaFan0d06edc2023-01-17 22:10:31 +0000218 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100219 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000220 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500221 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
PineaFan0d06edc2023-01-17 22:10:31 +0000222 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100223 return true;
224};
pineafan4f164f32022-02-26 22:07:12 +0000225
Skyler Grey75ea9172022-08-06 10:22:23 +0100226export { command, callback, check };