zekroTJA/ken

Embed Editing doesn't seem to work?

cybellereaper opened this issue · 4 comments

I've been trying to edit embeds with the new "api" it doesn't seem to work.

Can you show me how I can reproduce the issue?

Sure, here's some code:

func (*ImageboardCommand) Run(ctx ken.Context) (err error) {
	if err = ctx.Defer(); err != nil {
		return
	}
	bd := AssertArgument(ctx)
	message := ctx.FollowUpEmbed(bd.Modal())
	message.AddComponents(func(cb *ken.ComponentBuilder) {
		cb.AddActionsRow(func(b ken.ComponentAssembler) {
			b.Add(discordgo.Button{
				CustomID: fmt.Sprint(rand.Intn(3000)),
				Style:    discordgo.PrimaryButton,
				Label:    "⏭️",
			}, func(ctx ken.ComponentContext) bool {
				bd.Next()
				message.Send().EditEmbed(bd.Modal())
				return true
			})
		})
	})
	message.Send()
	return err
}

Yeah, there are definitely some Issues with your code.

First of all, ctx.FollowUpEmbed does now return a *FollowUpMessageBuilder which can be turned into a *FollowUpMessage by using the Send method on it.

So, the second call to Send in your component handler does not make a lot of sense, because the message has already been sent. You need to capture the resulting *FollowUpMessage and perform EditEmbed on that.

Something like this should work.

func (*ImageboardCommand) Run(ctx ken.Context) (err error) {
	if err = ctx.Defer(); err != nil {
		return
	}

        var fum *ken.FollowUpMessage

	bd := AssertArgument(ctx)
	message := ctx.FollowUpEmbed(bd.Modal())
	message.AddComponents(func(cb *ken.ComponentBuilder) {
		cb.AddActionsRow(func(b ken.ComponentAssembler) {
			b.Add(discordgo.Button{
				CustomID: fmt.Sprint(rand.Intn(3000)),
				Style:    discordgo.PrimaryButton,
				Label:    "⏭️",
			}, func(ctx ken.ComponentContext) bool {
				bd.Next()
				fum.EditEmbed(bd.Modal())
				return true
			})
		})
	})

	fum = message.Send()

	return fum.Error
}

Also, because Send does not directly return an error, the errors in the call chain are captured in the FollowUpMessage's property Error, which should be checked after performing actions like Send or EditEmbed. I know that this approach is not very easy to see through but I am working on it to make the API more understandable and usable. 😄

Ah, alright thanks for the advice!