pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
| 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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 7 | import { create, areTicketsEnabled } from "../../actions/createModActionTicket.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | import client from "../../utils/client.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | .setName("nick") |
| 13 | .setDescription("Changes a users nickname") |
| 14 | .addUserOption(option => option.setName("user").setDescription("The user to change").setRequired(true)) |
| 15 | .addStringOption(option => option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false)); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 16 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 17 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | const { renderUser } = client.logger; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 19 | // TODO:[Modals] Replace this with a modal |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 20 | let notify = true; |
| 21 | let confirmation; |
| 22 | while (true) { |
| 23 | confirmation = await new confirmationMessage(interaction) |
| 24 | .setEmoji("PUNISH.NICKNAME.RED") |
| 25 | .setTitle("Nickname") |
| 26 | .setDescription(keyValueList({ |
| 27 | "user": renderUser(interaction.options.getUser("user")), |
| 28 | "new nickname": `${interaction.options.getString("name") ? interaction.options.getString("name") : "*No nickname*"}` |
| 29 | }) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | + `The user **will${notify ? "" : " not"}** be notified\n\n` |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 31 | + `Are you sure you want to ${interaction.options.getString("name") ? "change" : "clear"} <@!${(interaction.options.getMember("user") as GuildMember).id}>'s nickname?`) |
| 32 | .setColor("Danger") |
| 33 | .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | .send(interaction.options.getString("name") !== null); |
| 35 | if (confirmation.cancelled) return; |
| 36 | if (confirmation.success) break; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 37 | if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | notify = confirmation.components.notify.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 39 | } |
| 40 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 41 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | let dmd = false; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 43 | let dm; |
| 44 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 45 | if (notify) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 46 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 47 | embeds: [new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 48 | .setEmoji("PUNISH.NICKNAME.RED") |
| 49 | .setTitle("Nickname changed") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 50 | .setDescription(`Your nickname was ${interaction.options.getString("name") ? "changed" : "cleared"} in ${interaction.guild.name}.` + |
| 51 | (interaction.options.getString("name") ? ` it is now: ${interaction.options.getString("name")}` : "") + "\n\n" + |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | (confirmation.components.appeal.response ? `You can appeal this here: <#${confirmation.components.appeal.response}>` : "")) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 53 | .setStatus("Danger") |
| 54 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | }); |
| 56 | dmd = true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 57 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 58 | } catch { dmd = false; } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 59 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | const member = (interaction.options.getMember("user") as GuildMember); |
| 61 | const before = member.nickname; |
| 62 | const nickname = interaction.options.getString("name"); |
| 63 | member.setNickname(nickname ?? null, "Nucleus Nickname command"); |
| 64 | await client.database.history.create( |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 65 | "nickname", interaction.guild.id, member.user, interaction.user, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | null, before, nickname); |
| 67 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 68 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 69 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | type: "memberUpdate", |
| 71 | displayName: "Member Updated", |
| 72 | calculateType: "guildMemberUpdate", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 73 | color: NucleusColors.yellow, |
| 74 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 75 | timestamp: new Date().getTime() |
| 76 | }, |
| 77 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 78 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | before: entry(before, before ? before : "*None*"), |
| 80 | after: entry(nickname, nickname ? nickname : "*None*"), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 81 | updated: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 82 | updatedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 83 | }, |
| 84 | hidden: { |
| 85 | guild: interaction.guild.id |
| 86 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 88 | log(data); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 89 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 90 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 91 | .setEmoji("PUNISH.NICKNAME.RED") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 92 | .setTitle("Nickname") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 93 | .setDescription("Something went wrong and the users nickname could not be changed.") |
| 94 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 95 | ], components: []}); |
| 96 | if (dmd) await dm.delete(); |
| 97 | return; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 98 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | const failed = (dmd === false && notify); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 100 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 101 | .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | .setTitle("Nickname") |
| 103 | .setDescription("The members nickname was changed" + (failed ? ", but was not notified" : "") + (confirmation.components.appeal.response ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` : "")) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 104 | .setStatus(failed ? "Warning" : "Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 105 | ], components: []}); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 106 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 107 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 108 | .setEmoji("PUNISH.NICKNAME.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | .setTitle("Nickname") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 110 | .setDescription("No changes were made") |
| 111 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | ], components: []}); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 113 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 114 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 115 | |
| 116 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 117 | const member = (interaction.member as GuildMember); |
| 118 | const me = (interaction.guild.me as GuildMember); |
| 119 | const apply = (interaction.options.getMember("user") as GuildMember); |
| 120 | if (member === null || me === null || apply === null) throw "That member is not in the server"; |
| 121 | const memberPos = member.roles ? member.roles.highest.position : 0; |
| 122 | const mePos = me.roles ? me.roles.highest.position : 0; |
| 123 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 124 | // Do not allow any changing of the owner |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 125 | if (member.id === interaction.guild.ownerId) throw "You cannot change the owner's nickname"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 126 | // Check if Nucleus can change the nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 127 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 128 | // Check if Nucleus has permission to change the nickname |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 129 | if (! me.permissions.has("MANAGE_NICKNAMES")) throw "I do not have the *Manage Nicknames* permission"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 130 | // Allow the owner to change anyone's nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 131 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 132 | // Check if the user has manage_nicknames permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 133 | if (! member.permissions.has("MANAGE_NICKNAMES")) throw "You do not have the *Manage Nicknames* permission"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 134 | // Allow changing your own nickname |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | if (member === apply) return true; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 136 | // Check if the user is below on the role list |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 137 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 138 | // Allow change |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 139 | return true; |
| 140 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 141 | |
| 142 | export { command, callback, check }; |