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

discord.py为embed添加带隐藏响应按钮功能问题求助

解决方案

核心问题

回调无法触发的根本原因是创建按钮时未指定custom_id,且回调装饰器没有绑定对应组件ID,机器人无法将按钮点击事件和你的处理逻辑关联。

修改步骤

1. 给按钮补充唯一custom_id

创建按钮时添加custom_id参数,值可自定义,保证全局唯一即可:

buttons = [
  create_button(style=ButtonStyle.green, label="I liked it", custom_id="feedback_like"), 
  create_button(style=ButtonStyle.grey, label="I'm neutral", custom_id="feedback_neutral"), 
  create_button(style=ButtonStyle.red, label="I did not like it", custom_id="feedback_dislike")
]
action_row = create_actionrow(*buttons)

bcc2 = await bcc.send(embed=embed, components=[action_row])

2. 绑定回调到对应按钮ID

在component_callback装饰器中传入要监听的按钮ID,支持传入多个ID实现多按钮共用同一段回调:

@slash.component_callback(components=["feedback_like", "feedback_neutral", "feedback_dislike"])
async def hello(ctx: ComponentContext):
    # 若你使用的库不支持hidden参数,替换为ephemeral=True即可,效果都是仅触发用户可见
    await ctx.send(content="You pressed a button!", hidden=True)

3. 确认回调注册位置

回调代码必须放在机器人启动逻辑之前,不要写在定时发送消息的函数内部,避免机器人启动时未加载回调规则导致无响应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 15:18:05