blob: 8dcbf625a777cb28315f357d71cab86842a1a103 [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +01001import { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js";
pineafan32767212022-03-14 21:27:39 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
4import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan32767212022-03-14 21:27:39 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan377794f2022-04-18 19:01:01 +01007import { create, areTicketsEnabled } from "../../automations/createModActionTicket.js";
pineafan4edb7762022-06-26 19:21:04 +01008import client from "../../utils/client.js"
pineafan32767212022-03-14 21:27:39 +00009
10const 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))
pineafan377794f2022-04-18 19:01:01 +010015 .addStringOption(option => option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false))
pineafan32767212022-03-14 21:27:39 +000016 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when their nickname is changed | Default no").setRequired(false)
17 .addChoices([["Yes", "yes"], ["No", "no"]])
18 )
19
pineafan4edb7762022-06-26 19:21:04 +010020const callback = async (interaction: CommandInteraction): Promise<any> => {
21 const { renderUser } = client.logger
pineafan377794f2022-04-18 19:01:01 +010022 // 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({
pineafan4edb7762022-06-26 19:21:04 +010027 "user": renderUser(interaction.options.getUser("user")),
pineafan377794f2022-04-18 19:01:01 +010028 "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")
pineafan4092b862022-05-20 19:27:23 +010033 .addCustomBoolean(
pineafan377794f2022-04-18 19:01:01 +010034 "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)),
pineafan6702cef2022-06-13 17:52:37 +010035 async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, interaction.options.getString("reason")),
pineafan4092b862022-05-20 19:27:23 +010036 "An appeal ticket will be created when Confirm is clicked")
pineafan377794f2022-04-18 19:01:01 +010037 .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({
pineafan4edb7762022-06-26 19:21:04 +010044 embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010045 .setEmoji("PUNISH.NICKNAME.RED")
46 .setTitle("Nickname changed")
pineafane625d782022-05-09 18:04:32 +010047 .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" +
PineappleFan30b5fda2022-05-22 15:41:47 +010049 (confirmation.buttonClicked ? `You can appeal this here: <#${confirmation.response}>` : ``))
pineafan377794f2022-04-18 19:01:01 +010050 .setStatus("Danger")
51 ]
52 })
53 dmd = true
54 }
55 } catch {}
56 try {
pineafane625d782022-05-09 18:04:32 +010057 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")
pineafan4edb7762022-06-26 19:21:04 +010061 try { await client.database.history.create(
62 "nickname", interaction.guild.id, member.user, interaction.user,
63 null, before, nickname) } catch {}
pineafane625d782022-05-09 18:04:32 +010064 // @ts-ignore
PineappleFanb3dd83c2022-06-17 10:53:48 +010065 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger
pineafane625d782022-05-09 18:04:32 +010066 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: {
pineafanda6e5342022-07-03 10:03:16 +010076 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010077 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 }
pineafan4edb7762022-06-26 19:21:04 +010086 log(data);
pineafan377794f2022-04-18 19:01:01 +010087 } catch {
pineafan4edb7762022-06-26 19:21:04 +010088 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010089 .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")
pineafan4edb7762022-06-26 19:21:04 +010098 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010099 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
100 .setTitle(`Nickname`)
pineafan4092b862022-05-20 19:27:23 +0100101 .setDescription("The members nickname was changed" + (failed ? ", but was not notified" : "") + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : ``))
pineafan377794f2022-04-18 19:01:01 +0100102 .setStatus(failed ? "Warning" : "Success")
103 ], components: []})
104 } else {
pineafan4edb7762022-06-26 19:21:04 +0100105 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +0100106 .setEmoji("PUNISH.NICKNAME.GREEN")
107 .setTitle(`Nickname`)
108 .setDescription("No changes were made")
109 .setStatus("Success")
110 ], components: []})
111 }
pineafan32767212022-03-14 21:27:39 +0000112}
113
114const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan377794f2022-04-18 19:01:01 +0100115 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
pineafan4edb7762022-06-26 19:21:04 +0100125 if (! me.permissions.has("MANAGE_NICKNAMES")) throw "I do not have the Manage nicknames permission";
pineafan377794f2022-04-18 19:01:01 +0100126 // Allow the owner to change anyone's nickname
pineafan663dc472022-05-10 18:13:47 +0100127 if (member.id == interaction.guild.ownerId) return true
pineafan377794f2022-04-18 19:01:01 +0100128 // Check if the user has manage_nicknames permission
pineafan4edb7762022-06-26 19:21:04 +0100129 if (! member.permissions.has("MANAGE_NICKNAMES")) throw "You do not have the Manage nicknames permission";
pineafane625d782022-05-09 18:04:32 +0100130 // Allow changing your own nickname
131 if (member == apply) return true
pineafan377794f2022-04-18 19:01:01 +0100132 // 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
pineafan32767212022-03-14 21:27:39 +0000136}
137
138export { command, callback, check };