#Dynamic Persistent Views

1 messages · Page 1 of 1 (latest)

still quest
#

Hello,

I've reviewed the persistent views page, however I'm trying to adapt it to be dynamic. Does @discord.ui.button() recognize the button based on the custom_id set or do I need to provide a message ID somewhere?

e.g. something like:

from db import main_db
import discord

giveaways = main_db["giveaways"]


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

    active = giveaways.find({"active": True})

    for doc in active:
        @discord.ui.button(label="Enter Giveaway", style=discord.ButtonStyle.green, custom_id=doc["id"])
        async def on_button_press(self, button: discord.ui.Button, interaction: discord.Interaction):
            ...
smoky isle
#

as long as you have a custom id and no timeout (and you used add_view) the view should be persistent

still quest
#

Or do I have to include that in there somewhere

smoky isle
#

No you add the view to the bot

#

bot.add_view(GiveawayViews())

still quest
#

ah yeah

#

thanks

floral furnace
#

/close