Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot reuse uploaded file as attachment #1551

Open
toby3d opened this issue Aug 2, 2024 · 0 comments
Open

Cannot reuse uploaded file as attachment #1551

toby3d opened this issue Aug 2, 2024 · 0 comments

Comments

@toby3d
Copy link

toby3d commented Aug 2, 2024

I'm trying to implement a simple cache for regularly used files in slash command responses:

const SlashCommandName string = "poster"

// ...

func (i *Interaction) ServeInteraction(session *discordgo.Session, event *discordgo.InteractionCreate) {
	if event.Type != discordgo.InteractionApplicationCommand {
		return
	}

	data := event.ApplicationCommandData()
	if data.CommandType != discordgo.ChatApplicationCommand || !strings.EqualFold(data.Name, SlashCommandName) {
		return
	}

	go session.InteractionRespond(event.Interaction, &discordgo.InteractionResponse{
		Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
	})

	poster, err := i.chooser.Reveal(context.Background(), language.Make(string(event.Locale)))
	if err != nil {
		log.Fatalln(err)
	}
	defer poster.Release()

	// NOTE(toby3d): check what this poster is uploaded early, reuse it.
	if cachedMessageAttachment, cached := i.cache.Load("poster:" + poster.Name()); cached {
		if _, err = session.InteractionResponseEdit(event.Interaction, &discordgo.WebhookEdit{
			Attachments: &[]*discordgo.MessageAttachment{
				cachedMessageAttachment.(*discordgo.MessageAttachment),
			},
		}); err != nil {
			log.Fatalln("cannot send cached poster by URL:", err)
		}

		return
	}

	// NOTE(toby3d): it's a first time: upload poster as file, then store attachment in cache
	message, err := session.InteractionResponseEdit(event.Interaction, &discordgo.WebhookEdit{
		Files: []*discordgo.File{{
			Name:        poster.Name(),
			ContentType: poster.ContentType(),
			Reader:      poster.Image(),
		}},
		Attachments: &[]*discordgo.MessageAttachment{{
			ID:  "0",
			URL: "attachment://" + poster.Name(),
		}},
	})
	if err != nil {
		log.Fatalln(err)
	}

	i.cache.Store("poster:"+poster.Name(), message.Attachments[0])
}

But sending data from the cache regularly results in an error:

cannot send cached poster by URL: HTTP 400 Bad Request, {"message": "Invalid Form Body", "code": 50035, "errors": {"attachments": {"0": {"_errors": [{"code": "ATTACHMENT_NOT_FOUND", "message": "Attachment data not found"}]}}}}
exit status 1

If I use just attachment URL, then I got error about "Invalid attachment ID".

If I use the URL of the uploaded file as an image, however, it embeds with a border that I want to avoid:
image

However, if I use the URL of the uploaded file as is, using it in Content, it uploads at a smaller size than the original in the client interface:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant