Skip to content

Commit

Permalink
Allowed embeds to be optional in followup messages, to prevent edits …
Browse files Browse the repository at this point in the history
…deleting them.
  • Loading branch information
TapGhoul committed Sep 11, 2024
1 parent 3fb1f37 commit 425dd27
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/builder/create_interaction_response_followup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub struct CreateInteractionResponseFollowup {
// [Omitting avatar_url: not supported in interaction followups]
#[serde(skip_serializing_if = "Option::is_none")]
tts: Option<bool>,
embeds: Vec<CreateEmbed>,
#[serde(skip_serializing_if = "Option::is_none")]
embeds: Option<Vec<CreateEmbed>>,
#[serde(skip_serializing_if = "Option::is_none")]
allowed_mentions: Option<CreateAllowedMentions>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -48,10 +49,12 @@ impl CreateInteractionResponseFollowup {
.map_err(|overflow| Error::Model(ModelError::MessageTooLong(overflow)))?;
}

check_overflow(self.embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in &self.embeds {
embed.check_length()?;
if let Some(embeds) = &self.embeds {
check_overflow(embeds.len(), constants::EMBED_MAX_COUNT)
.map_err(|_| Error::Model(ModelError::EmbedAmount))?;
for embed in embeds {
embed.check_length()?;
}
}

Ok(())
Expand Down Expand Up @@ -101,13 +104,13 @@ impl CreateInteractionResponseFollowup {

/// Adds an embed to the message.
pub fn add_embed(mut self, embed: CreateEmbed) -> Self {
self.embeds.push(embed);
self.embeds.get_or_insert_with(Vec::new).push(embed);
self
}

/// Adds multiple embeds to the message.
pub fn add_embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
self.embeds.extend(embeds);
self.embeds.get_or_insert_with(Vec::new).extend(embeds);
self
}

Expand All @@ -124,7 +127,7 @@ impl CreateInteractionResponseFollowup {
/// Calling this multiple times will overwrite the embed list. To append embeds, call
/// [`Self::add_embeds`] instead.
pub fn embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
self.embeds = embeds;
self.embeds = Some(embeds);
self
}

Expand Down

0 comments on commit 425dd27

Please sign in to comment.