blob: 34ad3c3840822172ac749351302e3ce2f517a504 [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";
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";
pineafan73a7c4a2022-07-24 10:38:04 +01006import { create, areTicketsEnabled } from "../../actions/createModActionTicket.js";
pineafan63fc5e22022-08-04 22:04:10 +01007import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("warn")
12 .setDescription("Warns a user")
13 .addUserOption(option => option.setName("user").setDescription("The user to warn").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafanbd02b4a2022-08-05 22:01:38 +010015const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010016 const { log, NucleusColors, renderUser, entry } = client.logger;
pineafan8b4b17f2022-02-27 20:42:52 +000017 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010018 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010019 let notify = true;
20 let createAppealTicket = false;
pineafan73a7c4a2022-07-24 10:38:04 +010021 let confirmation;
22 while (true) {
23 confirmation = await new confirmationMessage(interaction)
24 .setEmoji("PUNISH.WARN.RED")
25 .setTitle("Warn")
26 .setDescription(keyValueList({
27 "user": renderUser(interaction.options.getUser("user")),
28 "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*"
29 })
pineafan63fc5e22022-08-04 22:04:10 +010030 + `The user **will${notify ? "" : " not"}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010031 + `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
32 .setColor("Danger")
33 .addCustomBoolean(
pineafan02ba0232022-07-24 22:16:15 +010034 "appeal", "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)),
pineafan73a7c4a2022-07-24 10:38:04 +010035 async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason),
pineafan02ba0232022-07-24 22:16:15 +010036 "An appeal ticket will be created when Confirm is clicked", "CONTROL.TICKET", createAppealTicket)
37 .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify)
pineafan73a7c4a2022-07-24 10:38:04 +010038 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010039 .send(reason !== null);
40 reason = reason ?? "";
41 if (confirmation.cancelled) return;
42 if (confirmation.success) break;
43 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010044 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010045 notify = confirmation.components.notify.active;
46 createAppealTicket = confirmation.components.appeal.active;
pineafan02ba0232022-07-24 22:16:15 +010047 }
pineafan73a7c4a2022-07-24 10:38:04 +010048 }
pineafan377794f2022-04-18 19:01:01 +010049 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010050 let dmd = false;
pineafan8b4b17f2022-02-27 20:42:52 +000051 try {
pineafan02ba0232022-07-24 22:16:15 +010052 if (notify) {
pineafan63fc5e22022-08-04 22:04:10 +010053 const config = await client.database.guilds.read(interaction.guild.id);
pineafan8b4b17f2022-02-27 20:42:52 +000054 await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010055 embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +000056 .setEmoji("PUNISH.WARN.RED")
57 .setTitle("Warned")
58 .setDescription(`You have been warned in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010059 (reason ? ` for:\n> ${reason}` : ".") + "\n\n" +
pineafan63fc5e22022-08-04 22:04:10 +010060 (confirmation.components.appeal.response ? `You can appeal this here ticket: <#${confirmation.components.appeal.response}>` : ""))
pineafan8b4b17f2022-02-27 20:42:52 +000061 .setStatus("Danger")
pineafan02ba0232022-07-24 22:16:15 +010062 .setFooter({
63 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." : "",
64 iconURL: "https://cdn.discordapp.com/emojis/952295894370369587.webp?size=128&quality=lossless"
65 })
66 ],
67 components: config.moderation.warn.text ? [new MessageActionRow().addComponents([new MessageButton()
68 .setStyle("LINK")
69 .setLabel(config.moderation.warn.text)
70 .setURL(config.moderation.warn.link)
71 ])] : []
pineafan63fc5e22022-08-04 22:04:10 +010072 });
73 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +000074 }
pineafan63fc5e22022-08-04 22:04:10 +010075 } catch { dmd = false; }
76 const data = {
pineafan1dc15722022-03-14 21:27:34 +000077 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010078 type: "memberWarn",
79 displayName: "Member warned",
80 calculateType: "guildMemberPunish",
pineafan1dc15722022-03-14 21:27:34 +000081 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +010082 emoji: "PUNISH.WARN.YELLOW",
pineafan1dc15722022-03-14 21:27:34 +000083 timestamp: new Date().getTime()
84 },
85 list: {
pineafan377794f2022-04-18 19:01:01 +010086 user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)),
87 warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
pineafan73a7c4a2022-07-24 10:38:04 +010088 reason: reason ? `\n> ${reason}` : "No reason provided"
pineafan1dc15722022-03-14 21:27:34 +000089 },
90 hidden: {
91 guild: interaction.guild.id
92 }
pineafan63fc5e22022-08-04 22:04:10 +010093 };
94 await client.database.history.create(
pineafan4edb7762022-06-26 19:21:04 +010095 "warn", interaction.guild.id,
96 (interaction.options.getMember("user") as GuildMember).user,
pineafan73a7c4a2022-07-24 10:38:04 +010097 interaction.user, reason
pineafan63fc5e22022-08-04 22:04:10 +010098 );
pineafan4edb7762022-06-26 19:21:04 +010099 log(data);
pineafan63fc5e22022-08-04 22:04:10 +0100100 const failed = (dmd === false && notify);
pineafan5d1908e2022-02-28 21:34:47 +0000101 if (!failed) {
pineafan4edb7762022-06-26 19:21:04 +0100102 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100103 .setEmoji("PUNISH.WARN.GREEN")
104 .setTitle("Warn")
105 .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 +0000106 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100107 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000108 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100109 const canSeeChannel = (interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL");
110 const m = await interaction.editReply({
pineafan4edb7762022-06-26 19:21:04 +0100111 embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100112 .setEmoji("PUNISH.WARN.RED")
113 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000114 .setDescription("The user's DMs are not open\n\nWhat would you like to do?")
115 .setStatus("Danger")
116 ], components: [
117 new MessageActionRow().addComponents([
118 new Discord.MessageButton()
119 .setCustomId("log")
120 .setLabel("Ignore and log")
121 .setStyle("SECONDARY"),
122 new Discord.MessageButton()
123 .setCustomId("here")
124 .setLabel("Warn here")
pineafanc1c18792022-08-03 21:41:36 +0100125 .setStyle(canSeeChannel ? "PRIMARY" : "SECONDARY")
126 .setDisabled(!canSeeChannel),
127 new Discord.MessageButton()
128 .setCustomId("ticket")
129 .setLabel("Create ticket")
130 .setStyle(canSeeChannel ? "SECONDARY" : "PRIMARY")
pineafan5d1908e2022-02-28 21:34:47 +0000131 ])
pineafan02ba0232022-07-24 22:16:15 +0100132 ]
pineafanbd02b4a2022-08-05 22:01:38 +0100133 }) as Discord.Message;
pineafan5d1908e2022-02-28 21:34:47 +0000134 let component;
135 try {
pineafanbd02b4a2022-08-05 22:01:38 +0100136 component = await m.awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000});
pineafan5d1908e2022-02-28 21:34:47 +0000137 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +0100138 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100139 .setEmoji("PUNISH.WARN.GREEN")
140 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000141 .setDescription("No changes were made")
142 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100143 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000144 }
pineafane23c4ec2022-07-27 21:56:27 +0100145 if ( component.customId === "here" ) {
pineafan5d1908e2022-02-28 21:34:47 +0000146 await interaction.channel.send({
pineafan4edb7762022-06-26 19:21:04 +0100147 embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100148 .setEmoji("PUNISH.WARN.RED")
149 .setTitle("Warn")
150 .setDescription("You have been warned" +
pineafan73a7c4a2022-07-24 10:38:04 +0100151 (reason ? ` for:\n> ${reason}` : "."))
pineafan5d1908e2022-02-28 21:34:47 +0000152 .setStatus("Danger")
153 ],
154 content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`,
155 allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]}
pineafan63fc5e22022-08-04 22:04:10 +0100156 });
pineafan4edb7762022-06-26 19:21:04 +0100157 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100158 .setEmoji("PUNISH.WARN.GREEN")
159 .setTitle("Warn")
160 .setDescription("The user was warned" + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : ""))
pineafan5d1908e2022-02-28 21:34:47 +0000161 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100162 ], components: []});
pineafanc1c18792022-08-03 21:41:36 +0100163 } else if (component.customId === "log") {
pineafan4edb7762022-06-26 19:21:04 +0100164 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100165 .setEmoji("PUNISH.WARN.GREEN")
166 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000167 .setDescription("The warn was logged")
168 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100169 ], components: []});
pineafanc1c18792022-08-03 21:41:36 +0100170 } else if (component.customId === "ticket") {
pineafan63fc5e22022-08-04 22:04:10 +0100171 const ticketChannel = await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason, "Warn Notification");
pineafanc1c18792022-08-03 21:41:36 +0100172 if (ticketChannel === null) {
173 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100174 .setEmoji("PUNISH.WARN.RED")
175 .setTitle("Warn")
pineafanc1c18792022-08-03 21:41:36 +0100176 .setDescription("A ticket could not be created")
177 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +0100178 ], components: []});
pineafanc1c18792022-08-03 21:41:36 +0100179 }
180 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100181 .setEmoji("PUNISH.WARN.GREEN")
182 .setTitle("Warn")
pineafanc1c18792022-08-03 21:41:36 +0100183 .setDescription(`A ticket was created in <#${ticketChannel}>`)
184 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100185 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000186 }
187 }
pineafan8b4b17f2022-02-27 20:42:52 +0000188 } else {
pineafan4edb7762022-06-26 19:21:04 +0100189 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000190 .setEmoji("PUNISH.WARN.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100191 .setTitle("Warn")
pineafan8b4b17f2022-02-27 20:42:52 +0000192 .setDescription("No changes were made")
193 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100194 ], components: []});
pineafan8b4b17f2022-02-27 20:42:52 +0000195 }
pineafan63fc5e22022-08-04 22:04:10 +0100196};
pineafan4f164f32022-02-26 22:07:12 +0000197
pineafanbd02b4a2022-08-05 22:01:38 +0100198const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +0100199 const member = (interaction.member as GuildMember);
200 const me = (interaction.guild.me as GuildMember);
201 const apply = (interaction.options.getMember("user") as GuildMember);
202 if (member === null || me === null || apply === null) throw "That member is not in the server";
203 const memberPos = member.roles ? member.roles.highest.position : 0;
pineafan63fc5e22022-08-04 22:04:10 +0100204 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafan8b4b17f2022-02-27 20:42:52 +0000205 // Do not allow warning bots
pineafan63fc5e22022-08-04 22:04:10 +0100206 if (member.user.bot) throw "I cannot warn bots";
pineafan8b4b17f2022-02-27 20:42:52 +0000207 // Allow the owner to warn anyone
pineafan63fc5e22022-08-04 22:04:10 +0100208 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000209 // Check if the user has moderate_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100210 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000211 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100212 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan8b4b17f2022-02-27 20:42:52 +0000213 // Allow warn
pineafan63fc5e22022-08-04 22:04:10 +0100214 return true;
215};
pineafan4f164f32022-02-26 22:07:12 +0000216
pineafan8b4b17f2022-02-27 20:42:52 +0000217export { command, callback, check };