Skip to content

Commit

Permalink
Simplify impl of message component edit_original_message helper fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
mkrasnitski committed Sep 8, 2023
1 parent ab08c9f commit 7008591
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions src/model/application/interaction/message_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use serde::Serialize;

#[cfg(feature = "http")]
use crate::builder::{
CreateInteractionResponse, CreateInteractionResponseFollowup, EditInteractionResponse,
CreateInteractionResponse,
CreateInteractionResponseFollowup,
EditInteractionResponse,
};
#[cfg(feature = "http")]
use crate::http::Http;
Expand Down Expand Up @@ -282,44 +284,25 @@ impl MessageComponentInteraction {
http.as_ref().get_followup_message(&self.token, message_id.into().into()).await
}

/// Responds to this interaction by editing the original message of a component or modal interaction.
/// Responds to this interaction by editing the original message of a component or modal
/// interaction.
///
/// **Note**: Message contents must be under 2000 unicode code points.
///
/// # Errors
///
/// Returns an [`Error::Model`] if the message content is too long.
/// May also return an [`Error::Http`] if the API returns an error,
/// or an [`Error::Json`] if there is an error in deserializing the
/// API response.
/// Returns an [`Error::Model`] if the message content is too long. May also return an
/// [`Error::Http`] if the API returns an error, or an [`Error::Json`] if there is an error in
/// deserializing the API response.
pub async fn edit_original_message<'a, F>(&self, http: impl AsRef<Http>, f: F) -> Result<()>
where
for<'b> F:
FnOnce(&'b mut CreateInteractionResponse<'a>) -> &'b mut CreateInteractionResponse<'a>,
{
let mut interaction_response = CreateInteractionResponse::default();
interaction_response.kind(InteractionResponseType::UpdateMessage);
f(&mut interaction_response);

let map = json::hashmap_to_json_map(interaction_response.0);

Message::check_content_length(&map)?;
Message::check_embed_length(&map)?;

if interaction_response.1.is_empty() {
http.as_ref()
.create_interaction_response(self.id.0, &self.token, &Value::from(map))
.await
} else {
http.as_ref()
.create_interaction_response_with_files(
self.id.0,
&self.token,
&Value::from(map),
interaction_response.1,
)
.await
}
self.create_interaction_response(http, |r| {
f(r).kind(InteractionResponseType::UpdateMessage)
})
.await
}

/// Helper function to defer an interaction
Expand Down

0 comments on commit 7008591

Please sign in to comment.