如何获得 discord.Embed [DISCORD.PY] 的长度

How to get the length of a discord.Embed [DISCORD.PY]

我需要一种方法来获取嵌入的确切长度,以便检查它是否超过限制。所以如果它确实超过了限制,它可以分裂成多个 messages/embeds.

如果我们查看 official Discord documentation for rich Embed 限制,我们可以看到总限制为 6000 个字符。

Additionally, the characters in all title, description, field.name, field.value, footer.text, and author.name fields must not exceed 6000 characters in total. Violating any of these constraints will result in a Bad Request response.

但也有个别限制如下:

+-------------+------------------------+
|    Field    |         Limit          |
+-------------+------------------------+
| title       | 256 characters         |
| description | 4096 characters*       |
| fields      | Up to 25 field objects |
| field.name  | 256 characters         |
| field.value | 1024 characters        |
| footer.text | 2048 characters        |
| author.name | 256 characters         |
+-------------+------------------------+

*参见this announcement in the Discord Developers server

所以在很多情况下,您可能需要单独检查和处理所有这些限制。最后,这里是如何检查是否达到 嵌入限制,这 检查上面的单个限制。

# embed would be the discord.Embed instance
fields = [embed.title, embed.description, embed.footer.text, embed.author.name]

fields.extend([field.name for field in embed.fields])
fields.extend([field.value for field in embed.fields])

total = ""
for item in fields:
    # If we str(discord.Embed.Empty) we get 'Embed.Empty', when
    # we just want an empty string...
    total += str(item) if str(item) != 'Embed.Empty' else ''

print(len(total))