Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { LinkWarningFooter } from "./../../utils/defaults.js"; |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 2 | import { |
| 3 | ActionRowBuilder, |
| 4 | ButtonBuilder, |
| 5 | CommandInteraction, |
| 6 | GuildMember, |
| 7 | ButtonStyle, |
| 8 | Message, |
| 9 | ButtonInteraction, |
| 10 | ModalBuilder, |
| 11 | TextInputBuilder, |
| 12 | TextInputStyle |
| 13 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 14 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 15 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 16 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 17 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | import client from "../../utils/client.js"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 19 | import { areTicketsEnabled, create } from "../../actions/createModActionTicket.js"; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 20 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 21 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 22 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 23 | builder |
| 24 | .setName("nick") |
| 25 | .setDescription("Changes a users nickname") |
| 26 | .addUserOption((option) => option.setName("user").setDescription("The user to change").setRequired(true)) |
| 27 | .addStringOption((option) => |
| 28 | option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false) |
| 29 | ); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 30 | |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 31 | const callback = async ( |
| 32 | interaction: CommandInteraction | ButtonInteraction, |
| 33 | member?: GuildMember |
| 34 | ): Promise<unknown> => { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 35 | const { log, NucleusColors, entry, renderDelta, renderUser } = client.logger; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 36 | let newNickname; |
| 37 | if (!interaction.isButton()) { |
| 38 | member = interaction.options.getMember("user") as GuildMember; |
| 39 | newNickname = interaction.options.get("name")?.value as string | undefined; |
| 40 | } |
| 41 | if (!member) return; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 42 | // TODO:[Modals] Replace this with a modal |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 43 | let notify = false; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 44 | let confirmation; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 45 | let timedOut = false; |
| 46 | let success = false; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 47 | let createAppealTicket = false; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 48 | let firstRun = !interaction.isButton(); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 49 | do { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 50 | confirmation = await new confirmationMessage(interaction) |
| 51 | .setEmoji("PUNISH.NICKNAME.RED") |
| 52 | .setTitle("Nickname") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | .setDescription( |
| 54 | keyValueList({ |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 55 | user: renderUser(member.user), |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 56 | "new nickname": `${newNickname ? newNickname : "*No nickname*"}` |
| 57 | }) + `Are you sure you want to ${newNickname ? "change" : "clear"} <@!${member.id}>'s nickname?` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | ) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 59 | .setColor("Danger") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | .addCustomBoolean( |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 61 | "appeal", |
| 62 | "Create appeal ticket", |
| 63 | !(await areTicketsEnabled(interaction.guild!.id)), |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 64 | async () => await create(interaction.guild!, member!.user, interaction.user, "Nickname changed"), |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 65 | "An appeal ticket will be created", |
| 66 | null, |
| 67 | "CONTROL.TICKET", |
| 68 | createAppealTicket |
| 69 | ) |
| 70 | .addCustomBoolean( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | "notify", |
| 72 | "Notify user", |
| 73 | false, |
| 74 | null, |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 75 | "The user will be sent a DM", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 76 | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 78 | notify |
| 79 | ) |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 80 | .addModal( |
| 81 | "Change nickname", |
| 82 | "ICONS.EDIT", |
| 83 | "modal", |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 84 | { default: newNickname ?? "" }, |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 85 | new ModalBuilder().setTitle("Editing nickname").addComponents( |
| 86 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 87 | new TextInputBuilder() |
| 88 | .setCustomId("default") |
| 89 | .setLabel("Nickname") |
| 90 | .setMaxLength(32) |
| 91 | .setRequired(false) |
| 92 | .setStyle(TextInputStyle.Short) |
| 93 | .setValue(newNickname ? newNickname : " ") |
| 94 | ) |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 95 | ) |
| 96 | ) |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 97 | .setFailedMessage("No changes were made", "Success", "PUNISH.NICKNAME.GREEN") |
| 98 | .send(!firstRun); |
| 99 | firstRun = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 100 | if (confirmation.cancelled) timedOut = true; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 101 | else if (confirmation.success !== undefined) success = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 102 | else if (confirmation.components) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 103 | notify = confirmation.components["notify"]!.active; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 104 | createAppealTicket = confirmation.components["appeal"]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 105 | } |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 106 | if (confirmation.modals) newNickname = confirmation.modals![0]!.values["default"]; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 107 | } while (!timedOut && !success); |
| 108 | if (timedOut || !success) return; |
| 109 | let dmSent = false; |
| 110 | let dmMessage: Message; |
| 111 | const config = await client.database.guilds.read(interaction.guild!.id); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 112 | try { |
| 113 | if (notify) { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 114 | const messageData: { |
| 115 | embeds: EmojiEmbed[]; |
| 116 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 117 | } = { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 118 | embeds: [ |
| 119 | new EmojiEmbed() |
| 120 | .setEmoji("PUNISH.NICKNAME.RED") |
| 121 | .setTitle("Nickname changed") |
| 122 | .setDescription( |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 123 | `Your nickname was ${newNickname ? "changed" : "cleared"} in ${interaction.guild!.name}.` + |
| 124 | (newNickname ? `\nIt is now: ${newNickname}` : "") + |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 125 | "\n\n" + |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 126 | (createAppealTicket |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 127 | ? `You can appeal this in the ticket created in <#${ |
| 128 | confirmation.components!["appeal"]!.response |
| 129 | }>` |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 130 | : "") |
| 131 | ) |
| 132 | .setStatus("Danger") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 133 | ], |
| 134 | components: [] |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 135 | }; |
| 136 | if (config.moderation.nick.text && config.moderation.nick.link) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 137 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 138 | messageData.components.push( |
| 139 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 140 | new ButtonBuilder() |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 141 | .setStyle(ButtonStyle.Link) |
| 142 | .setLabel(config.moderation.nick.text) |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 143 | .setURL(config.moderation.nick.link.replaceAll("{id}", member.id)) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 144 | ) |
| 145 | ); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 146 | } |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 147 | dmMessage = await member.send(messageData); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 148 | dmSent = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 149 | } |
| 150 | } catch { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 151 | dmSent = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 152 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 153 | let before: string | null; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 154 | try { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 155 | before = member.nickname; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 156 | await member.setNickname(newNickname ?? null, "Nucleus Nickname command"); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 157 | await client.database.history.create( |
| 158 | "nickname", |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 159 | interaction.guild!.id, |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 160 | member.user, |
| 161 | interaction.user, |
| 162 | null, |
| 163 | before, |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 164 | newNickname |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 165 | ); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 166 | } catch { |
| 167 | await interaction.editReply({ |
| 168 | embeds: [ |
| 169 | new EmojiEmbed() |
| 170 | .setEmoji("PUNISH.NICKNAME.RED") |
| 171 | .setTitle("Nickname") |
| 172 | .setDescription("Something went wrong and the users nickname could not be changed.") |
| 173 | .setStatus("Danger") |
| 174 | ], |
| 175 | components: [] |
| 176 | }); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 177 | if (dmSent) await dmMessage!.delete(); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 178 | return; |
| 179 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 180 | const data = { |
| 181 | meta: { |
| 182 | type: "memberUpdate", |
| 183 | displayName: "Member Updated", |
| 184 | calculateType: "guildMemberUpdate", |
| 185 | color: NucleusColors.yellow, |
| 186 | emoji: "PUNISH.NICKNAME.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 187 | timestamp: Date.now() |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 188 | }, |
| 189 | list: { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 190 | member: entry(member.id, renderUser(member.user)), |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 191 | before: entry(before, before ?? "*No nickname set*"), |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 192 | after: entry(newNickname ?? null, newNickname ?? "*No nickname set*"), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 193 | updated: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 194 | updatedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 195 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 196 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 197 | end: |
| 198 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 199 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 200 | }, |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 201 | hidden: { |
| 202 | guild: interaction.guild!.id |
| 203 | } |
| 204 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 205 | await log(data); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 206 | const failed = !dmSent && notify; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 207 | await interaction.editReply({ |
| 208 | embeds: [ |
| 209 | new EmojiEmbed() |
| 210 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
| 211 | .setTitle("Nickname") |
| 212 | .setDescription( |
| 213 | "The members nickname was changed" + |
| 214 | (failed ? ", but was not notified" : "") + |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 215 | (confirmation.components!["appeal"]!.response !== null |
| 216 | ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>` |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 217 | : "") |
| 218 | ) |
| 219 | .setStatus(failed ? "Warning" : "Success") |
| 220 | ], |
| 221 | components: [] |
| 222 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 223 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 224 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 225 | const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean, target?: GuildMember) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 226 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 227 | // Check if the user has manage_nicknames permission |
| 228 | if (!member.permissions.has("ManageNicknames")) return "You do not have the *Manage Nicknames* permission"; |
| 229 | if (partial) return true; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 230 | const me = interaction.guild!.members.me!; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 231 | let apply: GuildMember; |
| 232 | if (interaction.isButton()) { |
| 233 | apply = target!; |
| 234 | } else { |
| 235 | apply = interaction.options.getMember("user") as GuildMember; |
| 236 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 237 | const memberPos = member.roles.cache.size ? member.roles.highest.position : 0; |
| 238 | const mePos = me.roles.cache.size ? me.roles.highest.position : 0; |
| 239 | const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 240 | if (!interaction.guild) return false; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 241 | // Do not allow any changing of the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 242 | 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] | 243 | // Check if Nucleus can change the nickname |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 244 | if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 245 | // Check if Nucleus has permission to change the nickname |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 246 | 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] | 247 | // Allow the owner to change anyone's nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 248 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 249 | // Allow changing your own nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 250 | if (member === apply) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 251 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 252 | if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 253 | // Allow change |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 254 | return true; |
| 255 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 256 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 257 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 258 | export const metadata = { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 259 | longDescription: |
| 260 | "Changes the nickname of a member. This is the name that shows in the member list and on messages.", |
| 261 | premiumOnly: true |
| 262 | }; |