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