serenity-rs / poise

Discord bot command framework for serenity, with advanced features like edit tracking and flexible argument parsing
MIT License
653 stars 114 forks source link

How to edit an initial interaction response to add an attachment #157

Closed patrickjm closed 1 year ago

patrickjm commented 1 year ago

What's a good way to edit an initial command response in poise to add an attachment?

Looks like support for that was added to poise in #66, but I am having issues. The image doesn't appear in Discord when I do this:

reply
  .edit(ctx, |builder| {
    for (i, file) in data.into_iter().enumerate() {
      builder.attachment(serenity::AttachmentType::Bytes {
        data: Cow::Owned(file.to_owned()),
        filename: format!("image-{}.jpg", i),
      });
    }
    builder
  })
  .await?;

But doing something similar with Embeds does work.

Any advice appreciated, thank you.

kangalio commented 1 year ago

The linked PR implements support for attachments in initial command responses, but not in their edits. In fact, serenity doesn't have support for that itself: https://docs.rs/serenity/latest/serenity/builder/struct.EditInteractionResponse.html

I created a tracking issue for the missing serenity feature https://github.com/serenity-rs/serenity/issues/2412

kangalio commented 1 year ago

Actually, serenity has already had support for editing initial interaction response attachments, I just didn't notice it was on the next branch, which will be released with serenity 0.12.

Once serenity 0.12 releases, poise will update to it too. Alternatively you can use poise's serenity-next branch

patrickjm commented 1 year ago

Hi @kangalio, thanks for looking into that. I updated my project to point to the serenity-next branch, but this is still resulting in no images:

let mut response = CreateReply::new().content(format!(
    "{} - **{}**",
    "@User".to_string(),
    self.prompt.clone()
));
for (i, file) in data.drain(..).enumerate() {
    response = response.attachment(CreateAttachment::bytes(file, format!("{}.png", i)));
}

reply.edit(ctx, response).await?;

Does that mean I should call serenity directly through poise?

kangalio commented 1 year ago

Oops I forgot to implement support in poise

https://github.com/serenity-rs/poise/blob/50ddbbb85a66068e3de33a9ba3c6ff72e62596b6/src/reply/builder.rs#L171