Are there restrictions on Discord Bot's persistent UI elements?

I’ve been working on a bot that creates interactive messages from slash commands. These messages need data from an API and have buttons that make API requests using info in the message view. Each user has their own API tokens, so I want the buttons to work for different users at any time.

I thought about making the message Views persistent. This way, a user could use the command in a channel, and others could interact with it later. But I noticed in the discord.py docs that Views need to be set up again when the bot restarts. I’m also worried about how many messages without timeouts might affect performance.

Does anyone know if Discord limits how many persistent views a bot can have in a server? Are there any performance or memory issues to watch out for? Am I being too ambitious with this idea?

Here’s a simple example of what I’m trying to do:

class PersistentView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(label="Click Me", style=discord.ButtonStyle.primary)
    async def button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message("Button clicked!", ephemeral=True)

@bot.slash_command()
def create_persistent_view(ctx):
    view = PersistentView()
    await ctx.respond("This is a persistent view!", view=view)

Any insights would be really helpful. Thanks!

I’ve dabbled with persistent views in my Discord bots, and they can be tricky. From my experience, there’s no hard cap on persistent views, but you’ll want to be mindful of resource usage. I’ve found that implementing a soft limit (say, 100 views per server) and a periodic cleanup routine helps keep things manageable.

For your API token situation, have you considered using Discord’s built-in user data storage? It’s a neat feature that lets you store small amounts of data per user, which could be perfect for API tokens. This way, you don’t need to worry about keeping that info in the message itself.

As for restarting, I’ve had success with a hybrid approach. I store critical view data in a database, but I also use a cooldown system. When the bot restarts, it only recreates views that have been interacted with recently. This helps prevent any potential rate limit issues on larger servers.

Just remember, while persistent views are powerful, they can become a headache if not managed properly. Start small, monitor performance, and scale up gradually. Good luck with your project!

I’ve encountered similar challenges with persistent views in my Discord bot projects. While Discord doesn’t impose strict limits, it’s wise to be cautious about scalability. Consider implementing a cleanup mechanism to remove inactive views after a certain period. This can help manage memory usage and maintain performance as your bot grows.

For handling API tokens securely, you might want to explore Discord’s interaction tokens. These allow you to respond to button clicks without storing user-specific data in the message itself. This approach could simplify your persistent view implementation and enhance security.

Regarding restarts, I’ve found that storing view data in a database and recreating views on startup works well. It adds some complexity but ensures your bot remains functional across restarts. Just be mindful of potential rate limits when bulk-recreating views on larger servers.

hey dancingfox, interesting project! i’ve worked with persistent views before. discord doesn’t have hard limits, but too many can slow things down. for performance, consider caching API data and using timeouts for inactive messages. also, look into storing view data in a database for restarts. good luck with ur bot!