So I am currently trying to make an archive bot where you can select characters and get all the information about them. I used buttons for each character.
Now I would want to sort the button rows so that each house/attribute has their own row. There are different amounts of characters in each house. Min 2 and max 4.
How can I sort the rows so it’ll only show character buttons of one house for each row? For now it will always use 5 for each row.
Thanks!
Thats my code so far:
async def archive(ctx):
characters = {
"Name": {"dropdown": NameDropdown, "image": "", "emoji": ""},
...
}
embed = discord.Embed(
title="",
description="",
color=COLOR
)
view = discord.ui.View(timeout=None)
for char_name, char_data in characters.items():
button = discord.ui.Button(
label=char_name,
style=discord.ButtonStyle.secondary,
emoji=char_data["emoji"],
custom_id=f"character_{char_name}"
)
async def button_callback(interaction: discord.Interaction, char_name=char_name, char_data=char_data):
char_dropdown = char_data["dropdown"]()
embed = discord.Embed(
title=f"{char_name}",
description=f"",
color=COLOR
)
embed.set_image(url=char_data["image"])
await interaction.response.send_message(embed=embed, view=char_dropdown, ephemeral=True)
button.callback = button_callback
view.add_item(button)
await ctx.send(embed=embed, view=view)
await ctx.message.delete()
I tried working with discord.ui.ActionRow()
but it seems I’m not able to understand how to use it properly or my information is outdated. Error codes like:
“AttributeError: module ‘discord.ui’ has no attribute ‘ActionRow'”
were the result.
Phi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If I understand your question correctly, you can achieve this by using the row
attribute in the discord.ui.Button
object. This allows you to assign buttons to specific rows.
Here’s a simple example on how this works:
view = discord.ui.View(timeout=None)
# Buttons for House 1 (Row 0)
button1 = discord.ui.Button(label="CharacterA", row=0)
button2 = discord.ui.Button(label="CharacterB", row=0)
# Buttons for House 2 (Row 1)
button3 = discord.ui.Button(label="CharacterC", row=1)
button4 = discord.ui.Button(label="CharacterD", row=1)
view.add_item(button1)
view.add_item(button2)
view.add_item(button3)
view.add_item(button4)
Note: The row
attribute is zero-based, so row=0
is the first row, row=1
is the second, and so on.