blob: 0bd7849e8be75a5fe45a5acbd5250c1240db02e6 [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")
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 );
19
pineafan32767212022-03-14 21:27:39 +000020
pineafan3a02ea32022-08-11 21:35:04 +010021const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFane6ba7882023-01-18 20:41:16 +000022 const { log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
pineafan377794f2022-04-18 19:01:01 +010023 // TODO:[Modals] Replace this with a modal
pineafan02ba0232022-07-24 22:16:15 +010024 let notify = true;
25 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010026 let timedOut = false;
27 let success = false;
PineaFane6ba7882023-01-18 20:41:16 +000028 let createAppealTicket = false;
29 let firstRun = true;
30 do {
pineafan02ba0232022-07-24 22:16:15 +010031 confirmation = await new confirmationMessage(interaction)
32 .setEmoji("PUNISH.NICKNAME.RED")
33 .setTitle("Nickname")
Skyler Grey75ea9172022-08-06 10:22:23 +010034 .setDescription(
35 keyValueList({
PineaFane6ba7882023-01-18 20:41:16 +000036 user: renderUser(interaction.options.getUser("user")!),
Skyler Grey75ea9172022-08-06 10:22:23 +010037 "new nickname": `${
PineaFane6ba7882023-01-18 20:41:16 +000038 interaction.options.get("name")?.value as string ? interaction.options.get("name")?.value as string : "*No nickname*"
Skyler Grey75ea9172022-08-06 10:22:23 +010039 }`
40 }) +
PineaFane6ba7882023-01-18 20:41:16 +000041 `Are you sure you want to ${interaction.options.get("name")?.value as string ? "change" : "clear"} <@!${
Skyler Grey11236ba2022-08-08 21:13:33 +010042 (interaction.options.getMember("user") as GuildMember).id
Skyler Grey75ea9172022-08-06 10:22:23 +010043 }>'s nickname?`
44 )
pineafan02ba0232022-07-24 22:16:15 +010045 .setColor("Danger")
Skyler Grey75ea9172022-08-06 10:22:23 +010046 .addCustomBoolean(
PineaFane6ba7882023-01-18 20:41:16 +000047 "appeal",
48 "Create appeal ticket",
49 !(await areTicketsEnabled(interaction.guild!.id)),
50 async () => await create(interaction.guild!, interaction.options.getUser("user")!, interaction.user, "Nickname changed"),
51 "An appeal ticket will be created",
52 null,
53 "CONTROL.TICKET",
54 createAppealTicket
55 )
56 .addCustomBoolean(
Skyler Grey75ea9172022-08-06 10:22:23 +010057 "notify",
58 "Notify user",
59 false,
60 null,
PineaFane6ba7882023-01-18 20:41:16 +000061 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000062 null,
Skyler Grey75ea9172022-08-06 10:22:23 +010063 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
64 notify
65 )
PineaFane6ba7882023-01-18 20:41:16 +000066 .setFailedMessage("No changes were made", "Success", "PUNISH.NICKNAME.GREEN")
67 .send(!firstRun);
68 firstRun = false;
Skyler Greyad002172022-08-16 18:48:26 +010069 if (confirmation.cancelled) timedOut = true;
PineaFane6ba7882023-01-18 20:41:16 +000070 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010071 else if (confirmation.components) {
PineaFane6ba7882023-01-18 20:41:16 +000072 notify = confirmation.components['notify']!.active;
73 createAppealTicket = confirmation.components["appeal"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010074 }
PineaFane6ba7882023-01-18 20:41:16 +000075 } while (!timedOut && !success);
76 if (timedOut || !success) return;
77 let dmSent = false;
78 let dmMessage: Message;
79 const config = await client.database.guilds.read(interaction.guild!.id);
Skyler Greyad002172022-08-16 18:48:26 +010080 try {
81 if (notify) {
PineaFane6ba7882023-01-18 20:41:16 +000082 const messageData: {
83 embeds: EmojiEmbed[];
84 components: ActionRowBuilder<ButtonBuilder>[];
85 } = {
Skyler Greyad002172022-08-16 18:48:26 +010086 embeds: [
87 new EmojiEmbed()
88 .setEmoji("PUNISH.NICKNAME.RED")
89 .setTitle("Nickname changed")
90 .setDescription(
PineaFane6ba7882023-01-18 20:41:16 +000091 `Your nickname was ${interaction.options.get("name")?.value as string ? "changed" : "cleared"} in ${
92 interaction.guild!.name
Skyler Greyad002172022-08-16 18:48:26 +010093 }.` +
PineaFane6ba7882023-01-18 20:41:16 +000094 (interaction.options.get("name")?.value as string
PineappleFan429c2d82023-03-01 08:15:50 +000095 ? `/nIt is now: ${interaction.options.get("name")?.value as string}`
Skyler Greyad002172022-08-16 18:48:26 +010096 : "") +
97 "\n\n" +
PineaFane6ba7882023-01-18 20:41:16 +000098 (createAppealTicket
99 ? `You can appeal this in the ticket created in <#${confirmation.components!["appeal"]!.response}>`
Skyler Greyad002172022-08-16 18:48:26 +0100100 : "")
101 )
102 .setStatus("Danger")
PineaFane6ba7882023-01-18 20:41:16 +0000103 ], components: []
104 };
105 if (config.moderation.nick.text && config.moderation.nick.link) {
106 messageData.embeds[0]!.setFooter(LinkWarningFooter)
107 messageData.components.push(new ActionRowBuilder<ButtonBuilder>()
108 .addComponents(new ButtonBuilder()
109 .setStyle(ButtonStyle.Link)
110 .setLabel(config.moderation.nick.text)
111 .setURL(config.moderation.nick.link.replaceAll("{id}", (interaction.options.getMember("user") as GuildMember).id))
112 )
113 )
114 }
115 dmMessage = await (interaction.options.getMember("user") as GuildMember).send(messageData);
116 dmSent = true;
Skyler Greyad002172022-08-16 18:48:26 +0100117 }
118 } catch {
PineaFane6ba7882023-01-18 20:41:16 +0000119 dmSent = false;
Skyler Greyad002172022-08-16 18:48:26 +0100120 }
PineaFane6ba7882023-01-18 20:41:16 +0000121 let member: GuildMember;
122 let before: string | null;
123 let nickname: string | undefined;
Skyler Greyad002172022-08-16 18:48:26 +0100124 try {
PineaFane6ba7882023-01-18 20:41:16 +0000125 member = interaction.options.getMember("user") as GuildMember;
126 before = member.nickname;
127 nickname = interaction.options.get("name")?.value as string | undefined;
Skyler Greyad002172022-08-16 18:48:26 +0100128 member.setNickname(nickname ?? null, "Nucleus Nickname command");
129 await client.database.history.create(
130 "nickname",
PineaFane6ba7882023-01-18 20:41:16 +0000131 interaction.guild!.id,
Skyler Greyad002172022-08-16 18:48:26 +0100132 member.user,
133 interaction.user,
134 null,
135 before,
136 nickname
137 );
Skyler Greyad002172022-08-16 18:48:26 +0100138 } catch {
139 await interaction.editReply({
140 embeds: [
141 new EmojiEmbed()
142 .setEmoji("PUNISH.NICKNAME.RED")
143 .setTitle("Nickname")
144 .setDescription("Something went wrong and the users nickname could not be changed.")
145 .setStatus("Danger")
146 ],
147 components: []
148 });
PineaFane6ba7882023-01-18 20:41:16 +0000149 if (dmSent) await dmMessage!.delete();
Skyler Greyad002172022-08-16 18:48:26 +0100150 return;
151 }
PineaFane6ba7882023-01-18 20:41:16 +0000152 const data = {
153 meta: {
154 type: "memberUpdate",
155 displayName: "Member Updated",
156 calculateType: "guildMemberUpdate",
157 color: NucleusColors.yellow,
158 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500159 timestamp: Date.now()
PineaFane6ba7882023-01-18 20:41:16 +0000160 },
161 list: {
162 memberId: entry(member.id, `\`${member.id}\``),
163 before: entry(before, before ?? "*No nickname set*"),
164 after: entry(nickname ?? null, nickname ?? "*No nickname set*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500165 updated: entry(Date.now(), renderDelta(Date.now())),
PineaFane6ba7882023-01-18 20:41:16 +0000166 updatedBy: entry(interaction.user.id, renderUser(interaction.user))
167 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500168 separate: {
169 end: getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + ` The user was ${notify ? "" : "not "}notified`
170 },
PineaFane6ba7882023-01-18 20:41:16 +0000171 hidden: {
172 guild: interaction.guild!.id
173 }
174 };
175 log(data);
176 const failed = !dmSent && notify;
Skyler Greyad002172022-08-16 18:48:26 +0100177 await interaction.editReply({
178 embeds: [
179 new EmojiEmbed()
180 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
181 .setTitle("Nickname")
182 .setDescription(
183 "The members nickname was changed" +
184 (failed ? ", but was not notified" : "") +
PineaFane6ba7882023-01-18 20:41:16 +0000185 (confirmation.components!["appeal"]!.response !== null
186 ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>`
Skyler Greyad002172022-08-16 18:48:26 +0100187 : "")
188 )
189 .setStatus(failed ? "Warning" : "Success")
190 ],
191 components: []
192 });
pineafan63fc5e22022-08-04 22:04:10 +0100193};
pineafan32767212022-03-14 21:27:39 +0000194
TheCodedProff86ba092023-01-27 17:10:07 -0500195const check = async (interaction: CommandInteraction, partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500197 // Check if the user has manage_nicknames permission
198 if (!member.permissions.has("ManageNicknames")) return "You do not have the *Manage Nicknames* permission";
199 if (partial) return true;
PineaFan1dee28f2023-01-16 22:09:07 +0000200 const me = interaction.guild!.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100201 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100202 const memberPos = member.roles.cache.size ? member.roles.highest.position : 0;
203 const mePos = me.roles.cache.size ? me.roles.highest.position : 0;
204 const applyPos = apply.roles.cache.size ? apply.roles.highest.position : 0;
PineaFan1dee28f2023-01-16 22:09:07 +0000205 if (!interaction.guild) return false;
pineafanc1c18792022-08-03 21:41:36 +0100206 // Do not allow any changing of the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000207 if (member.id === interaction.guild.ownerId) return "You cannot change the owner's nickname";
pineafan377794f2022-04-18 19:01:01 +0100208 // Check if Nucleus can change the nickname
TheCodedProf0941da42023-02-18 20:28:04 -0500209 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan377794f2022-04-18 19:01:01 +0100210 // Check if Nucleus has permission to change the nickname
PineaFan0d06edc2023-01-17 22:10:31 +0000211 if (!me.permissions.has("ManageNicknames")) return "I do not have the *Manage Nicknames* permission";
pineafan377794f2022-04-18 19:01:01 +0100212 // Allow the owner to change anyone's nickname
pineafan63fc5e22022-08-04 22:04:10 +0100213 if (member.id === interaction.guild.ownerId) return true;
pineafane625d782022-05-09 18:04:32 +0100214 // Allow changing your own nickname
pineafan63fc5e22022-08-04 22:04:10 +0100215 if (member === apply) return true;
pineafan377794f2022-04-18 19:01:01 +0100216 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500217 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan377794f2022-04-18 19:01:01 +0100218 // Allow change
pineafan63fc5e22022-08-04 22:04:10 +0100219 return true;
220};
pineafan32767212022-03-14 21:27:39 +0000221
Skyler Grey75ea9172022-08-06 10:22:23 +0100222export { command, callback, check };
TheCodedProfa112f612023-01-28 18:06:45 -0500223export const metadata = {
224 longDescription: "Changes the nickname of a member. This is the name that shows in the member list and on messages.",
225 premiumOnly: true,
226}