英文:
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组件的所有注意事项这里。
欢迎任何帮助来解决这个问题
英文:
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
答案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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论