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