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