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