Home > Software engineering >  Is there any way of finding the index of an embed field in discord? (Python)
Is there any way of finding the index of an embed field in discord? (Python)

Time:09-02

I need to find the index of an embed field in discord. Suppose I have
embed=discord.Embed(title="Embed",description="Example embed")
embed.add_field(name="one", value="1")
embed.add_field(name="two", value="2").... etc

and supposed I need to update a field using embed.set_field_at(index, name, value) so how can I get the index of the supposed field? Can I find that using its name/value?

CodePudding user response:

You can use next to find the first match of a predicate using generator-builder notation. To get the index, you can enumerate the fields and take the index part of the enumeration.

embed = discord.Embed()
embed.add_field(name="test 1", value="test a")
embed.add_field(name="test 2", value="test b")

# get index of the field with name "test 2" and value "test b"
field_index = next(
    index
    for (index, f) in enumerate(embed.fields)
    if f.name == "test 2" and f.value == "test b"
)

# update field at index
embed.set_field_at(field_index, name="new name", value="new value", inline=False)
  • Related