blob: 06fab4780f0e0dcc5efbf654e54af1797f991863 [file] [log] [blame]
pineafanbd02b4a2022-08-05 22:01:38 +01001import { CommandInteraction, GuildMember } from "discord.js";
pineafan32767212022-03-14 21:27:39 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan32767212022-03-14 21:27:39 +00003import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan32767212022-03-14 21:27:39 +00005import keyValueList from "../../utils/generateKeyValueList.js";
pineafan63fc5e22022-08-04 22:04:10 +01006import client from "../../utils/client.js";
pineafan32767212022-03-14 21:27:39 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan63fc5e22022-08-04 22:04:10 +010010 .setName("nick")
11 .setDescription("Changes a users nickname")
12 .addUserOption(option => option.setName("user").setDescription("The user to change").setRequired(true))
13 .addStringOption(option => option.setName("name").setDescription("The name to set | Leave blank to clear").setRequired(false));
pineafan32767212022-03-14 21:27:39 +000014
pineafanbd02b4a2022-08-05 22:01:38 +010015const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010016 const { renderUser } = client.logger;
pineafan377794f2022-04-18 19:01:01 +010017 // TODO:[Modals] Replace this with a modal
pineafan02ba0232022-07-24 22:16:15 +010018 let notify = true;
19 let confirmation;
20 while (true) {
21 confirmation = await new confirmationMessage(interaction)
22 .setEmoji("PUNISH.NICKNAME.RED")
23 .setTitle("Nickname")
24 .setDescription(keyValueList({
25 "user": renderUser(interaction.options.getUser("user")),
26 "new nickname": `${interaction.options.getString("name") ? interaction.options.getString("name") : "*No nickname*"}`
27 })
pineafan63fc5e22022-08-04 22:04:10 +010028 + `The user **will${notify ? "" : " not"}** be notified\n\n`
pineafan02ba0232022-07-24 22:16:15 +010029 + `Are you sure you want to ${interaction.options.getString("name") ? "change" : "clear"} <@!${(interaction.options.getMember("user") as GuildMember).id}>'s nickname?`)
30 .setColor("Danger")
31 .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify)
pineafan63fc5e22022-08-04 22:04:10 +010032 .send(interaction.options.getString("name") !== null);
33 if (confirmation.cancelled) return;
34 if (confirmation.success) break;
pineafan02ba0232022-07-24 22:16:15 +010035 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010036 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010037 }
38 }
pineafan377794f2022-04-18 19:01:01 +010039 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010040 let dmd = false;
pineafan377794f2022-04-18 19:01:01 +010041 let dm;
42 try {
pineafan02ba0232022-07-24 22:16:15 +010043 if (notify) {
pineafan377794f2022-04-18 19:01:01 +010044 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010045 embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010046 .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" +
pineafan63fc5e22022-08-04 22:04:10 +010050 (confirmation.components.appeal.response ? `You can appeal this here: <#${confirmation.components.appeal.response}>` : ""))
pineafan377794f2022-04-18 19:01:01 +010051 .setStatus("Danger")
52 ]
pineafan63fc5e22022-08-04 22:04:10 +010053 });
54 dmd = true;
pineafan377794f2022-04-18 19:01:01 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 } catch { dmd = false; }
pineafan377794f2022-04-18 19:01:01 +010057 try {
pineafan63fc5e22022-08-04 22:04:10 +010058 const member = (interaction.options.getMember("user") as GuildMember);
59 const before = member.nickname;
60 const nickname = interaction.options.getString("name");
61 member.setNickname(nickname ?? null, "Nucleus Nickname command");
62 await client.database.history.create(
pineafan4edb7762022-06-26 19:21:04 +010063 "nickname", interaction.guild.id, member.user, interaction.user,
pineafan63fc5e22022-08-04 22:04:10 +010064 null, before, nickname);
65 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
66 const data = {
pineafane625d782022-05-09 18:04:32 +010067 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010068 type: "memberUpdate",
69 displayName: "Member Updated",
70 calculateType: "guildMemberUpdate",
pineafane625d782022-05-09 18:04:32 +010071 color: NucleusColors.yellow,
72 emoji: "PUNISH.NICKNAME.YELLOW",
73 timestamp: new Date().getTime()
74 },
75 list: {
pineafanda6e5342022-07-03 10:03:16 +010076 memberId: entry(member.id, `\`${member.id}\``),
pineafan63fc5e22022-08-04 22:04:10 +010077 before: entry(before, before ? before : "*None*"),
78 after: entry(nickname, nickname ? nickname : "*None*"),
pineafane625d782022-05-09 18:04:32 +010079 updated: entry(new Date().getTime(), renderDelta(new Date().getTime())),
80 updatedBy: entry(interaction.user.id, renderUser(interaction.user))
81 },
82 hidden: {
83 guild: interaction.guild.id
84 }
pineafan63fc5e22022-08-04 22:04:10 +010085 };
pineafan4edb7762022-06-26 19:21:04 +010086 log(data);
pineafan377794f2022-04-18 19:01:01 +010087 } catch {
pineafan4edb7762022-06-26 19:21:04 +010088 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010089 .setEmoji("PUNISH.NICKNAME.RED")
pineafan63fc5e22022-08-04 22:04:10 +010090 .setTitle("Nickname")
pineafan377794f2022-04-18 19:01:01 +010091 .setDescription("Something went wrong and the users nickname could not be changed.")
92 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010093 ], components: []});
94 if (dmd) await dm.delete();
95 return;
pineafan377794f2022-04-18 19:01:01 +010096 }
pineafan63fc5e22022-08-04 22:04:10 +010097 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +010098 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +010099 .setEmoji(`PUNISH.NICKNAME.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +0100100 .setTitle("Nickname")
101 .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 +0100102 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +0100103 ], components: []});
pineafan377794f2022-04-18 19:01:01 +0100104 } else {
pineafan4edb7762022-06-26 19:21:04 +0100105 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan377794f2022-04-18 19:01:01 +0100106 .setEmoji("PUNISH.NICKNAME.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100107 .setTitle("Nickname")
pineafan377794f2022-04-18 19:01:01 +0100108 .setDescription("No changes were made")
109 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100110 ], components: []});
pineafan377794f2022-04-18 19:01:01 +0100111 }
pineafan63fc5e22022-08-04 22:04:10 +0100112};
pineafan32767212022-03-14 21:27:39 +0000113
pineafanbd02b4a2022-08-05 22:01:38 +0100114const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +0100115 const member = (interaction.member as GuildMember);
116 const me = (interaction.guild.me as GuildMember);
117 const apply = (interaction.options.getMember("user") as GuildMember);
118 if (member === null || me === null || apply === null) throw "That member is not in the server";
119 const memberPos = member.roles ? member.roles.highest.position : 0;
120 const mePos = me.roles ? me.roles.highest.position : 0;
121 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100122 // Do not allow any changing of the owner
pineafan63fc5e22022-08-04 22:04:10 +0100123 if (member.id === interaction.guild.ownerId) throw "You cannot change the owner's nickname";
pineafan377794f2022-04-18 19:01:01 +0100124 // Check if Nucleus can change the nickname
pineafan63fc5e22022-08-04 22:04:10 +0100125 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan377794f2022-04-18 19:01:01 +0100126 // Check if Nucleus has permission to change the nickname
pineafane23c4ec2022-07-27 21:56:27 +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
pineafan63fc5e22022-08-04 22:04:10 +0100129 if (member.id === interaction.guild.ownerId) return true;
pineafan377794f2022-04-18 19:01:01 +0100130 // Check if the user has manage_nicknames permission
pineafane23c4ec2022-07-27 21:56:27 +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
pineafan63fc5e22022-08-04 22:04:10 +0100133 if (member === apply) return true;
pineafan377794f2022-04-18 19:01:01 +0100134 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100135 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan377794f2022-04-18 19:01:01 +0100136 // Allow change
pineafan63fc5e22022-08-04 22:04:10 +0100137 return true;
138};
pineafan32767212022-03-14 21:27:39 +0000139
140export { command, callback, check };