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