blob: ae4f44621a0788f69ee256697e98efc6a3d2eeea [file] [log] [blame]
PineaFane6ba7882023-01-18 20:41:16 +00001import { LinkWarningFooter } from './../../utils/defaults.js';
2import { ActionRowBuilder, ButtonBuilder, CommandInteraction, GuildMember, ButtonStyle, Message } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05003import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan32767212022-03-14 21:27:39 +00004import 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";
pineafan63fc5e22022-08-04 22:04:10 +01007import client from "../../utils/client.js";
PineaFane6ba7882023-01-18 20:41:16 +00008import { areTicketsEnabled, create } from "../../actions/createModActionTicket.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -05009import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan32767212022-03-14 21:27:39 +000010
PineaFan64486c42022-12-28 09:21:04 +000011
12const command = (builder: SlashCommandSubcommandBuilder) => builder
13 .setName("nick")
PineaFan538d3752023-01-12 21:48:23 +000014 // .setNameLocalizations({"ru": "name", "zh-CN": "nickname"})
PineaFan64486c42022-12-28 09:21:04 +000015 .setDescription("Changes a users nickname")
16 .addUserOption((option) => option.setName("user").setDescription("The user to change").setRequired(true))
17 .addStringOption((option) =>
18 option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false)
19 );
20
pineafan32767212022-03-14 21:27:39 +000021
pineafan3a02ea32022-08-11 21:35:04 +010022const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFane6ba7882023-01-18 20:41:16 +000023 const { log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
pineafan377794f2022-04-18 19:01:01 +010024 // TODO:[Modals] Replace this with a modal
pineafan02ba0232022-07-24 22:16:15 +010025 let notify = true;
26 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010027 let timedOut = false;
28 let success = false;
PineaFane6ba7882023-01-18 20:41:16 +000029 let createAppealTicket = false;
30 let firstRun = true;
31 do {
pineafan02ba0232022-07-24 22:16:15 +010032 confirmation = await new confirmationMessage(interaction)
33 .setEmoji("PUNISH.NICKNAME.RED")
34 .setTitle("Nickname")
Skyler Grey75ea9172022-08-06 10:22:23 +010035 .setDescription(
36 keyValueList({
PineaFane6ba7882023-01-18 20:41:16 +000037 user: renderUser(interaction.options.getUser("user")!),
Skyler Grey75ea9172022-08-06 10:22:23 +010038 "new nickname": `${
PineaFane6ba7882023-01-18 20:41:16 +000039 interaction.options.get("name")?.value as string ? interaction.options.get("name")?.value as string : "*No nickname*"
Skyler Grey75ea9172022-08-06 10:22:23 +010040 }`
41 }) +
PineaFane6ba7882023-01-18 20:41:16 +000042 `Are you sure you want to ${interaction.options.get("name")?.value as string ? "change" : "clear"} <@!${
Skyler Grey11236ba2022-08-08 21:13:33 +010043 (interaction.options.getMember("user") as GuildMember).id
Skyler Grey75ea9172022-08-06 10:22:23 +010044 }>'s nickname?`
45 )
pineafan02ba0232022-07-24 22:16:15 +010046 .setColor("Danger")
Skyler Grey75ea9172022-08-06 10:22:23 +010047 .addCustomBoolean(
PineaFane6ba7882023-01-18 20:41:16 +000048 "appeal",
49 "Create appeal ticket",
50 !(await areTicketsEnabled(interaction.guild!.id)),
51 async () => await create(interaction.guild!, interaction.options.getUser("user")!, interaction.user, "Nickname changed"),
52 "An appeal ticket will be created",
53 null,
54 "CONTROL.TICKET",
55 createAppealTicket
56 )
57 .addCustomBoolean(
Skyler Grey75ea9172022-08-06 10:22:23 +010058 "notify",
59 "Notify user",
60 false,
61 null,
PineaFane6ba7882023-01-18 20:41:16 +000062 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000063 null,
Skyler Grey75ea9172022-08-06 10:22:23 +010064 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
65 notify
66 )
PineaFane6ba7882023-01-18 20:41:16 +000067 .setFailedMessage("No changes were made", "Success", "PUNISH.NICKNAME.GREEN")
68 .send(!firstRun);
69 firstRun = false;
Skyler Greyad002172022-08-16 18:48:26 +010070 if (confirmation.cancelled) timedOut = true;
PineaFane6ba7882023-01-18 20:41:16 +000071 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010072 else if (confirmation.components) {
PineaFane6ba7882023-01-18 20:41:16 +000073 notify = confirmation.components['notify']!.active;
74 createAppealTicket = confirmation.components["appeal"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010075 }
PineaFane6ba7882023-01-18 20:41:16 +000076 } while (!timedOut && !success);
77 if (timedOut || !success) return;
78 let dmSent = false;
79 let dmMessage: Message;
80 const config = await client.database.guilds.read(interaction.guild!.id);
Skyler Greyad002172022-08-16 18:48:26 +010081 try {
82 if (notify) {
PineaFane6ba7882023-01-18 20:41:16 +000083 const messageData: {
84 embeds: EmojiEmbed[];
85 components: ActionRowBuilder<ButtonBuilder>[];
86 } = {
Skyler Greyad002172022-08-16 18:48:26 +010087 embeds: [
88 new EmojiEmbed()
89 .setEmoji("PUNISH.NICKNAME.RED")
90 .setTitle("Nickname changed")
91 .setDescription(
PineaFane6ba7882023-01-18 20:41:16 +000092 `Your nickname was ${interaction.options.get("name")?.value as string ? "changed" : "cleared"} in ${
93 interaction.guild!.name
Skyler Greyad002172022-08-16 18:48:26 +010094 }.` +
PineaFane6ba7882023-01-18 20:41:16 +000095 (interaction.options.get("name")?.value as string
PineappleFan429c2d82023-03-01 08:15:50 +000096 ? `/nIt is now: ${interaction.options.get("name")?.value as string}`
Skyler Greyad002172022-08-16 18:48:26 +010097 : "") +
98 "\n\n" +
PineaFane6ba7882023-01-18 20:41:16 +000099 (createAppealTicket
100 ? `You can appeal this in the ticket created in <#${confirmation.components!["appeal"]!.response}>`
Skyler Greyad002172022-08-16 18:48:26 +0100101 : "")
102 )
103 .setStatus("Danger")
PineaFane6ba7882023-01-18 20:41:16 +0000104 ], components: []
105 };
106 if (config.moderation.nick.text && config.moderation.nick.link) {
107 messageData.embeds[0]!.setFooter(LinkWarningFooter)
108 messageData.components.push(new ActionRowBuilder<ButtonBuilder>()
109 .addComponents(new ButtonBuilder()
110 .setStyle(ButtonStyle.Link)
111 .setLabel(config.moderation.nick.text)
112 .setURL(config.moderation.nick.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
113 )
114 )
115 }
116 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
117 dmSent = true;
Skyler Greyad002172022-08-16 18:48:26 +0100118 }
119 } catch {
PineaFane6ba7882023-01-18 20:41:16 +0000120 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100121 }
PineaFane6ba7882023-01-18 20:41:16 +0000122 let member: GuildMember;
123 let before: string | null;
124 let nickname: string | undefined;
Skyler Greyad002172022-08-16 18:48:26 +0100125 try {
PineaFane6ba7882023-01-18 20:41:16 +0000126 member = interaction.options.getMember("user") as GuildMember;
127 before = member.nickname;
128 nickname = interaction.options.get("name")?.value as string | undefined;
Skyler Greyad002172022-08-16 18:48:26 +0100129 member.setNickname(nickname ?? null, "Nucleus Nickname command");
130 await client.database.history.create(
131 "nickname",
PineaFane6ba7882023-01-18 20:41:16 +0000132 interaction.guild!.id,
Skyler Greyad002172022-08-16 18:48:26 +0100133 member.user,
134 interaction.user,
135 null,
136 before,
137 nickname
138 );
Skyler Greyad002172022-08-16 18:48:26 +0100139 } catch {
140 await interaction.editReply({
141 embeds: [
142 new EmojiEmbed()
143 .setEmoji("PUNISH.NICKNAME.RED")
144 .setTitle("Nickname")
145 .setDescription("Something went wrong and the users nickname could not be changed.")
146 .setStatus("Danger")
147 ],
148 components: []
149 });
PineaFane6ba7882023-01-18 20:41:16 +0000150 if (dmSent) await dmMessage!.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100151 return;
152 }
PineaFane6ba7882023-01-18 20:41:16 +0000153 const data = {
154 meta: {
155 type: "memberUpdate",
156 displayName: "Member Updated",
157 calculateType: "guildMemberUpdate",
158 color: NucleusColors.yellow,
159 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500160 timestamp: Date.now()
PineaFane6ba7882023-01-18 20:41:16 +0000161 },
162 list: {
163 memberId: entry(member.id, `\`${member.id}\``),
164 before: entry(before, before ?? "*No nickname set*"),
165 after: entry(nickname ?? null, nickname ?? "*No nickname set*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500166 updated: entry(Date.now(), renderDelta(Date.now())),
PineaFane6ba7882023-01-18 20:41:16 +0000167 updatedBy: entry(interaction.user.id, renderUser(interaction.user))
168 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500169 separate: {
170 end: getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + ` The user was ${notify ? "" : "not "}notified`
171 },
PineaFane6ba7882023-01-18 20:41:16 +0000172 hidden: {
173 guild: interaction.guild!.id
174 }
175 };
176 log(data);
177 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100178 await interaction.editReply({
179 embeds: [
180 new EmojiEmbed()
181 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
182 .setTitle("Nickname")
183 .setDescription(
184 "The members nickname was changed" +
185 (failed ? ", but was not notified" : "") +
PineaFane6ba7882023-01-18 20:41:16 +0000186 (confirmation.components!["appeal"]!.response !== null
187 ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>`
Skyler Greyad002172022-08-16 18:48:26 +0100188 : "")
189 )
190 .setStatus(failed ? "Warning" : "Success")
191 ],
192 components: []
193 });
pineafan63fc5e22022-08-04 22:04:10 +0100194};
pineafan32767212022-03-14 21:27:39 +0000195
TheCodedProff86ba092023-01-27 17:10:07 -0500196const check = async (interaction: CommandInteraction, partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100197 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500198 // Check if the user has manage_nicknames permission
199 if (!member.permissions.has("ManageNicknames")) return "You do not have the *Manage Nicknames* permission";
200 if (partial) return true;
PineaFan1dee28f2023-01-16 22:09:07 +0000201 const me = interaction.guild!.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100202 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100203 const memberPos = member.roles.cache.size ? member.roles.highest.position : 0;
204 const mePos = me.roles.cache.size ? me.roles.highest.position : 0;
205 const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0;
PineaFan1dee28f2023-01-16 22:09:07 +0000206 if (!interaction.guild) return false;
pineafanc1c18792022-08-03 21:41:36 +0100207 // Do not allow any changing of the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000208 if (member.id === interaction.guild.ownerId) return "You cannot change the owner's nickname";
pineafan377794f2022-04-18 19:01:01 +0100209 // Check if Nucleus can change the nickname
TheCodedProf0941da42023-02-18 20:28:04 -0500210 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan377794f2022-04-18 19:01:01 +0100211 // Check if Nucleus has permission to change the nickname
PineaFan0d06edc2023-01-17 22:10:31 +0000212 if (!me.permissions.has("ManageNicknames")) return "I do not have the *Manage Nicknames* permission";
pineafan377794f2022-04-18 19:01:01 +0100213 // Allow the owner to change anyone's nickname
pineafan63fc5e22022-08-04 22:04:10 +0100214 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100215 // Allow changing your own nickname
pineafan63fc5e22022-08-04 22:04:10 +0100216 if (member === apply) return true;
pineafan377794f2022-04-18 19:01:01 +0100217 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500218 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan377794f2022-04-18 19:01:01 +0100219 // Allow change
pineafan63fc5e22022-08-04 22:04:10 +0100220 return true;
221};
pineafan32767212022-03-14 21:27:39 +0000222
Skyler Grey75ea9172022-08-06 10:22:23 +0100223export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500224export const metadata = {
225 longDescription: "Changes the nickname of a member. This is the name that shows in the member list and on messages.",
226 premiumOnly: true,
227}