blob: 50480fa5afa9e107b4e7df2d08c5f55edaea5506 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +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";
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";
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")
Skyler Grey11236ba2022-08-08 21:13:33 +010013 .addUserOption((option) => option.setName("user").setDescription("The user to warn").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan3a02ea32022-08-11 21:35:04 +010015const callback = async (interaction: CommandInteraction): Promise<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;
Skyler Greyad002172022-08-16 18:48:26 +010022 let timedOut = false;
23 let success = false;
24 while (!timedOut && !success) {
pineafan73a7c4a2022-07-24 10:38:04 +010025 confirmation = await new confirmationMessage(interaction)
26 .setEmoji("PUNISH.WARN.RED")
27 .setTitle("Warn")
Skyler Grey75ea9172022-08-06 10:22:23 +010028 .setDescription(
29 keyValueList({
30 user: renderUser(interaction.options.getUser("user")),
Skyler Greyad002172022-08-16 18:48:26 +010031 reason: reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*"
Skyler Grey75ea9172022-08-06 10:22:23 +010032 }) +
33 `The user **will${notify ? "" : " not"}** be notified\n\n` +
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",
40 !(await areTicketsEnabled(interaction.guild.id)),
41 async () =>
Skyler Grey11236ba2022-08-08 21:13:33 +010042 await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason),
Skyler Grey75ea9172022-08-06 10:22:23 +010043 "An appeal ticket will be created when Confirm is clicked",
44 "CONTROL.TICKET",
45 createAppealTicket
46 )
47 .addCustomBoolean(
48 "notify",
49 "Notify user",
50 false,
51 null,
52 null,
53 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
54 notify
55 )
pineafan73a7c4a2022-07-24 10:38:04 +010056 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010057 .send(reason !== null);
58 reason = reason ?? "";
Skyler Greyad002172022-08-16 18:48:26 +010059 if (confirmation.cancelled) timedOut = true;
60 else if (confirmation.success) success = true;
61 else if (confirmation.newReason) reason = confirmation.newReason;
62 else if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010063 notify = confirmation.components.notify.active;
64 createAppealTicket = confirmation.components.appeal.active;
pineafan02ba0232022-07-24 22:16:15 +010065 }
pineafan73a7c4a2022-07-24 10:38:04 +010066 }
Skyler Greyad002172022-08-16 18:48:26 +010067 if (timedOut) return;
pineafan377794f2022-04-18 19:01:01 +010068 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010069 let dmd = false;
pineafan8b4b17f2022-02-27 20:42:52 +000070 try {
pineafan02ba0232022-07-24 22:16:15 +010071 if (notify) {
Skyler Grey11236ba2022-08-08 21:13:33 +010072 const config = await client.database.guilds.read(interaction.guild.id);
73 await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010074 embeds: [
75 new EmojiEmbed()
76 .setEmoji("PUNISH.WARN.RED")
77 .setTitle("Warned")
78 .setDescription(
79 `You have been warned in ${interaction.guild.name}` +
80 (reason ? ` for:\n> ${reason}` : ".") +
81 "\n\n" +
82 (confirmation.components.appeal.response
83 ? `You can appeal this here ticket: <#${confirmation.components.appeal.response}>`
84 : "")
85 )
86 .setStatus("Danger")
87 .setFooter({
88 text: config.moderation.warn.text
89 ? "The button below is set by the server admins. Do not enter any passwords or other account details on the linked site."
90 : "",
91 iconURL:
92 "https://cdn.discordapp.com/emojis/952295894370369587.webp?size=128&quality=lossless"
93 })
pineafan02ba0232022-07-24 22:16:15 +010094 ],
Skyler Grey75ea9172022-08-06 10:22:23 +010095 components: config.moderation.warn.text
96 ? [
97 new MessageActionRow().addComponents([
98 new MessageButton()
99 .setStyle("LINK")
100 .setLabel(config.moderation.warn.text)
101 .setURL(config.moderation.warn.link)
102 ])
103 ]
104 : []
pineafan63fc5e22022-08-04 22:04:10 +0100105 });
106 dmd = true;
pineafan8b4b17f2022-02-27 20:42:52 +0000107 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 } catch {
109 dmd = false;
110 }
pineafan63fc5e22022-08-04 22:04:10 +0100111 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100113 type: "memberWarn",
114 displayName: "Member warned",
115 calculateType: "guildMemberPunish",
pineafan1dc15722022-03-14 21:27:34 +0000116 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100117 emoji: "PUNISH.WARN.YELLOW",
pineafan1dc15722022-03-14 21:27:34 +0000118 timestamp: new Date().getTime()
119 },
120 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 user: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +0100122 (interaction.options.getMember("user") as GuildMember).user.id,
123 renderUser((interaction.options.getMember("user") as GuildMember).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100125 warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
pineafan73a7c4a2022-07-24 10:38:04 +0100126 reason: reason ? `\n> ${reason}` : "No reason provided"
pineafan1dc15722022-03-14 21:27:34 +0000127 },
128 hidden: {
129 guild: interaction.guild.id
130 }
pineafan63fc5e22022-08-04 22:04:10 +0100131 };
132 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 "warn",
134 interaction.guild.id,
pineafan4edb7762022-06-26 19:21:04 +0100135 (interaction.options.getMember("user") as GuildMember).user,
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 interaction.user,
137 reason
pineafan63fc5e22022-08-04 22:04:10 +0100138 );
pineafan4edb7762022-06-26 19:21:04 +0100139 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 const failed = !dmd && notify;
pineafan5d1908e2022-02-28 21:34:47 +0000141 if (!failed) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 await interaction.editReply({
143 embeds: [
144 new EmojiEmbed()
145 .setEmoji("PUNISH.WARN.GREEN")
146 .setTitle("Warn")
147 .setDescription(
148 "The user was warned" +
149 (confirmation.components.appeal.response
150 ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>`
151 : "")
152 )
153 .setStatus("Success")
154 ],
155 components: []
156 });
pineafan5d1908e2022-02-28 21:34:47 +0000157 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +0100158 const canSeeChannel = (interaction.options.getMember("user") as GuildMember)
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 .permissionsIn(interaction.channel as Discord.TextChannel)
160 .has("VIEW_CHANNEL");
161 const m = (await interaction.editReply({
162 embeds: [
163 new EmojiEmbed()
164 .setEmoji("PUNISH.WARN.RED")
165 .setTitle("Warn")
Skyler Grey11236ba2022-08-08 21:13:33 +0100166 .setDescription("The user's DMs are not open\n\nWhat would you like to do?")
Skyler Grey75ea9172022-08-06 10:22:23 +0100167 .setStatus("Danger")
168 ],
169 components: [
pineafan5d1908e2022-02-28 21:34:47 +0000170 new MessageActionRow().addComponents([
Skyler Grey11236ba2022-08-08 21:13:33 +0100171 new Discord.MessageButton().setCustomId("log").setLabel("Ignore and log").setStyle("SECONDARY"),
pineafan5d1908e2022-02-28 21:34:47 +0000172 new Discord.MessageButton()
173 .setCustomId("here")
174 .setLabel("Warn here")
pineafanc1c18792022-08-03 21:41:36 +0100175 .setStyle(canSeeChannel ? "PRIMARY" : "SECONDARY")
176 .setDisabled(!canSeeChannel),
177 new Discord.MessageButton()
178 .setCustomId("ticket")
179 .setLabel("Create ticket")
180 .setStyle(canSeeChannel ? "SECONDARY" : "PRIMARY")
pineafan5d1908e2022-02-28 21:34:47 +0000181 ])
pineafan02ba0232022-07-24 22:16:15 +0100182 ]
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 })) as Discord.Message;
pineafan5d1908e2022-02-28 21:34:47 +0000184 let component;
185 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 component = await m.awaitMessageComponent({
187 filter: (m) => m.user.id === interaction.user.id,
188 time: 300000
189 });
pineafan5d1908e2022-02-28 21:34:47 +0000190 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 return await interaction.editReply({
192 embeds: [
193 new EmojiEmbed()
194 .setEmoji("PUNISH.WARN.GREEN")
195 .setTitle("Warn")
196 .setDescription("No changes were made")
197 .setStatus("Success")
198 ],
199 components: []
200 });
201 }
202 if (component.customId === "here") {
203 await interaction.channel.send({
204 embeds: [
205 new EmojiEmbed()
206 .setEmoji("PUNISH.WARN.RED")
207 .setTitle("Warn")
Skyler Grey11236ba2022-08-08 21:13:33 +0100208 .setDescription("You have been warned" + (reason ? ` for:\n> ${reason}` : "."))
Skyler Grey75ea9172022-08-06 10:22:23 +0100209 .setStatus("Danger")
210 ],
Skyler Grey11236ba2022-08-08 21:13:33 +0100211 content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`,
Skyler Grey75ea9172022-08-06 10:22:23 +0100212 allowedMentions: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100213 users: [(interaction.options.getMember("user") as GuildMember).id]
Skyler Grey75ea9172022-08-06 10:22:23 +0100214 }
215 });
216 return await interaction.editReply({
217 embeds: [
218 new EmojiEmbed()
219 .setEmoji("PUNISH.WARN.GREEN")
220 .setTitle("Warn")
221 .setDescription(
222 "The user was warned" +
223 (confirmation.response
224 ? ` and an appeal ticket was opened in <#${confirmation.response}>`
225 : "")
226 )
227 .setStatus("Success")
228 ],
229 components: []
230 });
231 } else if (component.customId === "log") {
232 await interaction.editReply({
233 embeds: [
234 new EmojiEmbed()
235 .setEmoji("PUNISH.WARN.GREEN")
236 .setTitle("Warn")
237 .setDescription("The warn was logged")
238 .setStatus("Success")
239 ],
240 components: []
241 });
242 } else if (component.customId === "ticket") {
243 const ticketChannel = await create(
244 interaction.guild,
245 interaction.options.getUser("user"),
246 interaction.user,
247 reason,
248 "Warn Notification"
249 );
250 if (ticketChannel === null) {
251 return await interaction.editReply({
252 embeds: [
253 new EmojiEmbed()
254 .setEmoji("PUNISH.WARN.RED")
255 .setTitle("Warn")
256 .setDescription("A ticket could not be created")
257 .setStatus("Danger")
258 ],
259 components: []
260 });
261 }
262 await interaction.editReply({
263 embeds: [
264 new EmojiEmbed()
265 .setEmoji("PUNISH.WARN.GREEN")
266 .setTitle("Warn")
Skyler Grey11236ba2022-08-08 21:13:33 +0100267 .setDescription(`A ticket was created in <#${ticketChannel}>`)
Skyler Grey75ea9172022-08-06 10:22:23 +0100268 .setStatus("Success")
269 ],
270 components: []
271 });
272 }
273 }
274 } else {
275 await interaction.editReply({
276 embeds: [
277 new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +0100278 .setEmoji("PUNISH.WARN.GREEN")
279 .setTitle("Warn")
pineafan5d1908e2022-02-28 21:34:47 +0000280 .setDescription("No changes were made")
281 .setStatus("Success")
Skyler Grey75ea9172022-08-06 10:22:23 +0100282 ],
283 components: []
284 });
pineafan8b4b17f2022-02-27 20:42:52 +0000285 }
pineafan63fc5e22022-08-04 22:04:10 +0100286};
pineafan4f164f32022-02-26 22:07:12 +0000287
pineafanbd02b4a2022-08-05 22:01:38 +0100288const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100289 const member = interaction.member as GuildMember;
Skyler Greyad002172022-08-16 18:48:26 +0100290 const apply = interaction.options.getMember("user") as GuildMember | null;
291 if (apply === null) throw new Error("That member is not in the server");
292 const memberPos = member.roles.cache.size ? member.roles.highest.position : 0;
293 const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0;
pineafan8b4b17f2022-02-27 20:42:52 +0000294 // Do not allow warning bots
pineafan3a02ea32022-08-11 21:35:04 +0100295 if (member.user.bot) throw new Error("I cannot warn bots");
pineafan8b4b17f2022-02-27 20:42:52 +0000296 // Allow the owner to warn anyone
pineafan63fc5e22022-08-04 22:04:10 +0100297 if (member.id === interaction.guild.ownerId) return true;
pineafan8b4b17f2022-02-27 20:42:52 +0000298 // Check if the user has moderate_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100299 if (!member.permissions.has("MODERATE_MEMBERS"))
300 throw new Error("You do not have the *Moderate Members* permission");
pineafan8b4b17f2022-02-27 20:42:52 +0000301 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100302 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan8b4b17f2022-02-27 20:42:52 +0000303 // Allow warn
pineafan63fc5e22022-08-04 22:04:10 +0100304 return true;
305};
pineafan4f164f32022-02-26 22:07:12 +0000306
Skyler Grey75ea9172022-08-06 10:22:23 +0100307export { command, callback, check };