From f2728141301cb88e3065c26d4459cb76dd2c9deb Mon Sep 17 00:00:00 2001 From: Andrew Silver Date: Wed, 11 Sep 2024 14:56:39 +1000 Subject: [PATCH 1/3] Allow edited interactions to be sent without removing existing embeds Fixes #300 # Conflicts: # src/reply/builder.rs --- 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 9692c757f1e..934bcb996fe 100644 --- a/src/reply/builder.rs +++ b/src/reply/builder.rs @@ -7,7 +7,7 @@ use crate::serenity_prelude as serenity; #[allow(clippy::missing_docs_in_private_items)] // docs on setters pub struct CreateReply { content: Option, - embeds: Vec, + embeds: Option>, attachments: Vec, pub(crate) ephemeral: Option, components: Option>, @@ -30,9 +30,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 } @@ -122,8 +131,11 @@ impl CreateReply { if let Some(poll) = poll { builder = builder.poll(poll); } + 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`] @@ -145,7 +157,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) } @@ -191,8 +205,11 @@ impl CreateReply { for attachment in attachments { builder = builder.new_attachment(attachment); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } - builder.embeds(embeds) + builder } /// Serialize this response builder to a [`serenity::EditMessage`] @@ -223,8 +240,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`] @@ -253,6 +273,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); } @@ -264,6 +287,6 @@ impl CreateReply { builder = builder.add_file(attachment); } - builder.embeds(embeds) + builder } } From 11c2282e582a1182ff5be489ec5cfd134c831662 Mon Sep 17 00:00:00 2001 From: Andrew Silver Date: Wed, 11 Sep 2024 15:05:27 +1000 Subject: [PATCH 2/3] Slight reorganization for consistency sake --- src/reply/builder.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/reply/builder.rs b/src/reply/builder.rs index 934bcb996fe..6496969908a 100644 --- a/src/reply/builder.rs +++ b/src/reply/builder.rs @@ -125,15 +125,15 @@ impl CreateReply { if let Some(components) = components { builder = builder.components(components); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } if let Some(ephemeral) = ephemeral { builder = builder.ephemeral(ephemeral); } if let Some(poll) = poll { builder = builder.poll(poll); } - if let Some(embeds) = embeds { - builder = builder.embeds(embeds); - } builder.add_files(attachments) } @@ -157,12 +157,12 @@ impl CreateReply { if let Some(content) = content { builder = builder.content(content); } - if let Some(embeds) = embeds { - builder = builder.embeds(embeds); - } if let Some(components) = components { builder = builder.components(components) } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } if let Some(allowed_mentions) = allowed_mentions { builder = builder.allowed_mentions(allowed_mentions); } @@ -199,15 +199,15 @@ impl CreateReply { if let Some(components) = components { builder = builder.components(components); } + if let Some(embeds) = embeds { + builder = builder.embeds(embeds); + } if let Some(allowed_mentions) = allowed_mentions { builder = builder.allowed_mentions(allowed_mentions); } for attachment in attachments { builder = builder.new_attachment(attachment); } - if let Some(embeds) = embeds { - builder = builder.embeds(embeds); - } builder } From 03a81f3aa7ba322eb06be2d920240544deec9671 Mon Sep 17 00:00:00 2001 From: Andrew Silver Date: Wed, 11 Sep 2024 23:45:57 +1000 Subject: [PATCH 3/3] Switched to Vec::new for the builder to mirror Serenity's builder pattern --- src/reply/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reply/builder.rs b/src/reply/builder.rs index 6496969908a..5cda021c894 100644 --- a/src/reply/builder.rs +++ b/src/reply/builder.rs @@ -33,7 +33,7 @@ impl CreateReply { /// 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.get_or_insert_with(|| Default::default()).push(embed); + self.embeds.get_or_insert_with(Vec::new).push(embed); self }