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 { create, areTicketsEnabled } from "../../automations/createModActionTicket.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
| 11 | .setName("nick") |
| 12 | .setDescription("Changes a users nickname") |
| 13 | .addUserOption(option => option.setName("user").setDescription("The user to change").setRequired(true)) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 14 | .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] | 15 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when their nickname is changed | Default no").setRequired(false) |
| 16 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 17 | ) |
| 18 | |
| 19 | const callback = async (interaction: CommandInteraction) => { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 20 | // TODO:[Modals] Replace this with a modal |
| 21 | let confirmation = await new confirmationMessage(interaction) |
| 22 | .setEmoji("PUNISH.NICKNAME.RED") |
| 23 | .setTitle("Nickname") |
| 24 | .setDescription(keyValueList({ |
| 25 | "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`, |
| 26 | "new nickname": `${interaction.options.getString("name") ? interaction.options.getString("name") : "*No nickname*"}` |
| 27 | }) |
| 28 | + `The user **will${interaction.options.getString("notify") == "yes" ? '' : ' not'}** be notified\n\n` |
| 29 | + `Are you sure you want to ${interaction.options.getString("name") ? "change" : "clear"} <@!${(interaction.options.getMember("user") as GuildMember).id}>'s nickname?`) |
| 30 | .setColor("Danger") |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 31 | .addCustomBoolean( |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 32 | "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 33 | async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, interaction.options.getString("reason")), |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 34 | "An appeal ticket will be created when Confirm is clicked") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 35 | .send() |
| 36 | if (confirmation.success) { |
| 37 | let dmd = false |
| 38 | let dm; |
| 39 | try { |
| 40 | if (interaction.options.getString("notify") == "yes") { |
| 41 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
| 42 | embeds: [new generateEmojiEmbed() |
| 43 | .setEmoji("PUNISH.NICKNAME.RED") |
| 44 | .setTitle("Nickname changed") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 45 | .setDescription(`Your nickname was ${interaction.options.getString("name") ? "changed" : "cleared"} in ${interaction.guild.name}.` + |
| 46 | (interaction.options.getString("name") ? ` it is now: ${interaction.options.getString("name")}` : "") + "\n\n" + |
PineappleFan | 30b5fda | 2022-05-22 15:41:47 +0100 | [diff] [blame] | 47 | (confirmation.buttonClicked ? `You can appeal this here: <#${confirmation.response}>` : ``)) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 48 | .setStatus("Danger") |
| 49 | ] |
| 50 | }) |
| 51 | dmd = true |
| 52 | } |
| 53 | } catch {} |
| 54 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 55 | let member = (interaction.options.getMember("user") as GuildMember) |
| 56 | let before = member.nickname |
| 57 | let nickname = interaction.options.getString("name") |
| 58 | member.setNickname(nickname ?? null, "Nucleus Nickname command") |
| 59 | // @ts-ignore |
| 60 | const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = interaction.client.logger |
| 61 | let data = { |
| 62 | meta: { |
| 63 | type: 'memberUpdate', |
| 64 | displayName: 'Member Updated', |
| 65 | calculateType: 'guildMemberUpdate', |
| 66 | color: NucleusColors.yellow, |
| 67 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 68 | timestamp: new Date().getTime() |
| 69 | }, |
| 70 | list: { |
| 71 | id: entry(member.id, `\`${member.id}\``), |
| 72 | before: entry(before, before ? before : '*None*'), |
| 73 | after: entry(nickname, nickname ? nickname : '*None*'), |
| 74 | updated: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 75 | updatedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 76 | }, |
| 77 | hidden: { |
| 78 | guild: interaction.guild.id |
| 79 | } |
| 80 | } |
| 81 | log(data, interaction.client); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 82 | } catch { |
| 83 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 84 | .setEmoji("PUNISH.NICKNAME.RED") |
| 85 | .setTitle(`Nickname`) |
| 86 | .setDescription("Something went wrong and the users nickname could not be changed.") |
| 87 | .setStatus("Danger") |
| 88 | ], components: []}) |
| 89 | if (dmd) await dm.delete() |
| 90 | return |
| 91 | } |
| 92 | let failed = (dmd == false && interaction.options.getString("notify") == "yes") |
| 93 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 94 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
| 95 | .setTitle(`Nickname`) |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 96 | .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] | 97 | .setStatus(failed ? "Warning" : "Success") |
| 98 | ], components: []}) |
| 99 | } else { |
| 100 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 101 | .setEmoji("PUNISH.NICKNAME.GREEN") |
| 102 | .setTitle(`Nickname`) |
| 103 | .setDescription("No changes were made") |
| 104 | .setStatus("Success") |
| 105 | ], components: []}) |
| 106 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 110 | let member = (interaction.member as GuildMember) |
| 111 | let me = (interaction.guild.me as GuildMember) |
| 112 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 113 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 114 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 115 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 116 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
| 117 | // Check if Nucleus can change the nickname |
| 118 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
| 119 | // Check if Nucleus has permission to change the nickname |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 120 | 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] | 121 | // Allow the owner to change anyone's nickname |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 122 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 123 | // Check if the user has manage_nicknames permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 124 | 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] | 125 | // Allow changing your own nickname |
| 126 | if (member == apply) return true |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 127 | // Check if the user is below on the role list |
| 128 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
| 129 | // Allow change |
| 130 | return true |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | export { command, callback, check }; |