Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
make role buttons persistent across restarts
Browse files
app.py
CHANGED
|
@@ -544,14 +544,20 @@ async def on_voice_state_update(member, before, after):
|
|
| 544 |
print(f"on_voice_state_update Error: {e}")
|
| 545 |
|
| 546 |
# -------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
| 547 |
class RoleToggleButton(Button):
|
| 548 |
def __init__(self, role: discord.Role):
|
| 549 |
-
#
|
| 550 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -573,11 +579,12 @@ class RoleToggleButton(Button):
|
|
| 573 |
f"Error adding role: {e}", ephemeral=True
|
| 574 |
)
|
| 575 |
|
| 576 |
-
#
|
| 577 |
-
class
|
| 578 |
def __init__(self, roles: list):
|
| 579 |
-
#
|
| 580 |
super().__init__(timeout=None)
|
|
|
|
| 581 |
for role in roles:
|
| 582 |
self.add_item(RoleToggleButton(role))
|
| 583 |
|
|
@@ -591,17 +598,18 @@ async def role_buttons(ctx):
|
|
| 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
|
| 598 |
-
view =
|
| 599 |
-
# Send
|
| 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():
|
|
@@ -627,6 +635,13 @@ async def on_ready():
|
|
| 627 |
await asyncio.sleep(5)
|
| 628 |
print(bot.log_channel)
|
| 629 |
guild = bot.get_guild(879548962464493619)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
for channel in guild.text_channels: # helps with more accurate logging across restarts
|
| 631 |
try:
|
| 632 |
message_cache.update({m.id: m async for m in channel.history(limit=10000)})
|
|
@@ -635,6 +650,11 @@ async def on_ready():
|
|
| 635 |
print(f"An error occurred while fetching messages from {channel.name}: {e}")
|
| 636 |
await asyncio.sleep(0.1)
|
| 637 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
|
| 639 |
def run_bot():
|
| 640 |
bot.run(DISCORD_TOKEN)
|
|
|
|
| 544 |
print(f"on_voice_state_update Error: {e}")
|
| 545 |
|
| 546 |
# -------------------------------------------------------------------------------------------------------------------------------------
|
| 547 |
+
|
| 548 |
+
# Custom persistent button that toggles a role for the clicking user.
|
| 549 |
class RoleToggleButton(Button):
|
| 550 |
def __init__(self, role: discord.Role):
|
| 551 |
+
# Assign a fixed custom_id that uniquely identifies this button.
|
| 552 |
+
# Ensure that the custom ID is unique among all buttons in your bot.
|
| 553 |
+
super().__init__(
|
| 554 |
+
label=role.name,
|
| 555 |
+
style=discord.ButtonStyle.primary,
|
| 556 |
+
custom_id=f"persistent_role_toggle_{role.id}"
|
| 557 |
+
)
|
| 558 |
self.role = role
|
| 559 |
|
| 560 |
async def callback(self, interaction: discord.Interaction):
|
|
|
|
| 561 |
if self.role in interaction.user.roles:
|
| 562 |
try:
|
| 563 |
await interaction.user.remove_roles(self.role)
|
|
|
|
| 579 |
f"Error adding role: {e}", ephemeral=True
|
| 580 |
)
|
| 581 |
|
| 582 |
+
# Persistent view that holds one toggle button per role.
|
| 583 |
+
class PersistentRoleSelectionView(View):
|
| 584 |
def __init__(self, roles: list):
|
| 585 |
+
# Set timeout to None to keep the view indefinitely active.
|
| 586 |
super().__init__(timeout=None)
|
| 587 |
+
# Create a button for each role
|
| 588 |
for role in roles:
|
| 589 |
self.add_item(RoleToggleButton(role))
|
| 590 |
|
|
|
|
| 598 |
|
| 599 |
# List the role IDs you want to include (replace these with your actual role IDs).
|
| 600 |
role_ids = [1014517792550166630, 1014548568238997616, 1014548769355862036, 1077250031180071023, 1093982736961785877]
|
|
|
|
| 601 |
roles = [ctx.guild.get_role(rid) for rid in role_ids if ctx.guild.get_role(rid) is not None]
|
| 602 |
|
| 603 |
+
# Create the persistent view.
|
| 604 |
+
view = PersistentRoleSelectionView(roles)
|
| 605 |
+
# Send the message with the persistent view attached.
|
| 606 |
await ctx.send("Click the buttons below to toggle roles:", view=view)
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
+
|
| 612 |
+
|
| 613 |
# github test stuff -------------------------------------------------------------------------------------------------------------------
|
| 614 |
"""
|
| 615 |
async def check_github():
|
|
|
|
| 635 |
await asyncio.sleep(5)
|
| 636 |
print(bot.log_channel)
|
| 637 |
guild = bot.get_guild(879548962464493619)
|
| 638 |
+
|
| 639 |
+
if guild:
|
| 640 |
+
role_ids = [1014517792550166630, 1014548568238997616, 1014548769355862036, 1077250031180071023, 1093982736961785877]
|
| 641 |
+
roles = [guild.get_role(rid) for rid in role_ids if guild.get_role(rid) is not None]
|
| 642 |
+
persistent_view = PersistentRoleSelectionView(roles)
|
| 643 |
+
bot.add_view(persistent_view) # This makes the view persistent across restarts.
|
| 644 |
+
|
| 645 |
for channel in guild.text_channels: # helps with more accurate logging across restarts
|
| 646 |
try:
|
| 647 |
message_cache.update({m.id: m async for m in channel.history(limit=10000)})
|
|
|
|
| 650 |
print(f"An error occurred while fetching messages from {channel.name}: {e}")
|
| 651 |
await asyncio.sleep(0.1)
|
| 652 |
|
| 653 |
+
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
|
| 657 |
+
|
| 658 |
|
| 659 |
def run_bot():
|
| 660 |
bot.run(DISCORD_TOKEN)
|