From a7183a2ee4475f044a8da346f3ddb0234db262b0 Mon Sep 17 00:00:00 2001 From: fanyx Date: Tue, 23 Jan 2024 15:56:01 +0100 Subject: [PATCH] Role selects now view user specific roles for adding and removing --- src/roles.py | 66 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/src/roles.py b/src/roles.py index ccf8d68..4f99442 100644 --- a/src/roles.py +++ b/src/roles.py @@ -8,12 +8,12 @@ class AddRoleSelect(Select): if role not in interaction.user.roles ] - print(added_roles) for role in added_roles: await interaction.user.add_roles(role) await interaction.response.send_message( ephemeral=True, + delete_after=30, content="Added Roles: " + ", ".join([ role.mention for role in added_roles ]) ) @@ -29,54 +29,76 @@ class RemoveRoleSelect(Select): await interaction.response.send_message( ephemeral=True, + delete_after=30, content="Removed Roles: " + ", ".join([ role.mention for role in removed_roles ]) ) class AddRoleButton(Button): async def callback(self, interaction: Interaction): view=View() + + options = [ + SelectOption( + label=f"@{interaction.guild.get_role(role['id']).name}", + value=role['id'], + description=role['description'], + emoji=interaction.client.get_emoji(role['emoji']) + ) + for role in interaction.client.config['roles'] + if role['id'] not in [ role.id for role in list(interaction.user.roles) ] + ] + + if options == []: + await interaction.response.send_message( + content="You already own all available roles." + ) + return + view.add_item( AddRoleSelect( min_values=0, - max_values=len(interaction.client.config['roles']) if len(interaction.client.config['roles']) <= 25 else 25, + max_values=len(options) if len(options) <= 25 else 25, placeholder="Select roles to add:", - options = [ - SelectOption( - label=f"@{interaction.guild.get_role(role['id']).name}", - value=role['id'], - description=role['description'], - emoji=interaction.client.get_emoji(role['emoji']) - ) - for role in interaction.client.config['roles'] - if role['id'] in [ role.id for role in list(interaction.guild.roles) ] - ] + options = options ) ) await interaction.response.send_message( ephemeral=True, + delete_after=30, view=view ) class RemoveRoleButton(Button): async def callback(self, interaction: Interaction): view=View() + + options = [ + SelectOption( + label=f"@{interaction.guild.get_role(role['id']).name}", + value=role['id'], + description=role['description'], + emoji=interaction.client.get_emoji(role['emoji']) + ) + for role in interaction.client.config['roles'] + if role['id'] in [ role.id for role in list(interaction.user.roles) ] + ] + + if options == []: + await interaction.response.send_message( + content="You own none of the available roles." + ) + return + view.add_item( RemoveRoleSelect( min_values=0, - max_values=len(interaction.client.config['roles']) if len(interaction.client.config['roles']) <= 25 else 25, + max_values=len(options) if len(options) <= 25 else 25, placeholder="Select roles to remove:", - options = [ - SelectOption( - label=f"@{interaction.guild.get_role(role['id']).name}", - value=role['id'], - description=role['description'], - emoji=interaction.client.get_emoji(role['emoji']) - ) - for role in interaction.client.config['roles'] - ] + options = options ) ) await interaction.response.send_message( ephemeral=True, + delete_after=30, view=view )