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

Python Discord机器人导出聊天记录时报TypeError错误如何解决?

错误原因
  • 业务代码逻辑缺陷
    1. 调用chat_exporter.export获取到聊天记录导出内容后,没有将内容写入到创建的io.BytesIO缓冲区中,发送的文件为空
    2. 重复构造discord.File对象:将已经生成的discord.File实例再次传入discord.File构造函数,属于非法调用
    3. discord.Embed错误使用text参数,该参数不存在,应替换为description
  • 依赖库兼容性问题
    你使用的旧版discord-components库对新版discord.py的FormData结构处理存在bug,重写的send方法无法兼容新版discord.py的参数传递逻辑,最终触发'FormData' object is not iterable报错。
修复方案
  1. 调整业务代码,修复上述逻辑缺陷,修复后代码如下:
async def transcript(self, ctx):
    """Creates a transcript for the channel the command is entered in
    """
    if ctx.channel.category.name in self.bot.ticket_categories:
        transcript = await chat_exporter.export(ctx.channel)

        if transcript is None:
            # 修复:将text参数替换为正确的description参数
            embed = discord.Embed(description="Transcript creation failed!",
                                  color=0xDE3163)
            await ctx.send(embed=embed)
            return
        
        # 修复:将导出的HTML内容编码后写入缓冲区
        buffer = io.BytesIO(transcript.encode("utf-8"))
        # 修复:将缓冲区指针移动到起始位置,保证读取内容完整
        buffer.seek(0)
        # 修复:仅构造一次discord.File对象
        transcript_file = discord.File(buffer, filename=f"transcript-{ctx.channel.name}.html")

        embed = discord.Embed(title="Transcript creation successful!",
                              color=0x00A86B)
        await ctx.send(embed=embed)
        await ctx.send(file=transcript_file)
  1. 修复依赖兼容性:将discord-components库升级到最新兼容版本,如果你当前业务没有用到该库提供的组件功能,可以直接移除该库的导入与初始化逻辑,即可解决兼容性报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 13:06:03