Skip to content

Implement View Persistence Across Bot Restarts #5

Description

@Zingzy

Currently, Discord UI components (buttons, selects, etc.) stop working after the bot restarts. This creates a poor user experience as users may encounter "This interaction failed" errors when trying to interact with previously sent messages. We need to implement a persistence mechanism to maintain view functionality across bot restarts.

Technical Implementation

  • Use Discord's custom_id parameter as a unique identifier for views
  • Implement a structured format for custom_ids (e.g., view_type:data:uuid)
  • Create a view factory that can reconstruct views from custom_ids
  • Implement serialization/deserialization of view state
  • Set up persistence infrastructure:
# Example implementation
class PersistentView(discord.ui.View):
    def __init__(self, timeout=180):
        super().__init__(timeout=timeout)
        self.add_item(discord.ui.Button(custom_id="persistent:button:example", label="Persistent Button"))
    
    @classmethod
    def from_custom_id(cls, custom_id, data):
        """Reconstruct view from stored custom_id and data"""
        view = cls()
        # Set up view state based on saved data
        return view

# In bot initialization
async def setup_hook(self):
    # Register persistent views on startup
    self.add_view(PersistentView())

Benefits

  • Improved user experience with consistently working UI components
  • No need to re-send messages after bot restarts
  • Reduced API usage from not having to re-send messages
  • Better reliability for long-running UI workflows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions