blob: 8b33551d41875718c1240d2fbe3b6f6f3172b8ea [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import { LinkWarningFooter } from "./../../utils/defaults.js";
PineaFane6ba7882023-01-18 20:41:16 +00002import { 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
Skyler Greyda16adf2023-03-05 10:22:12 +000011const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
13 .setName("nick")
14 .setDescription("Changes a users nickname")
15 .addUserOption((option) => option.setName("user").setDescription("The user to change").setRequired(true))
16 .addStringOption((option) =>
17 option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false)
18 );
pineafan32767212022-03-14 21:27:39 +000019
pineafan3a02ea32022-08-11 21:35:04 +010020const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFane6ba7882023-01-18 20:41:16 +000021 const { log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
pineafan377794f2022-04-18 19:01:01 +010022 // TODO:[Modals] Replace this with a modal
pineafan02ba0232022-07-24 22:16:15 +010023 let notify = true;
24 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010025 let timedOut = false;
26 let success = false;
PineaFane6ba7882023-01-18 20:41:16 +000027 let createAppealTicket = false;
28 let firstRun = true;
29 do {
pineafan02ba0232022-07-24 22:16:15 +010030 confirmation = await new confirmationMessage(interaction)
31 .setEmoji("PUNISH.NICKNAME.RED")
32 .setTitle("Nickname")
Skyler Grey75ea9172022-08-06 10:22:23 +010033 .setDescription(
34 keyValueList({
PineaFane6ba7882023-01-18 20:41:16 +000035 user: renderUser(interaction.options.getUser("user")!),
Skyler Grey75ea9172022-08-06 10:22:23 +010036 "new nickname": `${
Skyler Greyda16adf2023-03-05 10:22:12 +000037 (interaction.options.get("name")?.value as string)
38 ? (interaction.options.get("name")?.value as string)
39 : "*No nickname*"
Skyler Grey75ea9172022-08-06 10:22:23 +010040 }`
41 }) +
Skyler Greyda16adf2023-03-05 10:22:12 +000042 `Are you sure you want to ${
43 (interaction.options.get("name")?.value as string) ? "change" : "clear"
44 } <@!${(interaction.options.getMember("user") as GuildMember).id}>'s nickname?`
Skyler Grey75ea9172022-08-06 10:22:23 +010045 )
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)),
Skyler Greyda16adf2023-03-05 10:22:12 +000051 async () =>
52 await create(
53 interaction.guild!,
54 interaction.options.getUser("user")!,
55 interaction.user,
56 "Nickname changed"
57 ),
PineaFane6ba7882023-01-18 20:41:16 +000058 "An appeal ticket will be created",
59 null,
60 "CONTROL.TICKET",
61 createAppealTicket
62 )
63 .addCustomBoolean(
Skyler Grey75ea9172022-08-06 10:22:23 +010064 "notify",
65 "Notify user",
66 false,
67 null,
PineaFane6ba7882023-01-18 20:41:16 +000068 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000069 null,
Skyler Grey75ea9172022-08-06 10:22:23 +010070 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
71 notify
72 )
PineaFane6ba7882023-01-18 20:41:16 +000073 .setFailedMessage("No changes were made", "Success", "PUNISH.NICKNAME.GREEN")
74 .send(!firstRun);
75 firstRun = false;
Skyler Greyad002172022-08-16 18:48:26 +010076 if (confirmation.cancelled) timedOut = true;
PineaFane6ba7882023-01-18 20:41:16 +000077 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010078 else if (confirmation.components) {
Skyler Greyda16adf2023-03-05 10:22:12 +000079 notify = confirmation.components["notify"]!.active;
PineaFane6ba7882023-01-18 20:41:16 +000080 createAppealTicket = confirmation.components["appeal"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010081 }
PineaFane6ba7882023-01-18 20:41:16 +000082 } while (!timedOut && !success);
83 if (timedOut || !success) return;
84 let dmSent = false;
85 let dmMessage: Message;
86 const config = await client.database.guilds.read(interaction.guild!.id);
Skyler Greyad002172022-08-16 18:48:26 +010087 try {
88 if (notify) {
PineaFane6ba7882023-01-18 20:41:16 +000089 const messageData: {
90 embeds: EmojiEmbed[];
91 components: ActionRowBuilder<ButtonBuilder>[];
92 } = {
Skyler Greyad002172022-08-16 18:48:26 +010093 embeds: [
94 new EmojiEmbed()
95 .setEmoji("PUNISH.NICKNAME.RED")
96 .setTitle("Nickname changed")
97 .setDescription(
Skyler Greyda16adf2023-03-05 10:22:12 +000098 `Your nickname was ${
99 (interaction.options.get("name")?.value as string) ? "changed" : "cleared"
100 } in ${interaction.guild!.name}.` +
101 ((interaction.options.get("name")?.value as string)
PineappleFan60c02242023-03-01 08:14:40 +0000102 ? `\nIt is now: ${interaction.options.get("name")?.value as string}`
Skyler Greyad002172022-08-16 18:48:26 +0100103 : "") +
104 "\n\n" +
PineaFane6ba7882023-01-18 20:41:16 +0000105 (createAppealTicket
Skyler Greyda16adf2023-03-05 10:22:12 +0000106 ? `You can appeal this in the ticket created in <#${
107 confirmation.components!["appeal"]!.response
108 }>`
Skyler Greyad002172022-08-16 18:48:26 +0100109 : "")
110 )
111 .setStatus("Danger")
Skyler Greyda16adf2023-03-05 10:22:12 +0000112 ],
113 components: []
PineaFane6ba7882023-01-18 20:41:16 +0000114 };
115 if (config.moderation.nick.text && config.moderation.nick.link) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000116 messageData.embeds[0]!.setFooter(LinkWarningFooter);
117 messageData.components.push(
118 new ActionRowBuilder<ButtonBuilder>().addComponents(
119 new ButtonBuilder()
PineaFane6ba7882023-01-18 20:41:16 +0000120 .setStyle(ButtonStyle.Link)
121 .setLabel(config.moderation.nick.text)
Skyler Greyda16adf2023-03-05 10:22:12 +0000122 .setURL(
123 config.moderation.nick.link.replaceAll(
124 "{id}",
125 (interaction.options.getMember("user") as GuildMember).id
126 )
127 )
128 )
129 );
PineaFane6ba7882023-01-18 20:41:16 +0000130 }
131 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
132 dmSent = true;
Skyler Greyad002172022-08-16 18:48:26 +0100133 }
134 } catch {
PineaFane6ba7882023-01-18 20:41:16 +0000135 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100136 }
PineaFane6ba7882023-01-18 20:41:16 +0000137 let member: GuildMember;
138 let before: string | null;
139 let nickname: string | undefined;
Skyler Greyad002172022-08-16 18:48:26 +0100140 try {
PineaFane6ba7882023-01-18 20:41:16 +0000141 member = interaction.options.getMember("user") as GuildMember;
142 before = member.nickname;
143 nickname = interaction.options.get("name")?.value as string | undefined;
Skyler Greyad002172022-08-16 18:48:26 +0100144 member.setNickname(nickname ?? null, "Nucleus Nickname command");
145 await client.database.history.create(
146 "nickname",
PineaFane6ba7882023-01-18 20:41:16 +0000147 interaction.guild!.id,
Skyler Greyad002172022-08-16 18:48:26 +0100148 member.user,
149 interaction.user,
150 null,
151 before,
152 nickname
153 );
Skyler Greyad002172022-08-16 18:48:26 +0100154 } catch {
155 await interaction.editReply({
156 embeds: [
157 new EmojiEmbed()
158 .setEmoji("PUNISH.NICKNAME.RED")
159 .setTitle("Nickname")
160 .setDescription("Something went wrong and the users nickname could not be changed.")
161 .setStatus("Danger")
162 ],
163 components: []
164 });
PineaFane6ba7882023-01-18 20:41:16 +0000165 if (dmSent) await dmMessage!.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100166 return;
167 }
PineaFane6ba7882023-01-18 20:41:16 +0000168 const data = {
169 meta: {
170 type: "memberUpdate",
171 displayName: "Member Updated",
172 calculateType: "guildMemberUpdate",
173 color: NucleusColors.yellow,
174 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500175 timestamp: Date.now()
PineaFane6ba7882023-01-18 20:41:16 +0000176 },
177 list: {
178 memberId: entry(member.id, `\`${member.id}\``),
179 before: entry(before, before ?? "*No nickname set*"),
180 after: entry(nickname ?? null, nickname ?? "*No nickname set*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500181 updated: entry(Date.now(), renderDelta(Date.now())),
PineaFane6ba7882023-01-18 20:41:16 +0000182 updatedBy: entry(interaction.user.id, renderUser(interaction.user))
183 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500184 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000185 end:
186 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
187 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500188 },
PineaFane6ba7882023-01-18 20:41:16 +0000189 hidden: {
190 guild: interaction.guild!.id
191 }
192 };
193 log(data);
194 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100195 await interaction.editReply({
196 embeds: [
197 new EmojiEmbed()
198 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
199 .setTitle("Nickname")
200 .setDescription(
201 "The members nickname was changed" +
202 (failed ? ", but was not notified" : "") +
PineaFane6ba7882023-01-18 20:41:16 +0000203 (confirmation.components!["appeal"]!.response !== null
204 ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>`
Skyler Greyad002172022-08-16 18:48:26 +0100205 : "")
206 )
207 .setStatus(failed ? "Warning" : "Success")
208 ],
209 components: []
210 });
pineafan63fc5e22022-08-04 22:04:10 +0100211};
pineafan32767212022-03-14 21:27:39 +0000212
TheCodedProff86ba092023-01-27 17:10:07 -0500213const check = async (interaction: CommandInteraction, partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100214 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500215 // Check if the user has manage_nicknames permission
216 if (!member.permissions.has("ManageNicknames")) return "You do not have the *Manage Nicknames* permission";
217 if (partial) return true;
PineaFan1dee28f2023-01-16 22:09:07 +0000218 const me = interaction.guild!.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100219 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100220 const memberPos = member.roles.cache.size ? member.roles.highest.position : 0;
221 const mePos = me.roles.cache.size ? me.roles.highest.position : 0;
222 const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0;
PineaFan1dee28f2023-01-16 22:09:07 +0000223 if (!interaction.guild) return false;
pineafanc1c18792022-08-03 21:41:36 +0100224 // Do not allow any changing of the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000225 if (member.id === interaction.guild.ownerId) return "You cannot change the owner's nickname";
pineafan377794f2022-04-18 19:01:01 +0100226 // Check if Nucleus can change the nickname
TheCodedProf0941da42023-02-18 20:28:04 -0500227 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan377794f2022-04-18 19:01:01 +0100228 // Check if Nucleus has permission to change the nickname
PineaFan0d06edc2023-01-17 22:10:31 +0000229 if (!me.permissions.has("ManageNicknames")) return "I do not have the *Manage Nicknames* permission";
pineafan377794f2022-04-18 19:01:01 +0100230 // Allow the owner to change anyone's nickname
pineafan63fc5e22022-08-04 22:04:10 +0100231 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100232 // Allow changing your own nickname
pineafan63fc5e22022-08-04 22:04:10 +0100233 if (member === apply) return true;
pineafan377794f2022-04-18 19:01:01 +0100234 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500235 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan377794f2022-04-18 19:01:01 +0100236 // Allow change
pineafan63fc5e22022-08-04 22:04:10 +0100237 return true;
238};
pineafan32767212022-03-14 21:27:39 +0000239
Skyler Grey75ea9172022-08-06 10:22:23 +0100240export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500241export const metadata = {
Skyler Greyda16adf2023-03-05 10:22:12 +0000242 longDescription:
243 "Changes the nickname of a member. This is the name that shows in the member list and on messages.",
244 premiumOnly: true
245};