You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py发送超长Embed描述时如何实现自动文本分块发送?

问题解答

核心结论

discord.py 没有提供类似 discord.js 中 { split: true } 的原生自动拆分超长Embed内容的功能,需要自行编写拆分逻辑处理长度超限问题。

Discord 官方对Embed字段有固定长度限制:description 字段最大长度为4096字符,单个Embed总字符上限为6000,单条消息最多可携带10个Embed。

实现方案示例

1. 字符串拆分函数

优先按换行符拆分,避免切断完整语句,每个块预留10字符冗余避免边缘情况超限:

def split_text(text: str, chunk_size: int = 4086) -> list[str]:
    chunks = []
    current_chunk = ""
    # 按换行拆分避免断句
    for line in text.split("\n"):
        # 检查当前块加新行是否超限
        if len(current_chunk) + len(line) + 1 <= chunk_size:
            current_chunk += line + "\n"
        else:
            chunks.append(current_chunk.strip())
            current_chunk = line + "\n"
    # 加入最后剩余的内容
    if current_chunk:
        chunks.append(current_chunk.strip())
    return chunks

2. 分批发送Embed的调用示例

my_text = "...大于4096字符的超长内容..."
# 拆分内容
text_chunks = split_text(my_text)
# 逐个发送Embed
for index, chunk in enumerate(text_chunks):
    embed = discord.Embed(
        title=f"Hello World(第{index+1}/{len(text_chunks)}页)",
        description=chunk
    )
    await ctx.send(embed=embed)

可选优化方向

  • 若内容过长需要发送的Embed数量太多,可以把超长内容转为纯文本拆分发送,纯文本单条消息默认长度上限为2000字符
  • 如果允许上传附件,可以把超长内容写入txt文件作为消息附件发送,避免拆分影响阅读体验

内容的提问来源于stack exchange,提问作者doejoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 03:45:04