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

Golang发送Discord交互请求偶发400错误COMPONENT_VALIDATION_FAILED求助

偶发400 COMPONENT_VALIDATION_FAILED错误的核心诱因是两个参数格式问题:

  1. 空hash字段被强制带入请求payload:Discord交互接口仅在按钮本身返回hash值时要求携带该字段,若hash为空仍传入空字符串,会触发组件校验失败。你之前场景下hash有值所以运行正常,遇到无hash的按钮就会触发错误。
  2. type字段格式错误:你当前代码传的是字符串"3",接口要求该字段为数字类型,部分场景下字符串格式也会触发校验报错。

修复方案

替换你代码中构造data和values的部分即可:

// 构造组件数据,仅非空hash才加入字段
data := map[string]interface{}{
    "component_type": msg.Components[i].Buttons[k].Type,
    "custom_id":      msg.Components[i].Buttons[k].CustomID,
}
if msg.Components[i].Buttons[k].Hash != "" {
    data["hash"] = msg.Components[i].Buttons[k].Hash
}

// type字段使用数字类型而非字符串
values := map[string]interface{}{
    "application_id": "270904126974590976",
    "channel_id":     msg.ChannelID,
    "type":           3,
    "data":           data,
    "guild_id":       msg.GuildID,
    "message_flags":  0,
    "message_id":     msg.ID,
}

额外排查项(修改后仍有错误可检查)

  • 调用前校验i、k的取值范围,避免数组越界读取到无效的按钮属性
  • 确认操作的消息未过期,Discord按钮交互有效期为15分钟,过期消息的按钮调用会直接返回校验失败
  • 多协程调用场景下确认msg结构体没有被并发修改,避免传错custom_id等核心参数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 07:45:02