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