lunarflu HF Staff commited on
Commit
716f968
·
verified ·
1 Parent(s): cbeadf4

test roles

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py CHANGED
@@ -18,6 +18,8 @@ import asyncio # check if used
18
  import logging
19
  import urllib.parse
20
 
 
 
21
  zurich_tz = timezone("Europe/Zurich")
22
 
23
  def convert_to_timezone(dt, tz):
@@ -541,6 +543,65 @@ async def on_voice_state_update(member, before, after):
541
  except Exception as e:
542
  print(f"on_voice_state_update Error: {e}")
543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
544
  # github test stuff -------------------------------------------------------------------------------------------------------------------
545
  """
546
  async def check_github():
 
18
  import logging
19
  import urllib.parse
20
 
21
+ from discord.ui import Button, View
22
+
23
  zurich_tz = timezone("Europe/Zurich")
24
 
25
  def convert_to_timezone(dt, tz):
 
543
  except Exception as e:
544
  print(f"on_voice_state_update Error: {e}")
545
 
546
+ # -------------------------------------------------------------------------------------------------------------------------------------
547
+ class RoleToggleButton(Button):
548
+ def __init__(self, role: discord.Role):
549
+ # Set the button label to the role name and use a primary style.
550
+ super().__init__(label=role.name, style=discord.ButtonStyle.primary)
551
+ self.role = role
552
+
553
+ async def callback(self, interaction: discord.Interaction):
554
+ # Check if the user already has the role
555
+ if self.role in interaction.user.roles:
556
+ try:
557
+ await interaction.user.remove_roles(self.role)
558
+ await interaction.response.send_message(
559
+ f"Removed role: {self.role.name}", ephemeral=True
560
+ )
561
+ except Exception as e:
562
+ await interaction.response.send_message(
563
+ f"Error removing role: {e}", ephemeral=True
564
+ )
565
+ else:
566
+ try:
567
+ await interaction.user.add_roles(self.role)
568
+ await interaction.response.send_message(
569
+ f"Added role: {self.role.name}", ephemeral=True
570
+ )
571
+ except Exception as e:
572
+ await interaction.response.send_message(
573
+ f"Error adding role: {e}", ephemeral=True
574
+ )
575
+
576
+ # A view that holds one toggle button per role.
577
+ class RoleSelectionView(View):
578
+ def __init__(self, roles: list):
579
+ # Setting timeout to None allows the view to persist indefinitely.
580
+ super().__init__(timeout=None)
581
+ for role in roles:
582
+ self.add_item(RoleToggleButton(role))
583
+
584
+ # Command that sends the role buttons message.
585
+ @bot.command(name="role_buttons")
586
+ async def role_buttons(ctx):
587
+ # Only allow lunarflu (ID 811235357663297546) to use this command.
588
+ if ctx.author.id != 811235357663297546:
589
+ await ctx.send("You are not authorized to use this command.", delete_after=10)
590
+ return
591
+
592
+ # List the role IDs you want to include (replace these with your actual role IDs).
593
+ role_ids = [1014517792550166630, 1014548568238997616, 1014548769355862036, 1077250031180071023, 1093982736961785877]
594
+ # Retrieve the Role objects from the guild.
595
+ roles = [ctx.guild.get_role(rid) for rid in role_ids if ctx.guild.get_role(rid) is not None]
596
+
597
+ # Create the view with buttons for each role.
598
+ view = RoleSelectionView(roles)
599
+ # Send a message with the view attached.
600
+ await ctx.send("Click the buttons below to toggle roles:", view=view)
601
+
602
+
603
+
604
+
605
  # github test stuff -------------------------------------------------------------------------------------------------------------------
606
  """
607
  async def check_github():