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

使用discordgo发送TextInput组件报UNION_TYPE_CHOICES错误求助

问题原因与解决方案

核心问题

Discord的TextInput组件仅能用于模态框(Modal),无法直接添加到普通消息或InteractionResponseChannelMessageWithSource类型的交互响应中。你遇到的UNION_TYPE_CHOICES错误,本质是普通消息/普通交互响应的ActionRow不支持类型为4的TextInput组件(仅支持类型2、3、5-8的组件,比如按钮、选择菜单等)。

正确使用方式

1. 通过按钮触发模态框(最常见场景)

先发送带按钮的普通消息,用户点击按钮后,在交互回调中返回模态框,将TextInput放在模态框内:

// 第一步:发送带按钮的消息
btnComponents := []discordgo.MessageComponent{
    discordgo.ActionsRow{
        Components: []discordgo.MessageComponent{
            discordgo.Button{
                CustomID: "open_modal_btn",
                Label:    "打开输入框",
                Style:    discordgo.PrimaryButton,
            },
        },
    },
}
msgSend := &discordgo.MessageSend{
    Content:    "点击按钮打开输入框",
    Components: btnComponents,
}
_, err := session.ChannelMessageSendComplex(chnlId, msgSend)
if err != nil {
    // 错误处理
}

// 第二步:处理按钮点击交互,返回模态框
func handleButtonInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) {
    if i.MessageComponentData().CustomID != "open_modal_btn" {
        return
    }

    // 构造模态框,包含TextInput
    err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
        Type: discordgo.InteractionResponseModal, // 必须是模态框类型
        Data: &discordgo.InteractionResponseData{
            CustomID: "feedback_modal",
            Title:    "提交反馈",
            Components: []discordgo.MessageComponent{
                discordgo.ActionsRow{
                    Components: []discordgo.MessageComponent{
                        discordgo.TextInput{
                            CustomID:    "fd_text_short_111",
                            Label:       "对CEO的反馈",
                            Style:       discordgo.TextInputParagraph,
                            MinLength:   10,
                            MaxLength:   300,
                            Required:    true,
                            Placeholder: "请输入你的反馈...",
                        },
                    },
                },
            },
        },
    })

    if err != nil {
        panic(err)
    }
}

2. 修正你的交互响应代码

你之前尝试用文本命令返回TextInput时,错误使用了InteractionResponseChannelMessageWithSource类型,必须改为InteractionResponseModal类型:

"text": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
    err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
        Type: discordgo.InteractionResponseModal, // 替换原类型
        Data: &discordgo.InteractionResponseData{
            CustomID: "feedback_modal",
            Title:    "提交反馈", // 模态框必须有标题
            Components: []discordgo.MessageComponent{
                discordgo.ActionsRow{
                    Components: []discordgo.MessageComponent{
                        discordgo.TextInput{
                            CustomID: "fd_text_short_111",
                            Label:    "对CEO的反馈",
                            Style:    discordgo.TextInputParagraph,
                            MinLength: 10,
                            MaxLength: 300,
                            Required:  true,
                        },
                    },
                },
            },
        },
    })

    if err != nil {
        panic(err)
    }
},

关键规则回顾

  • TextInput必须嵌套在模态框的ActionRow中,不能出现在普通消息或普通交互响应里
  • 触发模态框的交互响应类型必须是InteractionResponseModal,且必须指定CustomID和Title
  • 每个模态框最多包含5个ActionRow,每个ActionRow只能放一个TextInput

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 18:24:42