#Dynamically create buttons
1 messages · Page 1 of 1 (latest)
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
how would i make it lambda
So
button.callback = lambda interaction: await interaction.response.send_message()```
try it and see
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```
give the buttons a custom_id
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
Uh why are you incrementing index manually?
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
you should just do label=str(index+1) then, don't do +=
Bump
Can yoiu show your updated code and the code that calls format_buttons
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
You should do this
And also are your sure that there is not a duplicate in the list of questiond?
Nope
There's no duplicate.
Printing them prints the correct one but the button is the same?
By the same do you mean a button has the same label as another?
Same label and callback
Are you using 2.0.0rc1?
Yes
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
Will try
@main grove Nope, didnt fix it