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") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 12 | .addUserOption((option) => option.setName("user").setDescription("The user to change").setRequired(true)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | .addStringOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 14 | option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | ); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 16 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 17 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | .setDescription( |
| 27 | keyValueList({ |
| 28 | user: renderUser(interaction.options.getUser("user")), |
| 29 | "new nickname": `${ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 30 | interaction.options.getString("name") ? interaction.options.getString("name") : "*No nickname*" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 31 | }` |
| 32 | }) + |
| 33 | `The user **will${notify ? "" : " not"}** be notified\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 34 | `Are you sure you want to ${interaction.options.getString("name") ? "change" : "clear"} <@!${ |
| 35 | (interaction.options.getMember("user") as GuildMember).id |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | }>'s nickname?` |
| 37 | ) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 38 | .setColor("Danger") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | .addCustomBoolean( |
| 40 | "notify", |
| 41 | "Notify user", |
| 42 | false, |
| 43 | null, |
| 44 | null, |
| 45 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 46 | notify |
| 47 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | .send(interaction.options.getString("name") !== null); |
| 49 | if (confirmation.cancelled) return; |
| 50 | if (confirmation.success) break; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 51 | if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | notify = confirmation.components.notify.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 55 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | let dmd = false; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 57 | let dm; |
| 58 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 59 | if (notify) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 60 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | embeds: [ |
| 62 | new EmojiEmbed() |
| 63 | .setEmoji("PUNISH.NICKNAME.RED") |
| 64 | .setTitle("Nickname changed") |
| 65 | .setDescription( |
| 66 | `Your nickname was ${ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 67 | interaction.options.getString("name") ? "changed" : "cleared" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 68 | } in ${interaction.guild.name}.` + |
| 69 | (interaction.options.getString("name") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 70 | ? ` it is now: ${interaction.options.getString("name")}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | : "") + |
| 72 | "\n\n" + |
| 73 | (confirmation.components.appeal.response |
| 74 | ? `You can appeal this here: <#${confirmation.components.appeal.response}>` |
| 75 | : "") |
| 76 | ) |
| 77 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 78 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | }); |
| 80 | dmd = true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 81 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | } catch { |
| 83 | dmd = false; |
| 84 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 85 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | const member = interaction.options.getMember("user") as GuildMember; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | const before = member.nickname; |
| 88 | const nickname = interaction.options.getString("name"); |
| 89 | member.setNickname(nickname ?? null, "Nucleus Nickname command"); |
| 90 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | "nickname", |
| 92 | interaction.guild.id, |
| 93 | member.user, |
| 94 | interaction.user, |
| 95 | null, |
| 96 | before, |
| 97 | nickname |
| 98 | ); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 99 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 101 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | type: "memberUpdate", |
| 103 | displayName: "Member Updated", |
| 104 | calculateType: "guildMemberUpdate", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 105 | color: NucleusColors.yellow, |
| 106 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 107 | timestamp: new Date().getTime() |
| 108 | }, |
| 109 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 110 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | before: entry(before, before ? before : "*None*"), |
| 112 | after: entry(nickname, nickname ? nickname : "*None*"), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 113 | updated: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 114 | updatedBy: entry(interaction.user.id, renderUser(interaction.user)) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 115 | }, |
| 116 | hidden: { |
| 117 | guild: interaction.guild.id |
| 118 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 119 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 120 | log(data); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 121 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 122 | await interaction.editReply({ |
| 123 | embeds: [ |
| 124 | new EmojiEmbed() |
| 125 | .setEmoji("PUNISH.NICKNAME.RED") |
| 126 | .setTitle("Nickname") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 127 | .setDescription("Something went wrong and the users nickname could not be changed.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | .setStatus("Danger") |
| 129 | ], |
| 130 | components: [] |
| 131 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 132 | if (dmd) await dm.delete(); |
| 133 | return; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 134 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 135 | const failed = !dmd && notify; |
| 136 | await interaction.editReply({ |
| 137 | embeds: [ |
| 138 | new EmojiEmbed() |
| 139 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
| 140 | .setTitle("Nickname") |
| 141 | .setDescription( |
| 142 | "The members nickname was changed" + |
| 143 | (failed ? ", but was not notified" : "") + |
| 144 | (confirmation.components.appeal.response |
| 145 | ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` |
| 146 | : "") |
| 147 | ) |
| 148 | .setStatus(failed ? "Warning" : "Success") |
| 149 | ], |
| 150 | components: [] |
| 151 | }); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 152 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 153 | await interaction.editReply({ |
| 154 | embeds: [ |
| 155 | new EmojiEmbed() |
| 156 | .setEmoji("PUNISH.NICKNAME.GREEN") |
| 157 | .setTitle("Nickname") |
| 158 | .setDescription("No changes were made") |
| 159 | .setStatus("Success") |
| 160 | ], |
| 161 | components: [] |
| 162 | }); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 163 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 164 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 165 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 166 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 167 | const member = interaction.member as GuildMember; |
| 168 | const me = interaction.guild.me!; |
| 169 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 170 | if (member === null || me === null || apply === null) throw new Error("That member is not in the server"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 171 | const memberPos = member.roles ? member.roles.highest.position : 0; |
| 172 | const mePos = me.roles ? me.roles.highest.position : 0; |
| 173 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 174 | // Do not allow any changing of the owner |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 175 | if (member.id === interaction.guild.ownerId) throw new Error("You cannot change the owner's nickname"); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 176 | // Check if Nucleus can change the nickname |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 177 | if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member"); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 178 | // Check if Nucleus has permission to change the nickname |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 179 | if (!me.permissions.has("MANAGE_NICKNAMES")) throw new Error("I do not have the *Manage Nicknames* permission"); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 180 | // Allow the owner to change anyone's nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 181 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 182 | // Check if the user has manage_nicknames permission |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 183 | if (!member.permissions.has("MANAGE_NICKNAMES")) |
| 184 | throw new Error("You do not have the *Manage Nicknames* permission"); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 185 | // Allow changing your own nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 186 | if (member === apply) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 187 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 188 | if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member"); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 189 | // Allow change |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 190 | return true; |
| 191 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 192 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 193 | export { command, callback, check }; |