Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Adam Molnar commited on
make role buttons persistent across restarts
Browse files543 unmodified lines54454554654754854955054955055155255355455555655755855956055456156256315 unmodified lines5795805815765775825835845795855865875885895907 unmodified lines59859960059460160259759859960360460560660760860961061161261361461519 unmodified lines6356366376386396406416426436446456466472 unmodified lines650651652653654655656657658659660543 unmodified lines print(f"on_voice_state_update Error: {e}")
# -------------------------------------------------------------------------------------------------------------------------------------
# Custom persistent button that toggles a role for the clicking user.class RoleToggleButton(Button): def __init__(self, role: discord.Role): # Set the button label to the role name and use a primary style. super().__init__(label=role.name, style=discord.ButtonStyle.primary) # Assign a fixed custom_id that uniquely identifies this button. # Ensure that the custom ID is unique among all buttons in your bot. super().__init__( label=role.name, style=discord.ButtonStyle.primary, custom_id=f"persistent_role_toggle_{role.id}" ) self.role = role
async def callback(self, interaction: discord.Interaction): # Check if the user already has the role if self.role in interaction.user.roles: try: await interaction.user.remove_roles(self.role)15 unmodified lines f"Error adding role: {e}", ephemeral=True )
# A view that holds one toggle button per role.class RoleSelectionView(View):# Persistent view that holds one toggle button per role.class PersistentRoleSelectionView(View): def __init__(self, roles: list): # Setting timeout to None allows the view to persist indefinitely. # Set timeout to None to keep the view indefinitely active. super().__init__(timeout=None) # Create a button for each role for role in roles: self.add_item(RoleToggleButton(role))
7 unmodified lines
# List the role IDs you want to include (replace these with your actual role IDs). role_ids = [1014517792550166630, 1014548568238997616, 1014548769355862036, 1077250031180071023, 1093982736961785877] # Retrieve the Role objects from the guild. roles = [ctx.guild.get_role(rid) for rid in role_ids if ctx.guild.get_role(rid) is not None]
# Create the view with buttons for each role. view = RoleSelectionView(roles) # Send a message with the view attached. # Create the persistent view. view = PersistentRoleSelectionView(roles) # Send the message with the persistent view attached. await ctx.send("Click the buttons below to toggle roles:", view=view)
# github test stuff -------------------------------------------------------------------------------------------------------------------"""async def check_github():19 unmodified lines await asyncio.sleep(5) print(bot.log_channel) guild = bot.get_guild(879548962464493619) if guild: role_ids = [1014517792550166630, 1014548568238997616, 1014548769355862036, 1077250031180071023, 1093982736961785877] roles = [guild.get_role(rid) for rid in role_ids if guild.get_role(rid) is not None] persistent_view = PersistentRoleSelectionView(roles) bot.add_view(persistent_view) # This makes the view persistent across restarts. for channel in guild.text_channels: # helps with more accurate logging across restarts try: message_cache.update({m.id: m async for m in channel.history(limit=10000)})2 unmodified lines print(f"An error occurred while fetching messages from {channel.name}: {e}") await asyncio.sleep(0.1)
def run_bot(): bot.run(DISCORD_TOKEN)app.py+30-10