blob: 19605c76e2a774f686aced2516c2cb38a8934574 [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) {
PineaFana6e37932023-09-25 22:09:08 +010084 let formattedReason: string | null = null;
Skyler Greyda16adf2023-03-05 10:22:12 +000085 if (reason) {
PineaFana6e37932023-09-25 22:09:08 +010086 formattedReason = reason
Skyler Greyda16adf2023-03-05 10:22:12 +000087 .split("\n")
88 .map((line) => "> " + line)
89 .join("\n");
90 }
PineaFan0d06edc2023-01-17 22:10:31 +000091 const messageData: {
92 embeds: EmojiEmbed[];
93 components: ActionRowBuilder<ButtonBuilder>[];
94 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 embeds: [
96 new EmojiEmbed()
97 .setEmoji("PUNISH.BAN.RED")
98 .setTitle("Softban")
PineaFan0d06edc2023-01-17 22:10:31 +000099 .setDescription(
100 `You have been softbanned from ${interaction.guild.name}` +
PineaFana6e37932023-09-25 22:09:08 +0100101 (formattedReason ? ` for:\n${formattedReason}` : ".\n*No reason was provided.*")
PineaFan0d06edc2023-01-17 22:10:31 +0000102 )
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 .setStatus("Danger")
104 ],
105 components: []
PineaFan0d06edc2023-01-17 22:10:31 +0000106 };
107 if (config.moderation.softban.text && config.moderation.softban.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000108 messageData.embeds[0]!.setFooter(LinkWarningFooter);
109 messageData.components.push(
110 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
111 new ButtonBuilder()
PineaFan0d06edc2023-01-17 22:10:31 +0000112 .setStyle(ButtonStyle.Link)
113 .setLabel(config.moderation.softban.text)
Skyler Greyda16adf2023-03-05 10:22:12 +0000114 .setURL(
115 config.moderation.softban.link.replaceAll(
116 "{id}",
117 (interaction.options.getMember("user") as GuildMember).id
118 )
119 )
120 )
121 );
PineaFan0d06edc2023-01-17 22:10:31 +0000122 }
123 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
124 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000125 }
PineaFan0d06edc2023-01-17 22:10:31 +0000126 } catch {
127 dmSent = false;
pineafan8b4b17f2022-02-27 20:42:52 +0000128 }
PineaFan0d06edc2023-01-17 22:10:31 +0000129 try {
130 const member = interaction.options.getMember("user") as GuildMember;
Skyler Greyda16adf2023-03-05 10:22:12 +0000131 const days: number = (interaction.options.get("delete")?.value as number | null) ?? 0;
Skyler Greyf4f21c42023-03-08 14:36:29 +0000132 await member.ban({
PineaFan0d06edc2023-01-17 22:10:31 +0000133 deleteMessageSeconds: days * 24 * 60 * 60,
134 reason: reason ?? "*No reason provided*"
135 });
136 await interaction.guild.members.unban(member, "Softban");
137 await client.database.history.create("ban", interaction.guild.id, member.user, interaction.user, reason);
138 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
139 const data = {
140 meta: {
141 type: "memberSoftban",
142 displayName: "Member Softbanned",
143 calculateType: "guildMemberPunish",
144 color: NucleusColors.yellow,
145 emoji: "PUNISH.BAN.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500146 timestamp: Date.now()
PineaFan0d06edc2023-01-17 22:10:31 +0000147 },
148 list: {
149 memberId: entry(member.user.id, `\`${member.user.id}\``),
150 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500151 softbanned: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan0d06edc2023-01-17 22:10:31 +0000152 softbannedBy: entry(interaction.user.id, renderUser(interaction.user)),
153 reason: entry(reason, reason ? `\n> ${reason}` : "*No reason provided.*"),
154 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
155 serverMemberCount: interaction.guild.memberCount
156 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500157 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000158 end:
159 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
160 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500161 },
PineaFan0d06edc2023-01-17 22:10:31 +0000162 hidden: {
163 guild: interaction.guild.id
164 }
165 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000166 await log(data);
PineaFan0d06edc2023-01-17 22:10:31 +0000167 } catch {
168 await interaction.editReply({
169 embeds: [
170 new EmojiEmbed()
171 .setEmoji("PUNISH.BAN.RED")
172 .setTitle("Softban")
173 .setDescription("Something went wrong and the user was not softbanned")
174 .setStatus("Danger")
175 ],
176 components: []
177 });
178 if (dmSent && dmMessage) await dmMessage.delete();
179 return;
180 }
181 const failed = !dmSent && notify;
182 await interaction.editReply({
183 embeds: [
184 new EmojiEmbed()
185 .setEmoji(`PUNISH.BAN.${failed ? "YELLOW" : "GREEN"}`)
186 .setTitle("Softban")
187 .setDescription("The member was softbanned" + (failed ? ", but could not be notified" : ""))
188 .setStatus(failed ? "Warning" : "Success")
189 ],
190 components: []
191 });
pineafan63fc5e22022-08-04 22:04:10 +0100192};
pineafan4f164f32022-02-26 22:07:12 +0000193
TheCodedProff86ba092023-01-27 17:10:07 -0500194const check = async (interaction: CommandInteraction, partial: boolean = false) => {
PineaFan0d06edc2023-01-17 22:10:31 +0000195 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500197 // Check if the user has ban_members permission
198 if (!member.permissions.has("BanMembers")) return "You do not have the *Ban Members* permission";
199 if (partial) return true;
PineaFan0d06edc2023-01-17 22:10:31 +0000200 const me = interaction.guild.members.me!;
201 let apply = interaction.options.getUser("user") as User | GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100202 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
203 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
Skyler Greyda16adf2023-03-05 10:22:12 +0000204 let applyPos = 0;
PineaFan0d06edc2023-01-17 22:10:31 +0000205 try {
Skyler Greyda16adf2023-03-05 10:22:12 +0000206 apply = (await interaction.guild.members.fetch(apply.id)) as GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000207 applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
208 } catch {
Skyler Greyda16adf2023-03-05 10:22:12 +0000209 apply = apply as User;
PineaFan0d06edc2023-01-17 22:10:31 +0000210 }
211 // Do not allow banning the owner
PineaFan5d98a4b2023-01-19 16:15:47 +0000212 if (member.id === interaction.guild.ownerId) return "You cannot softban the owner of the server";
pineafan8b4b17f2022-02-27 20:42:52 +0000213 // Check if Nucleus can ban the member
TheCodedProf0941da42023-02-18 20:28:04 -0500214 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan8b4b17f2022-02-27 20:42:52 +0000215 // Check if Nucleus has permission to ban
PineaFan5d98a4b2023-01-19 16:15:47 +0000216 if (!me.permissions.has("BanMembers")) return "I do not have the *Ban Members* permission";
PineaFan0d06edc2023-01-17 22:10:31 +0000217 // Do not allow banning Nucleus
PineaFan5d98a4b2023-01-19 16:15:47 +0000218 if (member.id === me.id) return "I cannot softban myself";
PineaFan0d06edc2023-01-17 22:10:31 +0000219 // Allow the owner to ban anyone
pineafan63fc5e22022-08-04 22:04:10 +0100220 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000221 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500222 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
PineaFan0d06edc2023-01-17 22:10:31 +0000223 // Allow ban
pineafan63fc5e22022-08-04 22:04:10 +0100224 return true;
225};
pineafan4f164f32022-02-26 22:07:12 +0000226
Skyler Grey75ea9172022-08-06 10:22:23 +0100227export { command, callback, check };