blob: 5a4291181cdb99e355216c1a24d4319e6062c7d9 [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 {
110 let m = await interaction.editReply({
pineafan4edb7762022-06-26 19:21:04 +0100111 embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000112 .setEmoji(`PUNISH.WARN.RED`)
113 .setTitle(`Warn`)
114 .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")
125 .setStyle("SECONDARY")
126 .setDisabled((interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL") === false),
127 ])
pineafan02ba0232022-07-24 22:16:15 +0100128 ]
pineafan5d1908e2022-02-28 21:34:47 +0000129 })
130 let component;
131 try {
pineafanc6158ab2022-06-17 16:34:07 +0100132 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000});
pineafan5d1908e2022-02-28 21:34:47 +0000133 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +0100134 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000135 .setEmoji(`PUNISH.WARN.GREEN`)
136 .setTitle(`Warn`)
137 .setDescription("No changes were made")
138 .setStatus("Success")
139 ], components: []})
140 }
pineafane23c4ec2022-07-27 21:56:27 +0100141 if ( component.customId === "here" ) {
pineafan5d1908e2022-02-28 21:34:47 +0000142 await interaction.channel.send({
pineafan4edb7762022-06-26 19:21:04 +0100143 embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000144 .setEmoji(`PUNISH.WARN.RED`)
145 .setTitle(`Warn`)
146 .setDescription(`You have been warned` +
pineafan73a7c4a2022-07-24 10:38:04 +0100147 (reason ? ` for:\n> ${reason}` : "."))
pineafan5d1908e2022-02-28 21:34:47 +0000148 .setStatus("Danger")
149 ],
150 content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`,
151 allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]}
152 })
pineafan4edb7762022-06-26 19:21:04 +0100153 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000154 .setEmoji(`PUNISH.WARN.GREEN`)
155 .setTitle(`Warn`)
pineafan4092b862022-05-20 19:27:23 +0100156 .setDescription("The user was warned" + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : ``))
pineafan5d1908e2022-02-28 21:34:47 +0000157 .setStatus("Success")
158 ], components: []})
159 } else {
pineafan4edb7762022-06-26 19:21:04 +0100160 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000161 .setEmoji(`PUNISH.WARN.GREEN`)
162 .setTitle(`Warn`)
163 .setDescription("The warn was logged")
164 .setStatus("Success")
165 ], components: []})
166 }
167 }
pineafan8b4b17f2022-02-27 20:42:52 +0000168 } else {
pineafan4edb7762022-06-26 19:21:04 +0100169 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan8b4b17f2022-02-27 20:42:52 +0000170 .setEmoji("PUNISH.WARN.GREEN")
171 .setTitle(`Warn`)
172 .setDescription("No changes were made")
173 .setStatus("Success")
174 ], components: []})
175 }
pineafan4f164f32022-02-26 22:07:12 +0000176}
177
178const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000179 let member = (interaction.member as GuildMember)
180 let me = (interaction.guild.me as GuildMember)
181 let apply = (interaction.options.getMember("user") as GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100182 if (member === null || me === null || apply === null) throw "That member is not in the server"
pineafan5d1908e2022-02-28 21:34:47 +0000183 let memberPos = member.roles ? member.roles.highest.position : 0
184 let mePos = me.roles ? me.roles.highest.position : 0
185 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +0000186 // Do not allow warning bots
pineafan663dc472022-05-10 18:13:47 +0100187 if (member.user.bot) throw "I cannot warn bots"
pineafan8b4b17f2022-02-27 20:42:52 +0000188 // Allow the owner to warn anyone
pineafane23c4ec2022-07-27 21:56:27 +0100189 if (member.id === interaction.guild.ownerId) return true
pineafan8b4b17f2022-02-27 20:42:52 +0000190 // Check if the user has moderate_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100191 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission";
pineafan8b4b17f2022-02-27 20:42:52 +0000192 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000193 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000194 // Allow warn
195 return true
pineafan4f164f32022-02-26 22:07:12 +0000196}
197
pineafan8b4b17f2022-02-27 20:42:52 +0000198export { command, callback, check };