使用discord-components报Button对象无法JSON序列化错误如何解决?
修复方案
报错原因
该错误是因为discord.py原生消息接口无法直接序列化discord-components库的Button对象,需要使用库内置的工具方法对组件进行包装后才能正常传参。
修复步骤
- 先确认你已经在bot初始化时注册了discord-components扩展,启动bot前添加以下代码:
from discord_components import DiscordComponents # 你的bot实例初始化完成后调用一次 DiscordComponents(bot)
- 引入
create_actionrow方法,将同一行的按钮包装为动作行对象后再传入components参数,修改你的消息发送代码如下:
from discord_components import Button, create_actionrow # 包装按钮为动作行 action_row = create_actionrow( Button(label=spaceCar, style=1, emoji="🚙", custom_id="Car"), Button(label=spaceCar, style=1, emoji="🛠️", custom_id="Tool") ) msg = await ctx.reply(embed=embed, components=[action_row])
- 如果修改后仍有报错,执行命令
pip install -U discord-components升级到库的最新版本即可。
内容的提问来源于stack exchange,提问作者Tesohh
相关产品推荐
相关产品推荐

