Role selects now view user specific roles for adding and removing

This commit is contained in:
fanyx 2024-01-23 15:56:01 +01:00
parent d84845b79e
commit a7183a2ee4
1 changed files with 44 additions and 22 deletions

View File

@ -8,12 +8,12 @@ class AddRoleSelect(Select):
if role not in interaction.user.roles if role not in interaction.user.roles
] ]
print(added_roles)
for role in added_roles: for role in added_roles:
await interaction.user.add_roles(role) await interaction.user.add_roles(role)
await interaction.response.send_message( await interaction.response.send_message(
ephemeral=True, ephemeral=True,
delete_after=30,
content="Added Roles: " + ", ".join([ role.mention for role in added_roles ]) content="Added Roles: " + ", ".join([ role.mention for role in added_roles ])
) )
@ -29,17 +29,14 @@ class RemoveRoleSelect(Select):
await interaction.response.send_message( await interaction.response.send_message(
ephemeral=True, ephemeral=True,
delete_after=30,
content="Removed Roles: " + ", ".join([ role.mention for role in removed_roles ]) content="Removed Roles: " + ", ".join([ role.mention for role in removed_roles ])
) )
class AddRoleButton(Button): class AddRoleButton(Button):
async def callback(self, interaction: Interaction): async def callback(self, interaction: Interaction):
view=View() view=View()
view.add_item(
AddRoleSelect(
min_values=0,
max_values=len(interaction.client.config['roles']) if len(interaction.client.config['roles']) <= 25 else 25,
placeholder="Select roles to add:",
options = [ options = [
SelectOption( SelectOption(
label=f"@{interaction.guild.get_role(role['id']).name}", label=f"@{interaction.guild.get_role(role['id']).name}",
@ -48,23 +45,33 @@ class AddRoleButton(Button):
emoji=interaction.client.get_emoji(role['emoji']) emoji=interaction.client.get_emoji(role['emoji'])
) )
for role in interaction.client.config['roles'] for role in interaction.client.config['roles']
if role['id'] in [ role.id for role in list(interaction.guild.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(options) if len(options) <= 25 else 25,
placeholder="Select roles to add:",
options = options
) )
) )
await interaction.response.send_message( await interaction.response.send_message(
ephemeral=True, ephemeral=True,
delete_after=30,
view=view view=view
) )
class RemoveRoleButton(Button): class RemoveRoleButton(Button):
async def callback(self, interaction: Interaction): async def callback(self, interaction: Interaction):
view=View() view=View()
view.add_item(
RemoveRoleSelect(
min_values=0,
max_values=len(interaction.client.config['roles']) if len(interaction.client.config['roles']) <= 25 else 25,
placeholder="Select roles to remove:",
options = [ options = [
SelectOption( SelectOption(
label=f"@{interaction.guild.get_role(role['id']).name}", label=f"@{interaction.guild.get_role(role['id']).name}",
@ -73,10 +80,25 @@ class RemoveRoleButton(Button):
emoji=interaction.client.get_emoji(role['emoji']) emoji=interaction.client.get_emoji(role['emoji'])
) )
for role in interaction.client.config['roles'] 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(options) if len(options) <= 25 else 25,
placeholder="Select roles to remove:",
options = options
) )
) )
await interaction.response.send_message( await interaction.response.send_message(
ephemeral=True, ephemeral=True,
delete_after=30,
view=view view=view
) )