blob: 0648f66eea05a3b0d617e51e826f14650716e2ad [file] [log] [blame]
pineafan02ba0232022-07-24 22:16:15 +01001import Discord, { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
pineafan8b4b17f2022-02-27 20:42:52 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan8b4b17f2022-02-27 20:42:52 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan73a7c4a2022-07-24 10:38:04 +01007import { create, areTicketsEnabled } from "../../actions/createModActionTicket.js";
pineafan63fc5e22022-08-04 22:04:10 +01008import client from "../../utils/client.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")
14 .addUserOption(option => option.setName("user").setDescription("The user to warn").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000015
pineafan6702cef2022-06-13 17:52:37 +010016const callback = async (interaction: CommandInteraction): Promise<any> => {
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
pineafan73a7c4a2022-07-24 10:38:04 +010019 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010020 let notify = true;
21 let createAppealTicket = false;
pineafan73a7c4a2022-07-24 10:38:04 +010022 let confirmation;
23 while (true) {
24 confirmation = await new confirmationMessage(interaction)
25 .setEmoji("PUNISH.WARN.RED")
26 .setTitle("Warn")
27 .setDescription(keyValueList({
28 "user": renderUser(interaction.options.getUser("user")),
29 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
30 })
pineafan63fc5e22022-08-04 22:04:10 +010031 + `The user **will${notify ? "" : " not"}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010032 + `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
33 .setColor("Danger")
34 .addCustomBoolean(
pineafan02ba0232022-07-24 22:16:15 +010035 "appeal", "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)),
pineafan73a7c4a2022-07-24 10:38:04 +010036 async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason),
pineafan02ba0232022-07-24 22:16:15 +010037 "An appeal ticket will be created when Confirm is clicked", "CONTROL.TICKET", createAppealTicket)
38 .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify)
pineafan73a7c4a2022-07-24 10:38:04 +010039 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010040 .send(reason !== null);
41 reason = reason ?? "";
42 if (confirmation.cancelled) return;
43 if (confirmation.success) break;
44 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010045 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010046 notify = confirmation.components.notify.active;
47 createAppealTicket = confirmation.components.appeal.active;
pineafan02ba0232022-07-24 22:16:15 +010048 }
pineafan73a7c4a2022-07-24 10:38:04 +010049 }
pineafan377794f2022-04-18 19:01:01 +010050 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010051 let dmd = false;
pineafan8b4b17f2022-02-27 20:42:52 +000052 try {
pineafan02ba0232022-07-24 22:16:15 +010053 if (notify) {
pineafan63fc5e22022-08-04 22:04:10 +010054 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000055 await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010056 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000057 .setEmoji("PUNISH.WARN.RED")
58 .setTitle("Warned")
59 .setDescription(`You have been warned in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010060 (reason ? ` for:\n> ${reason}` : ".") + "\n\n" +
pineafan63fc5e22022-08-04 22:04:10 +010061 (confirmation.components.appeal.response ? `You can appeal this here ticket: <#${confirmation.components.appeal.response}>` : ""))
pineafan8b4b17f2022-02-27 20:42:52 +000062 .setStatus("Danger")
pineafan02ba0232022-07-24 22:16:15 +010063 .setFooter({
64 text: config.moderation.warn.text ? "The button below is set by the server admins. Do not enter any passwords or other account details on the linked site." : "",
65 iconURL: "https://cdn.discordapp.com/emojis/952295894370369587.webp?size=128&quality=lossless"
66 })
67 ],
68 components: config.moderation.warn.text ? [new MessageActionRow().addComponents([new MessageButton()
69 .setStyle("LINK")
70 .setLabel(config.moderation.warn.text)
71 .setURL(config.moderation.warn.link)
72 ])] : []
pineafan63fc5e22022-08-04 22:04:10 +010073 });
74 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000075 }
pineafan63fc5e22022-08-04 22:04:10 +010076 } catch { dmd = false; }
77 const data = {
pineafan1dc15722022-03-14 21:27:34 +000078 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010079 type: "memberWarn",
80 displayName: "Member warned",
81 calculateType: "guildMemberPunish",
pineafan1dc15722022-03-14 21:27:34 +000082 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +010083 emoji: "PUNISH.WARN.YELLOW",
pineafan1dc15722022-03-14 21:27:34 +000084 timestamp: new Date().getTime()
85 },
86 list: {
pineafan377794f2022-04-18 19:01:01 +010087 user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)),
88 warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
pineafan73a7c4a2022-07-24 10:38:04 +010089 reason: reason ? `\n> ${reason}` : "No reason provided"
pineafan1dc15722022-03-14 21:27:34 +000090 },
91 hidden: {
92 guild: interaction.guild.id
93 }
pineafan63fc5e22022-08-04 22:04:10 +010094 };
95 await client.database.history.create(
pineafan4edb7762022-06-26 19:21:04 +010096 "warn", interaction.guild.id,
97 (interaction.options.getMember("user") as GuildMember).user,
pineafan73a7c4a2022-07-24 10:38:04 +010098 interaction.user, reason
pineafan63fc5e22022-08-04 22:04:10 +010099 );
pineafan4edb7762022-06-26 19:21:04 +0100100 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100101 const failed = (dmd === false && notify);
pineafan5d1908e2022-02-28 21:34:47 +0000102 if (!failed) {
pineafan4edb7762022-06-26 19:21:04 +0100103 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100104 .setEmoji("PUNISH.WARN.GREEN")
105 .setTitle("Warn")
106 .setDescription("The user was warned" + (confirmation.components.appeal.response ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` : ""))
pineafan5d1908e2022-02-28 21:34:47 +0000107 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100108 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000109 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100110 const canSeeChannel = (interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL");
111 const m = await interaction.editReply({
pineafan4edb7762022-06-26 19:21:04 +0100112 embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100113 .setEmoji("PUNISH.WARN.RED")
114 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000115 .setDescription("The user's DMs are not open\n\nWhat would you like to do?")
116 .setStatus("Danger")
117 ], components: [
118 new MessageActionRow().addComponents([
119 new Discord.MessageButton()
120 .setCustomId("log")
121 .setLabel("Ignore and log")
122 .setStyle("SECONDARY"),
123 new Discord.MessageButton()
124 .setCustomId("here")
125 .setLabel("Warn here")
pineafanc1c18792022-08-03 21:41:36 +0100126 .setStyle(canSeeChannel ? "PRIMARY" : "SECONDARY")
127 .setDisabled(!canSeeChannel),
128 new Discord.MessageButton()
129 .setCustomId("ticket")
130 .setLabel("Create ticket")
131 .setStyle(canSeeChannel ? "SECONDARY" : "PRIMARY")
pineafan5d1908e2022-02-28 21:34:47 +0000132 ])
pineafan02ba0232022-07-24 22:16:15 +0100133 ]
pineafan63fc5e22022-08-04 22:04:10 +0100134 });
pineafan5d1908e2022-02-28 21:34:47 +0000135 let component;
136 try {
pineafanc6158ab2022-06-17 16:34:07 +0100137 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000});
pineafan5d1908e2022-02-28 21:34:47 +0000138 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +0100139 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100140 .setEmoji("PUNISH.WARN.GREEN")
141 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000142 .setDescription("No changes were made")
143 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100144 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000145 }
pineafane23c4ec2022-07-27 21:56:27 +0100146 if ( component.customId === "here" ) {
pineafan5d1908e2022-02-28 21:34:47 +0000147 await interaction.channel.send({
pineafan4edb7762022-06-26 19:21:04 +0100148 embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100149 .setEmoji("PUNISH.WARN.RED")
150 .setTitle("Warn")
151 .setDescription("You have been warned" +
pineafan73a7c4a2022-07-24 10:38:04 +0100152 (reason ? ` for:\n> ${reason}` : "."))
pineafan5d1908e2022-02-28 21:34:47 +0000153 .setStatus("Danger")
154 ],
155 content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`,
156 allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]}
pineafan63fc5e22022-08-04 22:04:10 +0100157 });
pineafan4edb7762022-06-26 19:21:04 +0100158 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100159 .setEmoji("PUNISH.WARN.GREEN")
160 .setTitle("Warn")
161 .setDescription("The user was warned" + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : ""))
pineafan5d1908e2022-02-28 21:34:47 +0000162 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100163 ], components: []});
pineafanc1c18792022-08-03 21:41:36 +0100164 } else if (component.customId === "log") {
pineafan4edb7762022-06-26 19:21:04 +0100165 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100166 .setEmoji("PUNISH.WARN.GREEN")
167 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000168 .setDescription("The warn was logged")
169 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100170 ], components: []});
pineafanc1c18792022-08-03 21:41:36 +0100171 } else if (component.customId === "ticket") {
pineafan63fc5e22022-08-04 22:04:10 +0100172 const ticketChannel = await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason, "Warn Notification");
pineafanc1c18792022-08-03 21:41:36 +0100173 if (ticketChannel === null) {
174 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100175 .setEmoji("PUNISH.WARN.RED")
176 .setTitle("Warn")
pineafanc1c18792022-08-03 21:41:36 +0100177 .setDescription("A ticket could not be created")
178 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +0100179 ], components: []});
pineafanc1c18792022-08-03 21:41:36 +0100180 }
181 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100182 .setEmoji("PUNISH.WARN.GREEN")
183 .setTitle("Warn")
pineafanc1c18792022-08-03 21:41:36 +0100184 .setDescription(`A ticket was created in <#${ticketChannel}>`)
185 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100186 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000187 }
188 }
pineafan8b4b17f2022-02-27 20:42:52 +0000189 } else {
pineafan4edb7762022-06-26 19:21:04 +0100190 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000191 .setEmoji("PUNISH.WARN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100192 .setTitle("Warn")
pineafan8b4b17f2022-02-27 20:42:52 +0000193 .setDescription("No changes were made")
194 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100195 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +0000196 }
pineafan63fc5e22022-08-04 22:04:10 +0100197};
pineafan4f164f32022-02-26 22:07:12 +0000198
199const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100200 const member = (interaction.member as GuildMember);
201 const me = (interaction.guild.me as GuildMember);
202 const apply = (interaction.options.getMember("user") as GuildMember);
203 if (member === null || me === null || apply === null) throw "That member is not in the server";
204 const memberPos = member.roles ? member.roles.highest.position : 0;
205 const mePos = me.roles ? me.roles.highest.position : 0;
206 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafan8b4b17f2022-02-27 20:42:52 +0000207 // Do not allow warning bots
pineafan63fc5e22022-08-04 22:04:10 +0100208 if (member.user.bot) throw "I cannot warn bots";
pineafan8b4b17f2022-02-27 20:42:52 +0000209 // Allow the owner to warn anyone
pineafan63fc5e22022-08-04 22:04:10 +0100210 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000211 // Check if the user has moderate_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100212 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000213 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100214 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000215 // Allow warn
pineafan63fc5e22022-08-04 22:04:10 +0100216 return true;
217};
pineafan4f164f32022-02-26 22:07:12 +0000218
pineafan8b4b17f2022-02-27 20:42:52 +0000219export { command, callback, check };