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