blob: b007b29542d2a6910246cba05fe341d086878573 [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";
pineafan4edb7762022-06-26 19:21:04 +01008import client from "../../utils/client.js"
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("warn")
13 .setDescription("Warns a user")
pineafan8b4b17f2022-02-27 20:42:52 +000014 .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> => {
PineappleFanb3dd83c2022-06-17 10:53:48 +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 })
pineafan02ba0232022-07-24 22:16:15 +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 ?? "")
40 .send(reason !== null)
41 reason = reason ?? ""
pineafan02ba0232022-07-24 22:16:15 +010042 if (confirmation.cancelled) return
43 if (confirmation.success) break
44 if (confirmation.newReason) reason = confirmation.newReason
45 if (confirmation.components) {
46 notify = confirmation.components.notify.active
47 createAppealTicket = confirmation.components.appeal.active
48 }
pineafan73a7c4a2022-07-24 10:38:04 +010049 }
pineafan377794f2022-04-18 19:01:01 +010050 if (confirmation.success) {
pineafan8b4b17f2022-02-27 20:42:52 +000051 let dmd = false
52 try {
pineafan02ba0232022-07-24 22:16:15 +010053 if (notify) {
54 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" +
pineafan02ba0232022-07-24 22:16:15 +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 ])] : []
pineafan8b4b17f2022-02-27 20:42:52 +000073 })
74 dmd = true
75 }
pineafan02ba0232022-07-24 22:16:15 +010076 } catch {}
pineafan1dc15722022-03-14 21:27:34 +000077 let data = {
78 meta:{
79 type: 'memberWarn',
80 displayName: 'Member warned',
81 calculateType: 'guildMemberPunish',
82 color: NucleusColors.yellow,
83 emoji: 'PUNISH.WARN.YELLOW',
84 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 }
94 }
pineafan4edb7762022-06-26 19:21:04 +010095 try { await client.database.history.create(
96 "warn", interaction.guild.id,
97 (interaction.options.getMember("user") as GuildMember).user,
pineafan73a7c4a2022-07-24 10:38:04 +010098 interaction.user, reason
pineafan4edb7762022-06-26 19:21:04 +010099 )} catch {}
100 log(data);
pineafane23c4ec2022-07-27 21:56:27 +0100101 let 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()
pineafan5d1908e2022-02-28 21:34:47 +0000104 .setEmoji(`PUNISH.WARN.GREEN`)
105 .setTitle(`Warn`)
pineafan02ba0232022-07-24 22:16:15 +0100106 .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")
108 ], components: []})
109 } else {
pineafanc1c18792022-08-03 21:41:36 +0100110 let canSeeChannel = (interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL")
pineafan5d1908e2022-02-28 21:34:47 +0000111 let m = await interaction.editReply({
pineafan4edb7762022-06-26 19:21:04 +0100112 embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000113 .setEmoji(`PUNISH.WARN.RED`)
114 .setTitle(`Warn`)
115 .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 ]
pineafan5d1908e2022-02-28 21:34:47 +0000134 })
135 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()
pineafan5d1908e2022-02-28 21:34:47 +0000140 .setEmoji(`PUNISH.WARN.GREEN`)
141 .setTitle(`Warn`)
142 .setDescription("No changes were made")
143 .setStatus("Success")
144 ], components: []})
145 }
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()
pineafan5d1908e2022-02-28 21:34:47 +0000149 .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]}
157 })
pineafan4edb7762022-06-26 19:21:04 +0100158 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000159 .setEmoji(`PUNISH.WARN.GREEN`)
160 .setTitle(`Warn`)
pineafan4092b862022-05-20 19:27:23 +0100161 .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")
163 ], 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()
pineafan5d1908e2022-02-28 21:34:47 +0000166 .setEmoji(`PUNISH.WARN.GREEN`)
167 .setTitle(`Warn`)
168 .setDescription("The warn was logged")
169 .setStatus("Success")
170 ], components: []})
pineafanc1c18792022-08-03 21:41:36 +0100171 } else if (component.customId === "ticket") {
172 let ticketChannel = await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason, "Warn Notification")
173 if (ticketChannel === null) {
174 return await interaction.editReply({embeds: [new EmojiEmbed()
175 .setEmoji(`PUNISH.WARN.RED`)
176 .setTitle(`Warn`)
177 .setDescription("A ticket could not be created")
178 .setStatus("Danger")
179 ], components: []})
180 }
181 await interaction.editReply({embeds: [new EmojiEmbed()
182 .setEmoji(`PUNISH.WARN.GREEN`)
183 .setTitle(`Warn`)
184 .setDescription(`A ticket was created in <#${ticketChannel}>`)
185 .setStatus("Success")
186 ], 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")
192 .setTitle(`Warn`)
193 .setDescription("No changes were made")
194 .setStatus("Success")
195 ], components: []})
196 }
pineafan4f164f32022-02-26 22:07:12 +0000197}
198
199const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000200 let member = (interaction.member as GuildMember)
201 let me = (interaction.guild.me as GuildMember)
202 let apply = (interaction.options.getMember("user") as GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100203 if (member === null || me === null || apply === null) throw "That member is not in the server"
pineafan5d1908e2022-02-28 21:34:47 +0000204 let memberPos = member.roles ? member.roles.highest.position : 0
205 let mePos = me.roles ? me.roles.highest.position : 0
206 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +0000207 // Do not allow warning bots
pineafan663dc472022-05-10 18:13:47 +0100208 if (member.user.bot) throw "I cannot warn bots"
pineafan8b4b17f2022-02-27 20:42:52 +0000209 // Allow the owner to warn anyone
pineafane23c4ec2022-07-27 21:56:27 +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
pineafan5d1908e2022-02-28 21:34:47 +0000214 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000215 // Allow warn
216 return true
pineafan4f164f32022-02-26 22:07:12 +0000217}
218
pineafan8b4b17f2022-02-27 20:42:52 +0000219export { command, callback, check };