discordgo – Sending TextInput as MessageComponent returning "Invalid Form Body" with code "UNION_TYPE_CHOICES"

huangapple go评论167阅读模式
英文:

discordgo - Sending TextInput as MessageComponent returning "Invalid Form Body" with code "UNION_TYPE_CHOICES"

问题

我们正在使用Go语言和discordgo库开发一个Discord机器人。我能够使用[DiscordSession].ChannelMessageSendComplex(chnlId, msg)发送包含ActionRow、SelectMenu和Button组件的消息,但是当我将TextInput组件放在消息中时,它返回400错误,错误信息为"Invalid Form Body"。

完整的错误消息如下:

"HTTP 400 Bad Request, 
{"code": 50035, "errors": {"components": 
{"0": 
{"components": 
{"0": 
{"_errors": [
{"code": "UNION_TYPE_CHOICES", 
"message": "Value of field \"type\" must be one of (2, 3, 5, 6, 7, 8)."}]}}}}}, "message": "Invalid Form Body"}"

我用以下代码初始化组件对象:

components := []discordgo.MessageComponent{
    discordgo.ActionsRow{
        Components: []discordgo.MessageComponent{
            discordgo.TextInput{
                CustomID:    "fd_text_short",
                Label:       "Some Label",
                Style:       discordgo.TextInputShort,
                Placeholder: "test",
                MinLength:   1,
                MaxLength:   200,
            },
        },
    },
}

发送消息的代码:

msgSend := &discordgo.MessageSend{
    Content:    "Some Content",
    Components: components,
}
_, err := session.ChannelMessageSendComplex(chnlId, msgSend)

我还尝试在discordgo仓库的组件示例这里中使用现有的示例,并尝试添加文本应用命令以响应交互响应中的TextInput消息,但是出现相同的错误:

"text": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
    err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
        Type: discordgo.InteractionResponseChannelMessageWithSource,
        Data: &discordgo.InteractionResponseData{
            Content: "Please share any feedback you",
            Flags:   discordgo.MessageFlagsEphemeral,
            Components: []discordgo.MessageComponent{
                discordgo.ActionsRow{
                    Components: []discordgo.MessageComponent{
                        discordgo.TextInput{
                            CustomID: "fd_text_short_111",
                            Label:    "Any feedback you have with CEO",
                            Style: discordgo.TextInputParagraph,
                            MinLength: 10,
                            MaxLength: 300,
                            Required:  true,
                        },
                    },
                },
            },
        },
    })

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

我尝试遵循Discord API文档中关于TextInput组件的所有注意事项这里

欢迎任何帮助来解决这个问题 discordgo – Sending TextInput as MessageComponent returning "Invalid Form Body" with code "UNION_TYPE_CHOICES"

英文:

We are developing a Discord bot with Go language using discordgo library. I was able to send messages containing ActionRow, SelectMenu, Button components using [DiscordSession].ChannelMessageSendComplex(chnlId, msg) but when I put TextInput component inside the message, it's returning 400 "Invalid Form Body" error.

Complete Error Message:

    "HTTP 400 Bad Request, 
{\"code\": 50035, \"errors\": {\"components\": 
{\"0\": 
{\"components\": 
{\"0\": 
{\"_errors\": [
{\"code\": \"UNION_TYPE_CHOICES\", 
\"message\": \"Value of field \\\"type\\\" must be one of (2, 3, 5, 6, 7, 8).\"}]}}}}}, \"message\": \"Invalid Form Body\"}"

My Code to initiate the components object:

components := []discordgo.MessageComponent{
		discordgo.ActionsRow{
			Components: []discordgo.MessageComponent{
				discordgo.TextInput{
					CustomID:    "fd_text_short",
					Label:       "Some Label",
					Style:       discordgo.TextInputShort,
					Placeholder: "test",
					MinLength:   1,
					MaxLength:   200,
				},
			},
		},
	}

The code to send message:

msgSend := &discordgo.MessageSend{
		Content:    "Some Content",
		Components: components,
	}
	_, err := session.ChannelMessageSendComplex(chnlId, msgSend)

I also used existing samples in discordgo repo for components here and tried to add text application command to respond a TextInput mesage in interaction response, but getting same error:

"text": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
				Type: discordgo.InteractionResponseChannelMessageWithSource,
				Data: &discordgo.InteractionResponseData{
					Content: "Please share any feedback you",
					Flags:   discordgo.MessageFlagsEphemeral,
					Components: []discordgo.MessageComponent{
						discordgo.ActionsRow{
							Components: []discordgo.MessageComponent{
								discordgo.TextInput{
									CustomID: "fd_text_short_111",
									Label:    "Any feedback you have with CEO",
									Style: discordgo.TextInputParagraph,
									MinLength: 10,
									MaxLength: 300,
									Required:  true,
								},
							},
						},
					},
				},
			})

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

I tried to follow all considerations Discord API Documentation have for TextInput component here

Any help to figure out this problem is welcome discordgo – Sending TextInput as MessageComponent returning "Invalid Form Body" with code "UNION_TYPE_CHOICES"

答案1

得分: 1

我在discordgo频道这里提出了这个问题,答案是:

> 你不能在模态交互响应之外使用TextInput。

这意味着我们只能在模态中使用TextInput,这是有道理的,因为在与用户进行直接消息交互时,discord的输入框已经足够好用了。我不知道为什么这在Discord的文档这里没有提到。

英文:

I asked this question in discordgo channel here and the answer was:

> You cannot use TextInput outside a modal interaction response.

it means we only can use TextInput with Modals and it makes sense as in direct message interaction with users, discord's input box is good enough to get text from user. I don't know why this is not mentioned in Discord documentations here

答案2

得分: 0

当我将TYPE从discordgo.InteractionResponseChannelMessageWithSource更改为discordgo.InteractionResponseModal时,它对我有效。感谢Javad的建议!

英文:

It works for me when I change TYPE discordgo.InteractionResponseChannelMessageWithSource to discordgo.InteractionResponseModal.
Thanks Javad for the suggestion!

huangapple
  • 本文由 发表于 2022年9月1日 02:47:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73560591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定