Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { |
| 2 | ActionRowBuilder, |
| 3 | APIMessageComponentEmoji, |
| 4 | ButtonBuilder, |
| 5 | ButtonInteraction, |
| 6 | ButtonStyle, |
| 7 | CommandInteraction, |
| 8 | GuildMember, |
| 9 | Role, |
| 10 | RoleSelectMenuBuilder, |
| 11 | RoleSelectMenuInteraction, |
| 12 | UserSelectMenuBuilder, |
| 13 | UserSelectMenuInteraction |
| 14 | } from "discord.js"; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 15 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
| 16 | import client from "../../utils/client.js"; |
| 17 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 18 | import { LoadingEmbed } from "../../utils/defaults.js"; |
| 19 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 20 | import listToAndMore from "../../utils/listToAndMore.js"; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 21 | |
| 22 | const { renderUser } = client.logger; |
| 23 | |
| 24 | const canEdit = (role: Role, member: GuildMember, me: GuildMember): [string, boolean] => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 25 | if (role.position >= me.roles.highest.position || role.position >= member.roles.highest.position) |
| 26 | return [`~~<@&${role.id}>~~`, false]; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 27 | return [`<@&${role.id}>`, true]; |
| 28 | }; |
| 29 | |
| 30 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 31 | builder |
| 32 | .setName("role") |
| 33 | .setDescription("Gives or removes a role from someone") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 34 | .addUserOption((option) => option.setName("user").setDescription("The user to give or remove the role from")); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 35 | |
| 36 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
| 37 | const m = await interaction.reply({ embeds: LoadingEmbed, fetchReply: true, ephemeral: true }); |
| 38 | |
| 39 | let member = interaction.options.getMember("user") as GuildMember | null; |
| 40 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 41 | if (!member) { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 42 | const memberEmbed = new EmojiEmbed() |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 43 | .setTitle("Role") |
| 44 | .setDescription(`Please choose a member to edit the roles of.`) |
| 45 | .setEmoji("GUILD.ROLES.CREATE") |
| 46 | .setStatus("Success"); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 47 | const memberChooser = new ActionRowBuilder<UserSelectMenuBuilder>().addComponents( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | new UserSelectMenuBuilder().setCustomId("memberChooser").setPlaceholder("Select a member") |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 49 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 50 | await interaction.editReply({ embeds: [memberEmbed], components: [memberChooser] }); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 51 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 52 | const filter = (i: UserSelectMenuInteraction) => |
| 53 | i.customId === "memberChooser" && i.user.id === interaction.user.id; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 54 | |
| 55 | let i: UserSelectMenuInteraction | null; |
| 56 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 57 | i = await m.awaitMessageComponent<5>({ filter, time: 300000 }); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 58 | } catch (e) { |
| 59 | return; |
| 60 | } |
| 61 | |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 62 | memberEmbed.setDescription(`Editing roles for ${renderUser(i.values[0]!)}`); |
| 63 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 64 | await interaction.editReply({ embeds: LoadingEmbed, components: [] }); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 65 | member = await interaction.guild?.members.fetch(i.values[0]!)!; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | let closed = false; |
| 69 | let rolesToChange: string[] = []; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 70 | const roleAdd = new ActionRowBuilder<RoleSelectMenuBuilder>().addComponents( |
| 71 | new RoleSelectMenuBuilder().setCustomId("roleAdd").setPlaceholder("Select a role to add").setMaxValues(25) |
| 72 | ); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 73 | |
| 74 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 75 | const removing = rolesToChange |
| 76 | .filter((r) => member!.roles.cache.has(r)) |
| 77 | .map( |
| 78 | (r) => |
| 79 | canEdit( |
| 80 | interaction.guild?.roles.cache.get(r)!, |
| 81 | interaction.member as GuildMember, |
| 82 | interaction.guild?.members.me! |
| 83 | )[0] |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 84 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 85 | const adding = rolesToChange |
| 86 | .filter((r) => !member!.roles.cache.has(r)) |
| 87 | .map( |
| 88 | (r) => |
| 89 | canEdit( |
| 90 | interaction.guild?.roles.cache.get(r)!, |
| 91 | interaction.member as GuildMember, |
| 92 | interaction.guild?.members.me! |
| 93 | )[0] |
| 94 | ); |
| 95 | const embed = new EmojiEmbed() |
| 96 | .setTitle("Role") |
| 97 | .setDescription( |
| 98 | `${getEmojiByName("ICONS.EDIT")} Editing roles for <@${member.id}>\n\n` + |
| 99 | `Adding:\n` + |
| 100 | `${listToAndMore(adding.length > 0 ? adding : ["None"], 5)}\n` + |
| 101 | `Removing:\n` + |
| 102 | `${listToAndMore(removing.length > 0 ? removing : ["None"], 5)}\n` |
| 103 | ) |
| 104 | .setEmoji("GUILD.ROLES.CREATE") |
| 105 | .setStatus("Success"); |
| 106 | |
| 107 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 108 | new ButtonBuilder() |
| 109 | .setCustomId("roleSave") |
| 110 | .setLabel("Apply") |
| 111 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 112 | .setStyle(ButtonStyle.Success), |
| 113 | new ButtonBuilder() |
| 114 | .setCustomId("roleDiscard") |
| 115 | .setLabel("Reset") |
| 116 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id") as APIMessageComponentEmoji) |
| 117 | .setStyle(ButtonStyle.Danger) |
| 118 | ); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 119 | |
| 120 | await interaction.editReply({ embeds: [embed], components: [roleAdd, buttons] }); |
| 121 | |
| 122 | let i: RoleSelectMenuInteraction | ButtonInteraction | null; |
| 123 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 124 | i = (await m.awaitMessageComponent({ filter: (i) => i.user.id === interaction.user.id, time: 300000 })) as |
| 125 | | RoleSelectMenuInteraction |
| 126 | | ButtonInteraction; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 127 | } catch (e) { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 128 | closed = true; |
| 129 | continue; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 132 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 133 | if (i.isButton()) { |
| 134 | switch (i.customId) { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 135 | case "roleSave": { |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 136 | const roles = rolesToChange.map((r) => interaction.guild?.roles.cache.get(r)!); |
| 137 | await interaction.editReply({ embeds: LoadingEmbed, components: [] }); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 138 | const rolesToAdd: Role[] = []; |
| 139 | const rolesToRemove: Role[] = []; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 140 | for (const role of roles) { |
| 141 | if (!canEdit(role, interaction.member as GuildMember, interaction.guild?.members.me!)[1]) |
| 142 | continue; |
| 143 | if (member.roles.cache.has(role.id)) { |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 144 | rolesToRemove.push(role); |
| 145 | } else { |
| 146 | rolesToAdd.push(role); |
| 147 | } |
| 148 | } |
| 149 | await member.roles.add(rolesToAdd); |
| 150 | await member.roles.remove(rolesToRemove); |
| 151 | rolesToChange = []; |
| 152 | break; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 153 | } |
| 154 | case "roleDiscard": { |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 155 | rolesToChange = []; |
| 156 | await interaction.editReply({ embeds: LoadingEmbed, components: [] }); |
| 157 | break; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 158 | } |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 159 | } |
| 160 | } else { |
| 161 | rolesToChange = i.values; |
| 162 | } |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 163 | } while (!closed); |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
| 167 | const member = interaction.member as GuildMember; |
| 168 | // Check if the user has manage_roles permission |
| 169 | if (!member.permissions.has("ManageRoles")) return "You do not have the *Manage Roles* permission"; |
| 170 | if (partial) return true; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 171 | if (!interaction.guild) return; |
TheCodedProf | 4a958a1 | 2023-02-14 13:41:51 -0500 | [diff] [blame] | 172 | const me = interaction.guild.members.me!; |
| 173 | // Check if Nucleus has permission to role |
| 174 | if (!me.permissions.has("ManageRoles")) return "I do not have the *Manage Roles* permission"; |
| 175 | // Allow the owner to role anyone |
| 176 | if (member.id === interaction.guild.ownerId) return true; |
| 177 | // Allow role |
| 178 | return true; |
| 179 | }; |
| 180 | |
| 181 | export { command }; |
| 182 | export { callback }; |
| 183 | export { check }; |