使用discord.go,我如何从斜杠命令选项中检索消息附件?

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

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
	}

huangapple
  • 本文由 发表于 2023年1月24日 07:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75215946.html
匿名

发表评论

匿名网友

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

确定