blob: e8463f436dc4d9edd20319b358f8675817d5263e [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";
pineafan377794f2022-04-18 19:01:01 +01005import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan32767212022-03-14 21:27:39 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan377794f2022-04-18 19:01:01 +01007import readConfig from '../../utils/readConfig.js';
8import { create, areTicketsEnabled } from "../../automations/createModActionTicket.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
20const callback = async (interaction: CommandInteraction) => {
pineafan377794f2022-04-18 19:01:01 +010021 // TODO:[Modals] Replace this with a modal
22 let confirmation = await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.NICKNAME.RED")
24 .setTitle("Nickname")
25 .setDescription(keyValueList({
26 "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`,
27 "new nickname": `${interaction.options.getString("name") ? interaction.options.getString("name") : "*No nickname*"}`
28 })
29 + `The user **will${interaction.options.getString("notify") == "yes" ? '' : ' not'}** be notified\n\n`
30 + `Are you sure you want to ${interaction.options.getString("name") ? "change" : "clear"} <@!${(interaction.options.getMember("user") as GuildMember).id}>'s nickname?`)
31 .setColor("Danger")
pineafan6fb3e072022-05-20 19:27:23 +010032 .addCustomBoolean(
pineafan377794f2022-04-18 19:01:01 +010033 "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)),
pineafan6fb3e072022-05-20 19:27:23 +010034 async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, interaction.client),
35 "An appeal ticket will be created when Confirm is clicked")
pineafan377794f2022-04-18 19:01:01 +010036// pluralize("day", interaction.options.getInteger("delete"))
37// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
38 .send()
39 if (confirmation.success) {
40 let dmd = false
41 let dm;
42 try {
43 if (interaction.options.getString("notify") == "yes") {
44 dm = await (interaction.options.getMember("user") as GuildMember).send({
45 embeds: [new generateEmojiEmbed()
46 .setEmoji("PUNISH.NICKNAME.RED")
47 .setTitle("Nickname changed")
pineafane625d782022-05-09 18:04:32 +010048 .setDescription(`Your nickname was ${interaction.options.getString("name") ? "changed" : "cleared"} in ${interaction.guild.name}.` +
49 (interaction.options.getString("name") ? ` it is now: ${interaction.options.getString("name")}` : "") + "\n\n" +
pineafan377794f2022-04-18 19:01:01 +010050 (confirmation.buttonClicked ? `You can appeal this in this ticket: <#${confirmation.response}>` : ``))
51 .setStatus("Danger")
52 ]
53 })
54 dmd = true
55 }
56 } catch {}
57 try {
pineafane625d782022-05-09 18:04:32 +010058 let member = (interaction.options.getMember("user") as GuildMember)
59 let before = member.nickname
60 let nickname = interaction.options.getString("name")
61 member.setNickname(nickname ?? null, "Nucleus Nickname command")
62 // @ts-ignore
63 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = interaction.client.logger
64 let data = {
65 meta: {
66 type: 'memberUpdate',
67 displayName: 'Member Updated',
68 calculateType: 'guildMemberUpdate',
69 color: NucleusColors.yellow,
70 emoji: "PUNISH.NICKNAME.YELLOW",
71 timestamp: new Date().getTime()
72 },
73 list: {
74 id: entry(member.id, `\`${member.id}\``),
75 before: entry(before, before ? before : '*None*'),
76 after: entry(nickname, nickname ? nickname : '*None*'),
77 updated: entry(new Date().getTime(), renderDelta(new Date().getTime())),
78 updatedBy: entry(interaction.user.id, renderUser(interaction.user))
79 },
80 hidden: {
81 guild: interaction.guild.id
82 }
83 }
84 log(data, interaction.client);
pineafan377794f2022-04-18 19:01:01 +010085 } catch {
86 await interaction.editReply({embeds: [new generateEmojiEmbed()
87 .setEmoji("PUNISH.NICKNAME.RED")
88 .setTitle(`Nickname`)
89 .setDescription("Something went wrong and the users nickname could not be changed.")
90 .setStatus("Danger")
91 ], components: []})
92 if (dmd) await dm.delete()
93 return
94 }
95 let failed = (dmd == false && interaction.options.getString("notify") == "yes")
96 await interaction.editReply({embeds: [new generateEmojiEmbed()
97 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
98 .setTitle(`Nickname`)
pineafan6fb3e072022-05-20 19:27:23 +010099 .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 +0100100 .setStatus(failed ? "Warning" : "Success")
101 ], components: []})
102 } else {
103 await interaction.editReply({embeds: [new generateEmojiEmbed()
104 .setEmoji("PUNISH.NICKNAME.GREEN")
105 .setTitle(`Nickname`)
106 .setDescription("No changes were made")
107 .setStatus("Success")
108 ], components: []})
109 }
pineafan32767212022-03-14 21:27:39 +0000110}
111
112const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan377794f2022-04-18 19:01:01 +0100113 let member = (interaction.member as GuildMember)
114 let me = (interaction.guild.me as GuildMember)
115 let apply = (interaction.options.getMember("user") as GuildMember)
116 if (member == null || me == null || apply == null) throw "That member is not in the server"
117 let memberPos = member.roles ? member.roles.highest.position : 0
118 let mePos = me.roles ? me.roles.highest.position : 0
119 let applyPos = apply.roles ? apply.roles.highest.position : 0
120 // Check if Nucleus can change the nickname
121 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
122 // Check if Nucleus has permission to change the nickname
PineappleFan5fe720d2022-05-19 12:01:49 +0100123 if (! me.permissions.has("MANAGE_NICKNAMES")) throw "I do not have the `manage_nicknames` permission";
pineafan377794f2022-04-18 19:01:01 +0100124 // Allow the owner to change anyone's nickname
pineafan663dc472022-05-10 18:13:47 +0100125 if (member.id == interaction.guild.ownerId) return true
pineafan377794f2022-04-18 19:01:01 +0100126 // Check if the user has manage_nicknames permission
pineafan663dc472022-05-10 18:13:47 +0100127 if (! member.permissions.has("MANAGE_NICKNAMES")) throw "You do not have the `manage_nicknames` permission";
pineafane625d782022-05-09 18:04:32 +0100128 // Allow changing your own nickname
129 if (member == apply) return true
pineafan377794f2022-04-18 19:01:01 +0100130 // Check if the user is below on the role list
131 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
132 // Allow change
133 return true
pineafan32767212022-03-14 21:27:39 +0000134}
135
136export { command, callback, check };