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

discord.py使用slash指令发Button报'Button对象无get属性'错误怎么解决

修复方案

报错根本原因

你使用的discord-py-slash-command库对components参数的格式有强制要求:

  • 不能直接把Button对象放在数组里传入,最外层必须是ActionRow(动作行)对象数组,库会尝试对最外层组件调用字典的get方法校验类型,直接传Button就会触发属性不存在的报错
  • 你现有代码的按钮点击校验逻辑和实际按钮配置不匹配:按钮label是sus,但校验逻辑是匹配Click开头的label,就算解决了第一个报错也永远触发不了回调

具体修改操作

首先导入需要的工具方法:

from discord_slash.utils.manage_components import create_actionrow, create_button, ButtonStyle

然后调整命令逻辑,把按钮包裹进动作行,同步修改校验规则:

@slash.slash(
  name="ButtonTest",
  description='Test a button function',
  guild_ids=[894190915097337876]
)
async def _ButtonTest(ctx: SlashContext):
  # 创建按钮实例
  test_btn = create_button(style=ButtonStyle.blue, label='sus')
  # 将按钮封装进动作行
  row = create_actionrow(test_btn)
  # 发送消息时传入动作行数组
  await ctx.send("test", components=[row])
  # 修改校验规则匹配实际按钮标签
  click_event = await bot.wait_for("button_click", check=lambda i: i.component.label == "sus")
  # 用respond方法响应交互,避免discord提示交互失败
  await click_event.respond(content='Test button function succeeded!!')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 01:24:02