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

如何从interactionCreate事件中获取按钮的custom ID?

实现方法

可以直接在interaction.isButton()判断分支内通过interaction.customId属性读取被点击按钮的CustomId,该属性返回字符串值,和你创建按钮时配置的customId完全一致,区分大小写。

修改后的参考代码如下:

client.on("interactionCreate", interaction => {
    if (interaction.isButton()) {
        // 获取被点击按钮的CustomId
        const targetCustomId = interaction.customId
        // 可根据customId匹配执行对应逻辑,示例:
        switch(targetCustomId) {
            case 'confirm_order':
                // 处理订单确认逻辑
                break
            case 'cancel_order':
                // 处理订单取消逻辑
                break
        }
    }
})

如果你的按钮使用了动态拼接的CustomId(比如delete_1001这种带业务参数的格式),可以直接拆分字符串提取对应参数:

const [operateType, recordId] = targetCustomId.split('_')
// operateType 取到 delete,recordId 取到 1001

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 07:18:04