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