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") |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame^] | 32 | .addCustomBoolean( |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 33 | "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame^] | 34 | async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, interaction.client), |
| 35 | "An appeal ticket will be created when Confirm is clicked") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 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") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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" + |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 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 { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 58 | let member = (interaction.options.getMember("user") as GuildMember) |
| 59 | let before = member.nickname |
| 60 | let nickname = interaction.options.getString("name") |
| 61 | member.setNickname(nickname ?? null, "Nucleus Nickname command") |
| 62 | // @ts-ignore |
| 63 | const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = interaction.client.logger |
| 64 | let data = { |
| 65 | meta: { |
| 66 | type: 'memberUpdate', |
| 67 | displayName: 'Member Updated', |
| 68 | calculateType: 'guildMemberUpdate', |
| 69 | color: NucleusColors.yellow, |
| 70 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 71 | timestamp: new Date().getTime() |
| 72 | }, |
| 73 | list: { |
| 74 | id: entry(member.id, `\`${member.id}\``), |
| 75 | before: entry(before, before ? before : '*None*'), |
| 76 | after: entry(nickname, nickname ? nickname : '*None*'), |
| 77 | updated: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 78 | updatedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 79 | }, |
| 80 | hidden: { |
| 81 | guild: interaction.guild.id |
| 82 | } |
| 83 | } |
| 84 | log(data, interaction.client); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 85 | } catch { |
| 86 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 87 | .setEmoji("PUNISH.NICKNAME.RED") |
| 88 | .setTitle(`Nickname`) |
| 89 | .setDescription("Something went wrong and the users nickname could not be changed.") |
| 90 | .setStatus("Danger") |
| 91 | ], components: []}) |
| 92 | if (dmd) await dm.delete() |
| 93 | return |
| 94 | } |
| 95 | let failed = (dmd == false && interaction.options.getString("notify") == "yes") |
| 96 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 97 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
| 98 | .setTitle(`Nickname`) |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame^] | 99 | .setDescription("The members nickname was changed" + (failed ? ", but was not notified" : "") + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : ``)) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 100 | .setStatus(failed ? "Warning" : "Success") |
| 101 | ], components: []}) |
| 102 | } else { |
| 103 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 104 | .setEmoji("PUNISH.NICKNAME.GREEN") |
| 105 | .setTitle(`Nickname`) |
| 106 | .setDescription("No changes were made") |
| 107 | .setStatus("Success") |
| 108 | ], components: []}) |
| 109 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 113 | let member = (interaction.member as GuildMember) |
| 114 | let me = (interaction.guild.me as GuildMember) |
| 115 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 116 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 117 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 118 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 119 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
| 120 | // Check if Nucleus can change the nickname |
| 121 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
| 122 | // Check if Nucleus has permission to change the nickname |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 123 | if (! me.permissions.has("MANAGE_NICKNAMES")) throw "I do not have the `manage_nicknames` permission"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 124 | // Allow the owner to change anyone's nickname |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 125 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 126 | // Check if the user has manage_nicknames permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 127 | if (! member.permissions.has("MANAGE_NICKNAMES")) throw "You do not have the `manage_nicknames` permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 128 | // Allow changing your own nickname |
| 129 | if (member == apply) return true |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 130 | // Check if the user is below on the role list |
| 131 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
| 132 | // Allow change |
| 133 | return true |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | export { command, callback, check }; |