blob: 3ff18ecb35a284a4280d756c81f6a32a84e0e30c [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";
pineafan73a7c4a2022-07-24 10:38:04 +01007import { create, areTicketsEnabled } from "../../actions/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
pineafan4edb7762022-06-26 19:21:04 +010017const callback = async (interaction: CommandInteraction): Promise<any> => {
18 const { renderUser } = client.logger
pineafan377794f2022-04-18 19:01:01 +010019 // TODO:[Modals] Replace this with a modal
pineafan02ba0232022-07-24 22:16:15 +010020 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 })
30 + `The user **will${notify ? '' : ' 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")
33 .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify)
34 .send(interaction.options.getString("name") !== null)
35 if (confirmation.cancelled) return
36 if (confirmation.success) break
37 if (confirmation.components) {
38 notify = confirmation.components.notify.active
39 }
40 }
pineafan377794f2022-04-18 19:01:01 +010041 if (confirmation.success) {
42 let dmd = false
43 let dm;
44 try {
pineafan02ba0232022-07-24 22:16:15 +010045 if (notify) {
pineafan377794f2022-04-18 19:01:01 +010046 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010047 embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010048 .setEmoji("PUNISH.NICKNAME.RED")
49 .setTitle("Nickname changed")
pineafane625d782022-05-09 18:04:32 +010050 .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" +
pineafan02ba0232022-07-24 22:16:15 +010052 (confirmation.components.appeal.response ? `You can appeal this here: <#${confirmation.components.appeal.response}>` : ``))
pineafan377794f2022-04-18 19:01:01 +010053 .setStatus("Danger")
54 ]
55 })
56 dmd = true
57 }
58 } catch {}
59 try {
pineafane625d782022-05-09 18:04:32 +010060 let member = (interaction.options.getMember("user") as GuildMember)
61 let before = member.nickname
62 let nickname = interaction.options.getString("name")
63 member.setNickname(nickname ?? null, "Nucleus Nickname command")
pineafan4edb7762022-06-26 19:21:04 +010064 try { await client.database.history.create(
65 "nickname", interaction.guild.id, member.user, interaction.user,
66 null, before, nickname) } catch {}
pineafan02ba0232022-07-24 22:16:15 +010067 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
pineafane625d782022-05-09 18:04:32 +010068 let data = {
69 meta: {
70 type: 'memberUpdate',
71 displayName: 'Member Updated',
72 calculateType: 'guildMemberUpdate',
73 color: NucleusColors.yellow,
74 emoji: "PUNISH.NICKNAME.YELLOW",
75 timestamp: new Date().getTime()
76 },
77 list: {
pineafanda6e5342022-07-03 10:03:16 +010078 memberId: entry(member.id, `\`${member.id}\``),
pineafane625d782022-05-09 18:04:32 +010079 before: entry(before, before ? before : '*None*'),
80 after: entry(nickname, nickname ? nickname : '*None*'),
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 }
87 }
pineafan4edb7762022-06-26 19:21:04 +010088 log(data);
pineafan377794f2022-04-18 19:01:01 +010089 } catch {
pineafan4edb7762022-06-26 19:21:04 +010090 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010091 .setEmoji("PUNISH.NICKNAME.RED")
92 .setTitle(`Nickname`)
93 .setDescription("Something went wrong and the users nickname could not be changed.")
94 .setStatus("Danger")
95 ], components: []})
96 if (dmd) await dm.delete()
97 return
98 }
pineafan02ba0232022-07-24 22:16:15 +010099 let failed = (dmd == false && notify)
pineafan4edb7762022-06-26 19:21:04 +0100100 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +0100101 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
102 .setTitle(`Nickname`)
pineafan02ba0232022-07-24 22:16:15 +0100103 .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}>` : ``))
pineafan377794f2022-04-18 19:01:01 +0100104 .setStatus(failed ? "Warning" : "Success")
105 ], components: []})
106 } else {
pineafan4edb7762022-06-26 19:21:04 +0100107 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +0100108 .setEmoji("PUNISH.NICKNAME.GREEN")
109 .setTitle(`Nickname`)
110 .setDescription("No changes were made")
111 .setStatus("Success")
112 ], components: []})
113 }
pineafan32767212022-03-14 21:27:39 +0000114}
115
116const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan377794f2022-04-18 19:01:01 +0100117 let member = (interaction.member as GuildMember)
118 let me = (interaction.guild.me as GuildMember)
119 let apply = (interaction.options.getMember("user") as GuildMember)
120 if (member == null || me == null || apply == null) throw "That member is not in the server"
121 let memberPos = member.roles ? member.roles.highest.position : 0
122 let mePos = me.roles ? me.roles.highest.position : 0
123 let applyPos = apply.roles ? apply.roles.highest.position : 0
124 // Check if Nucleus can change the nickname
125 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
126 // Check if Nucleus has permission to change the nickname
pineafan4edb7762022-06-26 19:21:04 +0100127 if (! me.permissions.has("MANAGE_NICKNAMES")) throw "I do not have the Manage nicknames permission";
pineafan377794f2022-04-18 19:01:01 +0100128 // Allow the owner to change anyone's nickname
pineafan663dc472022-05-10 18:13:47 +0100129 if (member.id == interaction.guild.ownerId) return true
pineafan377794f2022-04-18 19:01:01 +0100130 // Check if the user has manage_nicknames permission
pineafan4edb7762022-06-26 19:21:04 +0100131 if (! member.permissions.has("MANAGE_NICKNAMES")) throw "You do not have the Manage nicknames permission";
pineafane625d782022-05-09 18:04:32 +0100132 // Allow changing your own nickname
133 if (member == apply) return true
pineafan377794f2022-04-18 19:01:01 +0100134 // Check if the user is below on the role list
135 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
136 // Allow change
137 return true
pineafan32767212022-03-14 21:27:39 +0000138}
139
140export { command, callback, check };