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"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 3 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
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"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 9 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 10 | |
| 11 | const command = (builder: SlashCommandSubcommandBuilder) => builder |
| 12 | .setName("nick") |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 13 | // .setNameLocalizations({"ru": "name", "zh-CN": "nickname"}) |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 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 |
| 95 | ? ` it 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", |
| 159 | timestamp: new Date().getTime() |
| 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*"), |
| 165 | updated: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 166 | updatedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 167 | }, |
| 168 | hidden: { |
| 169 | guild: interaction.guild!.id |
| 170 | } |
| 171 | }; |
| 172 | log(data); |
| 173 | const failed = !dmSent && notify; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 174 | await interaction.editReply({ |
| 175 | embeds: [ |
| 176 | new EmojiEmbed() |
| 177 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
| 178 | .setTitle("Nickname") |
| 179 | .setDescription( |
| 180 | "The members nickname was changed" + |
| 181 | (failed ? ", but was not notified" : "") + |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 182 | (confirmation.components!["appeal"]!.response !== null |
| 183 | ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>` |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 184 | : "") |
| 185 | ) |
| 186 | .setStatus(failed ? "Warning" : "Success") |
| 187 | ], |
| 188 | components: [] |
| 189 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 190 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 191 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 192 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 193 | const member = interaction.member as GuildMember; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 194 | const me = interaction.guild!.members.me!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 195 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 196 | const memberPos = member.roles.cache.size ? member.roles.highest.position : 0; |
| 197 | const mePos = me.roles.cache.size ? me.roles.highest.position : 0; |
| 198 | const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 199 | if (!interaction.guild) return false; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 200 | // Do not allow any changing of the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 201 | 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] | 202 | // Check if Nucleus can change the nickname |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 203 | 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] | 204 | // Check if Nucleus has permission to change the nickname |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 205 | 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] | 206 | // Allow the owner to change anyone's nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 207 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 208 | // Check if the user has manage_nicknames permission |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 209 | if (!member.permissions.has("ManageNicknames")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 210 | return "You do not have the *Manage Nicknames* permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 211 | // Allow changing your own nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 212 | if (member === apply) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 213 | // Check if the user is below on the role list |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 214 | 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] | 215 | // Allow change |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 216 | return true; |
| 217 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 218 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 219 | export { command, callback, check }; |