Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Adam Molnar commited on
make role buttons persistent across restarts
Browse files543 unmodified lines54454554654754854955055155255355455555655715 unmodified lines5735745755765775785795805815825837 unmodified lines59159259359459559659759859960060160260360460560660719 unmodified lines6276286296306316322 unmodified lines635636637638639640543 unmodified linesprint(f"on_voice_state_update Error: {e}")# -------------------------------------------------------------------------------------------------------------------------------------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)self.role = roleasync def callback(self, interaction: discord.Interaction):# Check if the user already has the roleif self.role in interaction.user.roles:try:await interaction.user.remove_roles(self.role)15 unmodified linesf"Error adding role: {e}", ephemeral=True)# A view that holds one toggle button per role.class RoleSelectionView(View):def __init__(self, roles: list):# Setting timeout to None allows the view to persist indefinitely.super().__init__(timeout=None)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.await ctx.send("Click the buttons below to toggle roles:", view=view)# github test stuff -------------------------------------------------------------------------------------------------------------------"""async def check_github():19 unmodified linesawait asyncio.sleep(5)print(bot.log_channel)guild = bot.get_guild(879548962464493619)for channel in guild.text_channels: # helps with more accurate logging across restartstry:message_cache.update({m.id: m async for m in channel.history(limit=10000)})2 unmodified linesprint(f"An error occurred while fetching messages from {channel.name}: {e}")await asyncio.sleep(0.1)def run_bot():bot.run(DISCORD_TOKEN)543 unmodified lines54454554654754854955055155255355455555655755855956056156256315 unmodified lines5795805815825835845855865875885895907 unmodified lines59859960060160260360460560660760860961061161261361461519 unmodified lines6356366376386396406416426436446456466472 unmodified lines650651652653654655656657658659660543 unmodified linesprint(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):# 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 = roleasync def callback(self, interaction: discord.Interaction):if self.role in interaction.user.roles:try:await interaction.user.remove_roles(self.role)15 unmodified linesf"Error adding role: {e}", ephemeral=True)# Persistent view that holds one toggle button per role.class PersistentRoleSelectionView(View):def __init__(self, roles: list):# Set timeout to None to keep the view indefinitely active.super().__init__(timeout=None)# Create a button for each rolefor 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]roles = [ctx.guild.get_role(rid) for rid in role_ids if ctx.guild.get_role(rid) is not None]# 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 linesawait 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 restartstry:message_cache.update({m.id: m async for m in channel.history(limit=10000)})2 unmodified linesprint(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