python - 使用 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')
解决方案
您可以遍历嵌入中的项目并查找item.name
or 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)
推荐阅读
- azure - 将 spark 数据帧从 azure databricks 写入 S3 会导致 java.lang.VerifyError: Bad type on operand stack 错误
- python - 用os.walk()搜索所有的excel文件,然后调用函数读取这个excel,怎么办?
- javascript - ng2-file-upload 单个请求 clearQueue() 不起作用
- c# - 如何组合列表
与列表 - >?
- android - 在模块 jetified-hamcrest-core-1.3.jar 中发现重复的类 org.hamcrest.BaseDescription
- java - Postgres:将自定义类型从 Java 传递给 postgres 函数
- css - Angular - 单击按钮后更改表中行的背景颜色
- android - facebook sdk 还需要在 android 上手动初始化吗?
- java - 将自定义元数据添加到二进制文件
- java - 测试期间spring webflux中的全局异常处理