From 39ffbe2cf7915f91a1c3237d5d4ecab612bdd60b Mon Sep 17 00:00:00 2001 From: Andrew Silver Date: Wed, 11 Sep 2024 14:56:39 +1000 Subject: [PATCH] Allow edited interactions to be sent without removing existing embeds Fixes #300 --- src/reply/builder.rs | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/reply/builder.rs b/src/reply/builder.rs index 980db844bf7a..f3201d615ae7 100644 --- a/src/reply/builder.rs +++ b/src/reply/builder.rs @@ -8,7 +8,7 @@ pub struct CreateReply { /// Message content. pub content: Option, /// Embeds, if present. - pub embeds: Vec, + pub embeds: Option>, /// Message attachments. pub attachments: Vec, /// Whether the message is ephemeral (only has an effect in application commands) @@ -32,9 +32,18 @@ impl CreateReply { /// Adds an embed to the message. /// - /// Existing embeds are kept. + /// Existing embeds on this are kept. + /// When editing a message, this will overwrite previously sent embeds. pub fn embed(mut self, embed: serenity::CreateEmbed) -> Self { - self.embeds.push(embed); + self.embeds.get_or_insert_with(|| Default::default()).push(embed); + self + } + + /// Set embeds for the message. + /// + /// Any previously set embeds will be overwritten. + pub fn embeds(mut self, embeds: Vec) -> Self { + self.embeds = Some(embeds); self } @@ -112,8 +121,11 @@ impl CreateReply { if let Some(ephemeral) = ephemeral { builder = builder.ephemeral(ephemeral); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } - builder.add_files(attachments).embeds(embeds) + builder.add_files(attachments) } /// Serialize this response builder to a [`serenity::CreateInteractionResponseFollowup`] @@ -135,7 +147,9 @@ impl CreateReply { if let Some(content) = content { builder = builder.content(content); } - builder = builder.embeds(embeds); + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } if let Some(components) = components { builder = builder.components(components) } @@ -174,8 +188,11 @@ impl CreateReply { if let Some(allowed_mentions) = allowed_mentions { builder = builder.allowed_mentions(allowed_mentions); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } - builder.embeds(embeds) + builder } /// Serialize this response builder to a [`serenity::EditMessage`] @@ -205,8 +222,11 @@ impl CreateReply { if let Some(components) = components { builder = builder.components(components); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } - builder.embeds(embeds).attachments(attachments_builder) + builder.attachments(attachments_builder) } /// Serialize this response builder to a [`serenity::CreateMessage`] @@ -235,6 +255,9 @@ impl CreateReply { if let Some(components) = components { builder = builder.components(components); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds) + } if reply { builder = builder.reference_message(invocation_message); } @@ -243,6 +266,6 @@ impl CreateReply { builder = builder.add_file(attachment); } - builder.embeds(embeds) + builder } }