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