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