使用discord.py与chat-exporter时出现AttributeError报错求助
解决discord.py + chat-exporter的AttributeError: 'NoneType' object has no attribute 'html'问题
报错根源
这个错误是因为chat_exporter.export()方法内部,负责生成HTML转录的核心对象未被正确初始化,变成了None,代码后续尝试访问它的.html属性时触发了异常。
可能的原因及解决方案
1. 未初始化chat_exporter
新版chat-exporter需要在bot启动时完成初始化,否则无法正常生成转录内容。在你的bot启动代码中添加初始化逻辑:
import chat_exporter # 在bot.run()之前执行初始化 chat_exporter.init_exporter(client)
2. Bot权限不足
确保你的bot拥有以下必要权限:
- 读取消息历史记录
- 查看频道
- 读取成员信息
权限缺失会导致chat-exporter无法获取生成转录所需的频道、消息或用户数据,进而引发内部对象为None的问题。
3. 命令执行在不支持的频道类型
chat-exporter仅支持文本频道,如果在语音频道、论坛频道等非文本频道执行archive命令,会导致生成失败。可以在命令中增加频道类型检查:
@client.command() @commands.has_role(MANAGEMENT_ROLE_ID) async def archive(ctx: commands.Context): # 检查是否为文本频道 if not isinstance(ctx.channel, discord.TextChannel): await ctx.send("仅支持在文本频道执行归档命令!") return transcript = await chat_exporter.export( ctx.channel, tz_info="UTC", military_time=True, bot=client ) if transcript is None: await ctx.send("无法生成频道转录内容,请检查权限或频道类型!") return transcript_file = discord.File( io.BytesIO(transcript.encode()), filename=f"transcript-{ctx.channel.name}.html", # 添加.html后缀方便直接打开 ) await ctx.send(file=transcript_file)
4. 版本兼容性问题
确认discord.py和chat-exporter的版本匹配:
- discord.py v2.x 需要搭配最新版的chat-exporter(v2.x系列)
- 如果使用discord.py v1.x,需要对应chat-exporter的v1.x版本
可以通过重新安装最新版解决:
pip install --upgrade discord.py chat-exporter
内容的提问来源于stack exchange,提问作者Joniii11
相关产品推荐
相关产品推荐

