首页 > 解决方案 > 使用 discord.py 有没有办法读取嵌入并将嵌入的一部分存储为变量?

问题描述

我尝试在嵌入中查找关键字,但这给了我一个错误,说类型不可迭代,它用于不和谐机器人游戏,我希望它读取配置文件嵌入,然后将玩家统计信息存储为变量,但我不能找到一种方法来做到这一点。

@bot.command(name='dungeon')
async def dungeon(ctx):
    @bot.event
    async def on_message(message):
        embeds = message.embeds # return list of embeds
        for embed in embeds:
            print(embed.to_dict())
            if '**AT**' in embeds[0]:
                print('found')

标签: pythondiscord.py

解决方案


您可以遍历嵌入中的项目并查找item.nameor item.value

第一行是仅采用消息中的第一个嵌入,您可以对其进行编辑以将它们全部循环。

PS 我添加了有关如何更新项目名称或值的方法。如果你想这样做。

embed = message.embeds[0]
for i, item in enumerate(embed.fields):
    if "**AT**" in item.name:
        print(f'found {item.value}')
        
        # to update the embed 
        embed.set_field_at(index=i, name=item.name, inline=item.inline, value='NEWVALUE')

await message.edit(embed=embed)

推荐阅读