blob: cbf7701c50cf80ac0de7cc2bea39f5c798f9224f [file] [log] [blame]
pineafan8b4b17f2022-02-27 20:42:52 +00001import { CommandInteraction, GuildMember } 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) => {
19 // TODO:[Modals] Replace this with a modal
20 if (await new confirmationMessage(interaction)
21 .setEmoji("PUNISH.WARN.RED")
22 .setTitle("Warn")
23 .setDescription(keyValueList({
24 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
25 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
26 })
27 + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`)
28 .setColor("Danger")
29// pluralize("day", interaction.options.getInteger("delete"))
30// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
31 .send()) {
32 let dmd = false
33 try {
34 if (interaction.options.getString("notify") != "no") {
35 await (interaction.options.getMember("user") as GuildMember).send({
36 embeds: [new EmojiEmbed()
37 .setEmoji("PUNISH.WARN.RED")
38 .setTitle("Warned")
39 .setDescription(`You have been warned in ${interaction.guild.name}` +
40 (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided."))
41 .setStatus("Danger")
42 ]
43 })
44 dmd = true
45 }
46 } catch {}
47 try {
48 let failed = (dmd == false && interaction.options.getString("notify") != "no") // TODO: some way of dealing with not DMing users
49 await interaction.editReply({embeds: [new EmojiEmbed()
50 .setEmoji(`PUNISH.WARN.${failed ? "YELLOW" : "GREEN"}`)
51 .setTitle(`Warn`)
52 .setDescription(failed ? "The user cannot be messaged and was not warned" : "The user was warned")
53 .setStatus(failed ? "Warning" : "Success")
54 ], components: []})
55 } catch {
56 await interaction.editReply({embeds: [new EmojiEmbed()
57 .setEmoji("PUNISH.WARN.RED")
58 .setTitle(`Warn`)
59 .setDescription("Something went wrong and the user was not warned")
60 .setStatus("Danger")
61 ], components: []})
62 }
63 } else {
64 await interaction.editReply({embeds: [new EmojiEmbed()
65 .setEmoji("PUNISH.WARN.GREEN")
66 .setTitle(`Warn`)
67 .setDescription("No changes were made")
68 .setStatus("Success")
69 ], components: []})
70 }
pineafan4f164f32022-02-26 22:07:12 +000071}
72
73const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan8b4b17f2022-02-27 20:42:52 +000074 // Do not allow warning bots
75 if ((interaction.member as GuildMember).user.bot) throw "I cannot warn bots"
76 // Allow the owner to warn anyone
77 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
78 // Check if the user has moderate_members permission
79 if (! (interaction.member as GuildMember).permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission";
80 // Check if the user is below on the role list
81 if (! ((interaction.member as GuildMember).roles.highest.position > (interaction.options.getMember("user") as GuildMember).roles.highest.position)) throw "You do not have a role higher than that member"
82 // Allow warn
83 return true
pineafan4f164f32022-02-26 22:07:12 +000084}
85
pineafan8b4b17f2022-02-27 20:42:52 +000086export { command, callback, check };