Search code examples
pythondiscorddiscord.pypycord

How would I go about automatically creating new buttons in discord.py depending on user input?


What I need is for a user to input a number of arguments into a modal (max of 5), and then those arguments are passed to buttons, with each argument having its own button. I have tried using a for loop, but using the only way I know how to make buttons what happens is I just get a single button.

class ButtonView(discord.ui.View):
    for _ in range(int(poptions)):
        print(bnum)

        @discord.ui.button(label=f"{optionsdict[guildid][bnum]}", style=discord.ButtonStyle.green)
        async def first_button_callback(self, button, interaction):
            await interaction.response.send_message(f"You voted for {optionsdict[guildid][bnum]}", ephemeral=True)

        if bnum != poptions:
            bnum += 1

Is there any other method of creating buttons that would allow them to be dynamically created? I figure I could just use 5 if statements but I am wondering if there's any more graceful way to do it.


Solution

  • You can use View.add_item to add something to a view, and just create Button instances manually.