【问题标题】:Is there a way to read a value of a specific field in an embed discord.py有没有办法在嵌入 discord.py 中读取特定字段的值
【发布时间】:2021-08-05 11:41:51
【问题描述】:

所以我有一个像图片中的嵌入,我想从那个嵌入中获取用户 ID,有什么办法吗?

到目前为止我尝试了什么:

【问题讨论】:

  • 是的,有。我的问题是 - 到目前为止你尝试过什么?
  • 我曾尝试使用 embed.to_dict() 但无法获得该特定字段的值,我也尝试使用 .fields 和 .set_field_at 但它们都不起作用。
  • 您能否将这些方法添加到您的问题中?
  • 您能详细说明每种方法吗?就像您尝试获取该字段的值所做的一样,等等。

标签: python discord.py


【解决方案1】:

set_field_at() 是一个更新字段内容的函数。您将无法使用此方法检索内容。

您仍然可以使用.fields。您可以在您选择的方法中放入类似于以下内容的内容。

embed = # Put the Embed in the picture as a discord.Embed object and assign it to this variable
for field in embed.fields: # Dynamically get the user id field.
    if field.name.lower() == "user id": # I recommend copying and pasting the field of choice, just in case the characters are not the same visually.
        user_id_field = field
        break
else: # In case the field isn't found
    pass # Put some code here
user_id = int(user_id_field.value) # Get the value of the field
# Either send or print it

参考资料:

【讨论】:

  • 很确定第 3 行中的 'in' 应该是 == 因为我的代码不能使用 in 但可以使用 ==
  • @NimVrod 我保留了in,以防有额外的字符你看不到。但两者都很好。
猜你喜欢
  • 2020-10-17
  • 2021-04-25
  • 2011-07-07
  • 2020-11-27
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多