pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
| 4 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 5 | import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 7 | import readConfig from '../../utils/readConfig.js'; |
| 8 | import { create, areTicketsEnabled } from "../../automations/createModActionTicket.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("nick") |
| 13 | .setDescription("Changes a users nickname") |
| 14 | .addUserOption(option => option.setName("user").setDescription("The user to change").setRequired(true)) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 15 | .addStringOption(option => option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false)) |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 16 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when their nickname is changed | Default no").setRequired(false) |
| 17 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 18 | ) |
| 19 | |
| 20 | const callback = async (interaction: CommandInteraction) => { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 21 | // TODO:[Modals] Replace this with a modal |
| 22 | let confirmation = await new confirmationMessage(interaction) |
| 23 | .setEmoji("PUNISH.NICKNAME.RED") |
| 24 | .setTitle("Nickname") |
| 25 | .setDescription(keyValueList({ |
| 26 | "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`, |
| 27 | "new nickname": `${interaction.options.getString("name") ? interaction.options.getString("name") : "*No nickname*"}` |
| 28 | }) |
| 29 | + `The user **will${interaction.options.getString("notify") == "yes" ? '' : ' not'}** be notified\n\n` |
| 30 | + `Are you sure you want to ${interaction.options.getString("name") ? "change" : "clear"} <@!${(interaction.options.getMember("user") as GuildMember).id}>'s nickname?`) |
| 31 | .setColor("Danger") |
| 32 | .addCustomCallback( |
| 33 | "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
| 34 | () => { create(interaction.guild, interaction.options.getUser("user"), interaction.client)}, |
| 35 | "An appeal ticket was created") |
| 36 | // pluralize("day", interaction.options.getInteger("delete")) |
| 37 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
| 38 | .send() |
| 39 | if (confirmation.success) { |
| 40 | let dmd = false |
| 41 | let dm; |
| 42 | try { |
| 43 | if (interaction.options.getString("notify") == "yes") { |
| 44 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
| 45 | embeds: [new generateEmojiEmbed() |
| 46 | .setEmoji("PUNISH.NICKNAME.RED") |
| 47 | .setTitle("Nickname changed") |
| 48 | .setDescription(`Your nickname was ${interaction.options.getString("name") ? "changed" : "cleared"} in ${interaction.guild.name}` + |
| 49 | (interaction.options.getString("name") ? ` it is now: ${interaction.options.getString("name")}` : ".") + "\n\n" + |
| 50 | (confirmation.buttonClicked ? `You can appeal this in this ticket: <#${confirmation.response}>` : ``)) |
| 51 | .setStatus("Danger") |
| 52 | ] |
| 53 | }) |
| 54 | dmd = true |
| 55 | } |
| 56 | } catch {} |
| 57 | try { |
| 58 | (interaction.options.getMember("user") as GuildMember).setNickname(interaction.options.getString("name") ?? null, "Nucleus Nickname command") |
| 59 | } catch { |
| 60 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 61 | .setEmoji("PUNISH.NICKNAME.RED") |
| 62 | .setTitle(`Nickname`) |
| 63 | .setDescription("Something went wrong and the users nickname could not be changed.") |
| 64 | .setStatus("Danger") |
| 65 | ], components: []}) |
| 66 | if (dmd) await dm.delete() |
| 67 | return |
| 68 | } |
| 69 | let failed = (dmd == false && interaction.options.getString("notify") == "yes") |
| 70 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 71 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
| 72 | .setTitle(`Nickname`) |
| 73 | .setDescription("The members nickname was changed" + (failed ? ", but was not notified" : "")) |
| 74 | .setStatus(failed ? "Warning" : "Success") |
| 75 | ], components: []}) |
| 76 | } else { |
| 77 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 78 | .setEmoji("PUNISH.NICKNAME.GREEN") |
| 79 | .setTitle(`Nickname`) |
| 80 | .setDescription("No changes were made") |
| 81 | .setStatus("Success") |
| 82 | ], components: []}) |
| 83 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 87 | let member = (interaction.member as GuildMember) |
| 88 | let me = (interaction.guild.me as GuildMember) |
| 89 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 90 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 91 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 92 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 93 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
| 94 | // Check if Nucleus can change the nickname |
| 95 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
| 96 | // Check if Nucleus has permission to change the nickname |
| 97 | if (! interaction.guild.me.permissions.has("MANAGE_NICKNAMES")) throw "I do not have the `manage_nicknames` permission"; |
| 98 | // Allow the owner to change anyone's nickname |
| 99 | if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true |
| 100 | // Check if the user has manage_nicknames permission |
| 101 | if (! (interaction.member as GuildMember).permissions.has("MANAGE_NICKNAMES")) throw "You do not have the `manage_nicknames` permission"; |
| 102 | // Check if the user is below on the role list |
| 103 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
| 104 | // Allow change |
| 105 | return true |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | export { command, callback, check }; |