#Dynamically create buttons

1 messages · Page 1 of 1 (latest)

stoic crypt
#

So, I want to create buttons dynamically, from a given dict, so it depends on the len of such dict. So far I got something messy, withouth subclassing a view, I am setting the button callback as button.callback, but how would i send a response? I have no interaction defined

#
async def format_buttons(questions: dict):
    """Formats the buttons for the support system"""
    view = View(timeout=None)
    for index, question in enumerate(questions):
        button = discord.ui.Button(label=str(index), style=ButtonStyle.blurple)
        e = discord.Embed(title=question["q"], description=question["a"])
        button.callback = await interaction.response.send_message(embed=e)
        view.add_item(button)

    return view```
#

Thats what I got so far

true lotus
#

you can make it a lambda

#

or define a callback function and just point it to taht

stoic crypt
#

how would i make it lambda

true lotus
stoic crypt
#

So

button.callback = lambda interaction: await interaction.response.send_message()```
true lotus
#

try it and see

stoic crypt
#

i will

#

well, that fixes something. thanks

#

but now it is telling me await is used outside an async func? but the func itself is async

#

dismiss

#

any reason why it creates the same button

#
async def format_buttons(questions: list):
    """Formats the buttons for the support system"""
    print(questions)
    view = View(timeout=None)
    
    for index, question in enumerate(questions):
        index =+ 1
        button = discord.ui.Button(label=str(index), style=ButtonStyle.blurple)
        e = discord.Embed(title=question["q"], description=question["a"])
        button.callback = lambda interaction: interaction.response.send_message(embed=e)
        view.add_item(button)

    return view```
true lotus
#

give the buttons a custom_id

stoic crypt
#

oh

#

nope

#

still tries to create the same button, it just raises the duplicated custom id error now

#

Weird, because it is looping correctly

#

it always gets the second item

#

yeah, no idea what to do

true lotus
#

Uh why are you incrementing index manually?

stoic crypt
#

Cause button label would show 0 as the first one

#

Creates button with the same index. So it uses the same item?

#

For some reason?

#

If I print the item it prints the correct one

true lotus
#

you should just do label=str(index+1) then, don't do +=

stoic crypt
#

True

#

Any reason about the duplicated button?

stoic crypt
#

Bump

main grove
#

Can yoiu show your updated code and the code that calls format_buttons

stoic crypt
#

Sure. Give me a few mins, I'm not on my pc

#
@discord.ui.select(placeholder="Select a category", min_values=1, max_values=1, options=options)
    async def select_callback(self, select: discord.ui.Select, interaction: discord.Interaction):
        
        if select.values[0] == "verification":
            questions = await return_all("faq-verification")

            qs = []
            for qsn, question in enumerate(questions):
                q = question["q"]
                qsn += 1
                qs.append(f"**{qsn}** - {q}")

            embed = discord.Embed(title="Verification", description="\n".join(qs))
            view = await format_buttons(questions)
            await interaction.response.send_message(embed=embed, view=view)

        else:
            await interaction.response.send_message("Thonk")```
#
async def format_buttons(questions: list):
    """Formats the buttons for the support system"""
    view = View(timeout=None)

    for index, question in enumerate(questions):
        
        index += 1

        embed = discord.Embed(title=question["q"], description=question["a"])

        button = discord.ui.Button(label=str(index), style=ButtonStyle.blurple, custom_id=f"{index}button")
        button.callback = lambda interaction: interaction.response.send_message(embed=embed)

        view.add_item(button)



    return view```
#

return_all just retuns a list wich contains dicts from a mongo db collection

main grove
stoic crypt
#

Nope

#

There's no duplicate.

#

Printing them prints the correct one but the button is the same?

main grove
#

By the same do you mean a button has the same label as another?

stoic crypt
#

Same label and callback

main grove
#

Are you using 2.0.0rc1?

stoic crypt
#

Yes

main grove
#

Fix the index += thing and see if the issue persists. It might be becouse you are changing a value that is not ment to be changed

stoic crypt
#

Will try

stoic crypt
#

@main grove Nope, didnt fix it

stoic crypt
#

boop

#

solved it by subclassing the button and editing the callback there

#

instead of lambda