discord.py打印Interaction对象报__slots__属性缺失错误如何解决?
报错原因
该错误由discord.py-message-components第三方扩展库与原生discord.py的版本冲突导致:
discord.py-message-components是discord.py未官方支持交互组件时期的第三方扩展,会覆盖原生discord.py的部分核心模块。它自行实现的Interaction类未定义__slots__属性,但你环境中残留的原生discord.py的interactions.py模块的__repr__方法仍然会尝试访问该属性,直接触发属性不存在报错。button_click事件返回的是扩展库自行实现的Interaction实例,和你导入的原生discord.Interaction类型不匹配,也是导致异常的潜在因素。
修复方案
- 第一步:先彻底卸载环境中所有discord相关依赖,清除残留冲突文件:
pip uninstall -y discord.py discord.py-message-components discord
- 方案一:继续使用
discord.py-message-components扩展
卸载完成后单独重新安装该扩展即可,它自带适配后的discord核心模块,不会再出现冲突:
python3 -m pip install -U discord.py-message-components
- 方案二(更推荐):使用官方原生支持组件的discord.py 2.0+版本
目前discord.py 2.0及以上版本已经原生支持按钮、下拉菜单等交互组件,不需要额外安装第三方扩展,稳定性更高:
- 卸载残留后直接安装官方最新版discord.py:
pip install -U discord.py
- 对业务代码做少量适配调整即可正常运行,调整后示例:
@commands.command(ignore_extra=True) async def test_command(self, ctx, count:int): # 用官方View类管理交互组件 view = discord.ui.View() new_button = discord.ui.Button(label="test_button", custom_id = "009") view.add_item(new_button) message = await ctx.send("test", view=view) def check_button(i: discord.Interaction): return i.user == ctx.author and i.message == message and i.custom_id == "009" interaction = await bot.wait_for('interaction', check=check_button) print(interaction, new_button) await interaction.response.edit_message(content=str(count))
内容的提问来源于stack exchange,提问作者Ibrahim
相关产品推荐
相关产品推荐

