blob: 51255873f26d83557fa262184e4e59a984bdbf4a [file] [log] [blame]
TheCodedProf21c08592022-09-13 14:14:43 -04001import Discord, { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
PineaFan100df682023-01-02 13:26:08 +00002import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
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";
Skyler Grey11236ba2022-08-08 21:13:33 +01006import { create, areTicketsEnabled } from "../../actions/createModActionTicket.js";
pineafan63fc5e22022-08-04 22:04:10 +01007import client from "../../utils/client.js";
PineaFan100df682023-01-02 13:26:08 +00008import { LinkWarningFooter } from "../../utils/defaultEmbeds.js";
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("warn")
13 .setDescription("Warns a user")
Skyler Grey11236ba2022-08-08 21:13:33 +010014 .addUserOption((option) => option.setName("user").setDescription("The user to warn").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000015
pineafan3a02ea32022-08-11 21:35:04 +010016const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010017 const { log, NucleusColors, renderUser, entry } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000018 // TODO:[Modals] Replace this with a modal
PineaFan100df682023-01-02 13:26:08 +000019 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010020 let notify = true;
21 let createAppealTicket = false;
pineafan73a7c4a2022-07-24 10:38:04 +010022 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010023 let timedOut = false;
24 let success = false;
pineafan62ce1922022-08-25 20:34:45 +010025 do {
pineafan73a7c4a2022-07-24 10:38:04 +010026 confirmation = await new confirmationMessage(interaction)
27 .setEmoji("PUNISH.WARN.RED")
28 .setTitle("Warn")
Skyler Grey75ea9172022-08-06 10:22:23 +010029 .setDescription(
30 keyValueList({
PineaFan100df682023-01-02 13:26:08 +000031 user: renderUser(interaction.options.getUser("user")!),
Skyler Greyad002172022-08-16 18:48:26 +010032 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010033 }) +
Skyler Grey11236ba2022-08-08 21:13:33 +010034 `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010035 )
pineafan73a7c4a2022-07-24 10:38:04 +010036 .setColor("Danger")
37 .addCustomBoolean(
Skyler Grey75ea9172022-08-06 10:22:23 +010038 "appeal",
39 "Create appeal ticket",
PineaFan100df682023-01-02 13:26:08 +000040 !(await areTicketsEnabled(interaction.guild!.id)),
41 async () => await create(interaction.guild!, interaction.options.getUser("user")!, interaction.user, reason),
42 "An appeal ticket will be created",
Skyler Grey75ea9172022-08-06 10:22:23 +010043 "CONTROL.TICKET",
44 createAppealTicket
45 )
46 .addCustomBoolean(
47 "notify",
48 "Notify user",
49 false,
50 null,
PineaFan100df682023-01-02 13:26:08 +000051 "The user will be sent a DM",
Skyler Grey75ea9172022-08-06 10:22:23 +010052 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
53 notify
54 )
pineafan73a7c4a2022-07-24 10:38:04 +010055 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010056 .send(reason !== null);
57 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010058 if (confirmation.cancelled) timedOut = true;
pineafan62ce1922022-08-25 20:34:45 +010059 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010060 else if (confirmation.newReason) reason = confirmation.newReason;
61 else if (confirmation.components) {
PineaFan100df682023-01-02 13:26:08 +000062 notify = confirmation.components["notify"]!.active;
63 createAppealTicket = confirmation.components["appeal"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010064 }
pineafan62ce1922022-08-25 20:34:45 +010065 } while (!timedOut && !success)
Skyler Greyad002172022-08-16 18:48:26 +010066 if (timedOut) return;
pineafan377794f2022-04-18 19:01:01 +010067 if (confirmation.success) {
PineaFan100df682023-01-02 13:26:08 +000068 let dmSent = false;
69 const config = await client.database.guilds.read(interaction.guild!.id);
pineafan8b4b17f2022-02-27 20:42:52 +000070 try {
pineafan02ba0232022-07-24 22:16:15 +010071 if (notify) {
PineaFan100df682023-01-02 13:26:08 +000072 const messageData: {
73 embeds: EmojiEmbed[];
74 components: ActionRowBuilder<ButtonBuilder>[];
75 } = {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 embeds: [
77 new EmojiEmbed()
78 .setEmoji("PUNISH.WARN.RED")
79 .setTitle("Warned")
80 .setDescription(
PineaFan100df682023-01-02 13:26:08 +000081 `You have been warned in ${interaction.guild!.name}` +
82 (reason ? ` for:\n> ${reason}` : ".\n*No reason was provided*") +
Skyler Grey75ea9172022-08-06 10:22:23 +010083 "\n\n" +
PineaFan100df682023-01-02 13:26:08 +000084 (createAppealTicket
85 ? `You can appeal this in the ticket created in <#${confirmation.components!["appeal"]!.response}>`
Skyler Grey75ea9172022-08-06 10:22:23 +010086 : "")
87 )
88 .setStatus("Danger")
pineafan02ba0232022-07-24 22:16:15 +010089 ],
PineaFan100df682023-01-02 13:26:08 +000090 components: []
91 };
92 if (config.moderation.warn.text && config.moderation.warn.link) {
93 messageData.embeds[0]!.setFooter(LinkWarningFooter)
94 messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>()
95 .addComponents(new ButtonBuilder()
96 .setStyle(ButtonStyle.Link)
97 .setLabel(config.moderation.warn.text)
98 .setURL(config.moderation.warn.link)
99 )
100 )
101 }
102 await (interaction.options.getMember("user") as GuildMember).send(messageData);
103 dmSent = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000104 }
PineaFan100df682023-01-02 13:26:08 +0000105 } catch (e) {
106 dmSent = false;
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 }
pineafan63fc5e22022-08-04 22:04:10 +0100108 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100110 type: "memberWarn",
111 displayName: "Member warned",
112 calculateType: "guildMemberPunish",
pineafan1dc15722022-03-14 21:27:34 +0000113 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100114 emoji: "PUNISH.WARN.YELLOW",
pineafan1dc15722022-03-14 21:27:34 +0000115 timestamp: new Date().getTime()
116 },
117 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 user: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +0100119 (interaction.options.getMember("user") as GuildMember).user.id,
120 renderUser((interaction.options.getMember("user") as GuildMember).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 ),
PineaFan100df682023-01-02 13:26:08 +0000122 warnedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)),
123 reason: reason ? `\n> ${reason}` : "*No reason provided*"
pineafan1dc15722022-03-14 21:27:34 +0000124 },
125 hidden: {
PineaFan100df682023-01-02 13:26:08 +0000126 guild: interaction.guild!.id
pineafan1dc15722022-03-14 21:27:34 +0000127 }
pineafan63fc5e22022-08-04 22:04:10 +0100128 };
129 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +0100130 "warn",
PineaFan100df682023-01-02 13:26:08 +0000131 interaction.guild!.id,
pineafan4edb7762022-06-26 19:21:04 +0100132 (interaction.options.getMember("user") as GuildMember).user,
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 interaction.user,
134 reason
pineafan63fc5e22022-08-04 22:04:10 +0100135 );
pineafan4edb7762022-06-26 19:21:04 +0100136 log(data);
PineaFan100df682023-01-02 13:26:08 +0000137 const failed = !dmSent && notify;
pineafan5d1908e2022-02-28 21:34:47 +0000138 if (!failed) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 await interaction.editReply({
140 embeds: [
141 new EmojiEmbed()
142 .setEmoji("PUNISH.WARN.GREEN")
143 .setTitle("Warn")
144 .setDescription(
145 "The user was warned" +
PineaFan100df682023-01-02 13:26:08 +0000146 (createAppealTicket
147 ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>`
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 : "")
149 )
150 .setStatus("Success")
151 ],
152 components: []
153 });
pineafan5d1908e2022-02-28 21:34:47 +0000154 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +0100155 const canSeeChannel = (interaction.options.getMember("user") as GuildMember)
Skyler Grey75ea9172022-08-06 10:22:23 +0100156 .permissionsIn(interaction.channel as Discord.TextChannel)
PineaFan100df682023-01-02 13:26:08 +0000157 .has("ViewChannel");
Skyler Grey75ea9172022-08-06 10:22:23 +0100158 const m = (await interaction.editReply({
159 embeds: [
160 new EmojiEmbed()
161 .setEmoji("PUNISH.WARN.RED")
162 .setTitle("Warn")
Skyler Grey11236ba2022-08-08 21:13:33 +0100163 .setDescription("The user's DMs are not open\n\nWhat would you like to do?")
Skyler Grey75ea9172022-08-06 10:22:23 +0100164 .setStatus("Danger")
165 ],
166 components: [
PineaFan100df682023-01-02 13:26:08 +0000167 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents(
TheCodedProf21c08592022-09-13 14:14:43 -0400168 new Discord.ButtonBuilder().setCustomId("log").setLabel("Ignore and log").setStyle(ButtonStyle.Secondary),
169 new Discord.ButtonBuilder()
pineafan5d1908e2022-02-28 21:34:47 +0000170 .setCustomId("here")
171 .setLabel("Warn here")
TheCodedProf21c08592022-09-13 14:14:43 -0400172 .setStyle(canSeeChannel ? ButtonStyle.Primary : ButtonStyle.Secondary)
pineafanc1c18792022-08-03 21:41:36 +0100173 .setDisabled(!canSeeChannel),
TheCodedProf21c08592022-09-13 14:14:43 -0400174 new Discord.ButtonBuilder()
pineafanc1c18792022-08-03 21:41:36 +0100175 .setCustomId("ticket")
176 .setLabel("Create ticket")
TheCodedProf21c08592022-09-13 14:14:43 -0400177 .setStyle(canSeeChannel ? ButtonStyle.Primary : ButtonStyle.Secondary)
PineaFan100df682023-01-02 13:26:08 +0000178 .setDisabled(createAppealTicket)
179 )
pineafan02ba0232022-07-24 22:16:15 +0100180 ]
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 })) as Discord.Message;
pineafan5d1908e2022-02-28 21:34:47 +0000182 let component;
183 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 component = await m.awaitMessageComponent({
185 filter: (m) => m.user.id === interaction.user.id,
186 time: 300000
187 });
pineafan5d1908e2022-02-28 21:34:47 +0000188 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 return await interaction.editReply({
190 embeds: [
191 new EmojiEmbed()
192 .setEmoji("PUNISH.WARN.GREEN")
193 .setTitle("Warn")
194 .setDescription("No changes were made")
195 .setStatus("Success")
196 ],
197 components: []
198 });
199 }
200 if (component.customId === "here") {
PineaFan100df682023-01-02 13:26:08 +0000201 await interaction.channel!.send({
Skyler Grey75ea9172022-08-06 10:22:23 +0100202 embeds: [
203 new EmojiEmbed()
204 .setEmoji("PUNISH.WARN.RED")
205 .setTitle("Warn")
Skyler Grey11236ba2022-08-08 21:13:33 +0100206 .setDescription("You have been warned" + (reason ? ` for:\n> ${reason}` : "."))
Skyler Grey75ea9172022-08-06 10:22:23 +0100207 .setStatus("Danger")
208 ],
Skyler Grey11236ba2022-08-08 21:13:33 +0100209 content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`,
Skyler Grey75ea9172022-08-06 10:22:23 +0100210 allowedMentions: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100211 users: [(interaction.options.getMember("user") as GuildMember).id]
Skyler Grey75ea9172022-08-06 10:22:23 +0100212 }
213 });
214 return await interaction.editReply({
215 embeds: [
216 new EmojiEmbed()
217 .setEmoji("PUNISH.WARN.GREEN")
218 .setTitle("Warn")
219 .setDescription(
220 "The user was warned" +
PineaFan100df682023-01-02 13:26:08 +0000221 (createAppealTicket
222 ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>`
Skyler Grey75ea9172022-08-06 10:22:23 +0100223 : "")
224 )
225 .setStatus("Success")
226 ],
227 components: []
228 });
229 } else if (component.customId === "log") {
230 await interaction.editReply({
231 embeds: [
232 new EmojiEmbed()
233 .setEmoji("PUNISH.WARN.GREEN")
234 .setTitle("Warn")
235 .setDescription("The warn was logged")
236 .setStatus("Success")
237 ],
238 components: []
239 });
240 } else if (component.customId === "ticket") {
241 const ticketChannel = await create(
PineaFan100df682023-01-02 13:26:08 +0000242 interaction.guild!,
243 interaction.options.getUser("user")!,
Skyler Grey75ea9172022-08-06 10:22:23 +0100244 interaction.user,
245 reason,
246 "Warn Notification"
247 );
248 if (ticketChannel === null) {
249 return await interaction.editReply({
250 embeds: [
251 new EmojiEmbed()
252 .setEmoji("PUNISH.WARN.RED")
253 .setTitle("Warn")
254 .setDescription("A ticket could not be created")
255 .setStatus("Danger")
256 ],
257 components: []
258 });
259 }
260 await interaction.editReply({
261 embeds: [
262 new EmojiEmbed()
263 .setEmoji("PUNISH.WARN.GREEN")
264 .setTitle("Warn")
Skyler Grey11236ba2022-08-08 21:13:33 +0100265 .setDescription(`A ticket was created in <#${ticketChannel}>`)
Skyler Grey75ea9172022-08-06 10:22:23 +0100266 .setStatus("Success")
267 ],
268 components: []
269 });
270 }
271 }
272 } else {
273 await interaction.editReply({
274 embeds: [
275 new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100276 .setEmoji("PUNISH.WARN.GREEN")
277 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000278 .setDescription("No changes were made")
279 .setStatus("Success")
Skyler Grey75ea9172022-08-06 10:22:23 +0100280 ],
281 components: []
282 });
pineafan8b4b17f2022-02-27 20:42:52 +0000283 }
pineafan63fc5e22022-08-04 22:04:10 +0100284};
pineafan4f164f32022-02-26 22:07:12 +0000285
pineafanbd02b4a2022-08-05 22:01:38 +0100286const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100287 const member = interaction.member as GuildMember;
Skyler Greyad002172022-08-16 18:48:26 +0100288 const apply = interaction.options.getMember("user") as GuildMember | null;
289 if (apply === null) throw new Error("That member is not in the server");
290 const memberPos = member.roles.cache.size ? member.roles.highest.position : 0;
291 const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0;
pineafan8b4b17f2022-02-27 20:42:52 +0000292 // Do not allow warning bots
pineafan3a02ea32022-08-11 21:35:04 +0100293 if (member.user.bot) throw new Error("I cannot warn bots");
pineafan8b4b17f2022-02-27 20:42:52 +0000294 // Allow the owner to warn anyone
pineafan62ce1922022-08-25 20:34:45 +0100295 if (member.id === interaction.guild!.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000296 // Check if the user has moderate_members permission
PineaFan100df682023-01-02 13:26:08 +0000297 if (!member.permissions.has("ModerateMembers"))
pineafan3a02ea32022-08-11 21:35:04 +0100298 throw new Error("You do not have the *Moderate Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000299 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100300 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000301 // Allow warn
pineafan63fc5e22022-08-04 22:04:10 +0100302 return true;
303};
pineafan4f164f32022-02-26 22:07:12 +0000304
Skyler Grey75ea9172022-08-06 10:22:23 +0100305export { command, callback, check };