blob: 366289627aea91c65406f00d38a811817d09c2ad [file] [log] [blame]
pineafan5d1908e2022-02-28 21:34:47 +00001import Discord, { CommandInteraction, GuildMember, MessageActionRow } 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";
5import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
6import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
10 .setName("warn")
11 .setDescription("Warns a user")
pineafan8b4b17f2022-02-27 20:42:52 +000012 .addUserOption(option => option.setName("user").setDescription("The user to warn").setRequired(true))
13 .addStringOption(option => option.setName("reason").setDescription("The reason for the warn").setRequired(false))
14 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are warned | Default yes").setRequired(false)
15 .addChoices([["Yes", "yes"], ["No", "no"]])
16 )
pineafan4f164f32022-02-26 22:07:12 +000017
pineafan8b4b17f2022-02-27 20:42:52 +000018const callback = async (interaction: CommandInteraction) => {
pineafan1dc15722022-03-14 21:27:34 +000019 // @ts-ignore
20 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = interaction.client.logger
pineafan8b4b17f2022-02-27 20:42:52 +000021 // TODO:[Modals] Replace this with a modal
22 if (await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.WARN.RED")
24 .setTitle("Warn")
25 .setDescription(keyValueList({
26 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
27 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
28 })
pineafan1dc15722022-03-14 21:27:34 +000029 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`
30 + `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
pineafan8b4b17f2022-02-27 20:42:52 +000031 .setColor("Danger")
32// pluralize("day", interaction.options.getInteger("delete"))
33// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
34 .send()) {
35 let dmd = false
36 try {
37 if (interaction.options.getString("notify") != "no") {
38 await (interaction.options.getMember("user") as GuildMember).send({
39 embeds: [new EmojiEmbed()
40 .setEmoji("PUNISH.WARN.RED")
41 .setTitle("Warned")
42 .setDescription(`You have been warned in ${interaction.guild.name}` +
pineafan1dc15722022-03-14 21:27:34 +000043 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan8b4b17f2022-02-27 20:42:52 +000044 .setStatus("Danger")
45 ]
46 })
47 dmd = true
48 }
pineafan8b4b17f2022-02-27 20:42:52 +000049 } catch {
50 await interaction.editReply({embeds: [new EmojiEmbed()
51 .setEmoji("PUNISH.WARN.RED")
52 .setTitle(`Warn`)
53 .setDescription("Something went wrong and the user was not warned")
54 .setStatus("Danger")
55 ], components: []})
56 }
pineafan1dc15722022-03-14 21:27:34 +000057 let data = {
58 meta:{
59 type: 'memberWarn',
60 displayName: 'Member warned',
61 calculateType: 'guildMemberPunish',
62 color: NucleusColors.yellow,
63 emoji: 'PUNISH.WARN.YELLOW',
64 timestamp: new Date().getTime()
65 },
66 list: {
67 user: renderUser((interaction.options.getMember("user") as GuildMember).user.id, (interaction.options.getMember("user") as GuildMember).user),
68 warnedBy: renderUser(interaction.member.user.id, interaction.member.user),
69 reason: (interaction.options.getString("reason") ? `\n> ${interaction.options.getString("reason")}` : "No reason provided")
70 },
71 hidden: {
72 guild: interaction.guild.id
73 }
74 }
75 log(data, interaction.client);
pineafan5d1908e2022-02-28 21:34:47 +000076 let failed = (dmd == false && interaction.options.getString("notify") != "no")
77 if (!failed) {
78 await interaction.editReply({embeds: [new EmojiEmbed()
79 .setEmoji(`PUNISH.WARN.GREEN`)
80 .setTitle(`Warn`)
81 .setDescription("The user was warned")
82 .setStatus("Success")
83 ], components: []})
84 } else {
85 let m = await interaction.editReply({
86 embeds: [new EmojiEmbed()
87 .setEmoji(`PUNISH.WARN.RED`)
88 .setTitle(`Warn`)
89 .setDescription("The user's DMs are not open\n\nWhat would you like to do?")
90 .setStatus("Danger")
91 ], components: [
92 new MessageActionRow().addComponents([
93 new Discord.MessageButton()
94 .setCustomId("log")
95 .setLabel("Ignore and log")
96 .setStyle("SECONDARY"),
97 new Discord.MessageButton()
98 .setCustomId("here")
99 .setLabel("Warn here")
100 .setStyle("SECONDARY")
101 .setDisabled((interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL") === false),
102 ])
103 ],
104 })
105 let component;
106 try {
107 component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000});
108 } catch (e) {
109 return await interaction.editReply({embeds: [new EmojiEmbed()
110 .setEmoji(`PUNISH.WARN.GREEN`)
111 .setTitle(`Warn`)
112 .setDescription("No changes were made")
113 .setStatus("Success")
114 ], components: []})
115 }
116 if ( component.customId == "here" ) {
117 await interaction.channel.send({
118 embeds: [new EmojiEmbed()
119 .setEmoji(`PUNISH.WARN.RED`)
120 .setTitle(`Warn`)
121 .setDescription(`You have been warned` +
pineafan1dc15722022-03-14 21:27:34 +0000122 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : "."))
pineafan5d1908e2022-02-28 21:34:47 +0000123 .setStatus("Danger")
124 ],
125 content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`,
126 allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]}
127 })
128 return await interaction.editReply({embeds: [new EmojiEmbed()
129 .setEmoji(`PUNISH.WARN.GREEN`)
130 .setTitle(`Warn`)
131 .setDescription("The user was warned")
132 .setStatus("Success")
133 ], components: []})
134 } else {
135 await interaction.editReply({embeds: [new EmojiEmbed()
136 .setEmoji(`PUNISH.WARN.GREEN`)
137 .setTitle(`Warn`)
138 .setDescription("The warn was logged")
139 .setStatus("Success")
140 ], components: []})
141 }
142 }
pineafan8b4b17f2022-02-27 20:42:52 +0000143 } else {
144 await interaction.editReply({embeds: [new EmojiEmbed()
145 .setEmoji("PUNISH.WARN.GREEN")
146 .setTitle(`Warn`)
147 .setDescription("No changes were made")
148 .setStatus("Success")
149 ], components: []})
150 }
pineafan4f164f32022-02-26 22:07:12 +0000151}
152
153const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000154 let member = (interaction.member as GuildMember)
155 let me = (interaction.guild.me as GuildMember)
156 let apply = (interaction.options.getMember("user") as GuildMember)
157 if (member == null || me == null || apply == null) throw "That member is not in the server"
158 let memberPos = member.roles ? member.roles.highest.position : 0
159 let mePos = me.roles ? me.roles.highest.position : 0
160 let applyPos = apply.roles ? apply.roles.highest.position : 0
pineafan8b4b17f2022-02-27 20:42:52 +0000161 // Do not allow warning bots
162 if ((interaction.member as GuildMember).user.bot) throw "I cannot warn bots"
163 // Allow the owner to warn anyone
164 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
165 // Check if the user has moderate_members permission
166 if (! (interaction.member as GuildMember).permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission";
167 // Check if the user is below on the role list
pineafan5d1908e2022-02-28 21:34:47 +0000168 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
pineafan8b4b17f2022-02-27 20:42:52 +0000169 // Allow warn
170 return true
pineafan4f164f32022-02-26 22:07:12 +0000171}
172
pineafan8b4b17f2022-02-27 20:42:52 +0000173export { command, callback, check };