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

Discord.py中Slash命令无法与Buttons组件配合使用求助

解决discord_slash与discord_components按钮兼容问题

问题原因

你使用的discord_slash库的ctx.send是Slash命令专属的自定义实现,它的components参数只支持Discord官方原生组件(如ActionRow)的结构;而discord_components库的按钮是通过二维列表格式传递的,两者格式不兼容,导致内部处理时误将列表当作字典调用get方法,触发AttributeError。

解决方案

把ctx.send替换为Discord.py原生的频道发送方法ctx.channel.send——discord_components库是对Discord.py原生send方法打补丁来支持按钮组件的,而discord_slash的ctx.send未被该补丁覆盖,所以无法识别按钮格式。

修改后的代码:

msg = await ctx.channel.send(
    embed=a_dictionary["a"],
    components = [
        [Button(style=ButtonStyle.blue, label="◀️"),
         Button(style=ButtonStyle.blue, label="▶️"),
         Button(style=ButtonStyle.green, label="Get Job"),
         Button(style=ButtonStyle.red, label="Remove Job")
        ],
    ]
)

额外注意事项

  • 必须在代码初始化阶段调用DiscordComponents(bot)注册组件支持:
    from discord_components import DiscordComponents
    
    DiscordComponents(bot)
    
  • 监听按钮点击事件时,要使用discord_components提供的方法,示例:
    from discord_components import ButtonInteraction
    
    interaction = await bot.wait_for("button_click")
    if interaction.component.label == "Get Job":
        await interaction.respond(content="已领取任务!")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 18:10:31