英文:
using discord.go, how do I retrieve message attachments from a slash command option
问题
我正在尝试从一个类型为ApplicationCommandOptionAttachment
的斜杠命令选项中检索附件并下载内容。我的当前代码如下:
"import-file": func(s *discordgo.Session, i *discordgo.InteractionCreate) *discordgo.MessageEmbed {
logger.Info(i.Interaction.Message.Attachments)
logger.Info(i.ApplicationCommandData().Options[0].Value)
return nil
},
请注意,这只是代码的翻译部分,不包括其他内容。
英文:
I'm trying to retrieve attachments and download the contents from a slash command option with the type: ApplicationCommandOptionAttachment
. My current code:
"import-file": func(s *discordgo.Session, i *discordgo.InteractionCreate) *discordgo.MessageEmbed {
logger.Info(i.Interaction.Message.Attachments)
logger.Info(i.ApplicationCommandData().Options[0].Value)
return nil
},
答案1
得分: 1
为了获取通过斜杠命令选项提供的附件内容,请使用以下代码。
func SlashCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
attachmentID := i.ApplicationCommandData().Options[0].Value.(string)
attachmentUrl := i.ApplicationCommandData().Resolved.Attachments[attachmentID].URL
//获取文件内容
res, err := http.DefaultClient.Get(attachmentUrl)
if err != nil {
log.Println(errors.New("无法从代码解释机器人获取响应"))
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "无法获取响应",
},
})
return
}
}
英文:
In order to get the contents of an attachment provided through a slash command option. Use the following code.
func SlashCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
attachmentID := i.ApplicationCommandData().Options[0].Value.(string)
attachmentUrl := i.ApplicationCommandData().Resolved.Attachments[attachmentID].URL
//get the file contents
res, err := http.DefaultClient.Get(attachmentUrl)
if err != nil {
log.Println(errors.New("could not get response from code explain bot"))
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Could not get response",
},
})
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论