Skip to content

Commit

Permalink
Rework EditWebhookMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Jun 27, 2022
1 parent be3f947 commit 060addd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
70 changes: 63 additions & 7 deletions src/builder/edit_webhook_message.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
#[cfg(not(feature = "http"))]
use std::marker::PhantomData;

use super::{CreateAllowedMentions, CreateComponents, CreateEmbed};
#[cfg(feature = "http")]
use crate::http::Http;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
use crate::model::prelude::*;

/// A builder to specify the fields to edit in an existing [`Webhook`]'s message.
///
/// [`Webhook`]: crate::model::webhook::Webhook
#[derive(Clone, Debug, Default, Serialize)]
pub struct EditWebhookMessage {
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct EditWebhookMessage<'a> {
#[serde(skip)]
#[cfg(feature = "http")]
webhook: &'a Webhook,
#[cfg(not(feature = "http"))]
webhook: PhantomData<&'a ()>,
#[cfg(feature = "http")]
#[serde(skip)]
message_id: MessageId,

#[serde(skip_serializing_if = "Option::is_none")]
content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -15,12 +34,31 @@ pub struct EditWebhookMessage {
components: Option<CreateComponents>,
}

impl EditWebhookMessage {
impl<'a> EditWebhookMessage<'a> {
pub fn new(
#[cfg(feature = "http")] webhook: &'a Webhook,
#[cfg(feature = "http")] message_id: MessageId,
) -> Self {
Self {
#[cfg(feature = "http")]
webhook,
#[cfg(not(feature = "http"))]
webhook: PhantomData::default(),
#[cfg(feature = "http")]
message_id,

content: None,
embeds: None,
allowed_mentions: None,
components: None,
}
}

/// Set the content of the message.
///
/// **Note**: Message contents must be under 2000 unicode code points.
#[inline]
pub fn content(&mut self, content: impl Into<String>) -> &mut Self {
pub fn content(mut self, content: impl Into<String>) -> Self {
self.content = Some(content.into());
self
}
Expand All @@ -34,13 +72,13 @@ impl EditWebhookMessage {
///
/// [struct-level documentation of `ExecuteWebhook`]: crate::builder::ExecuteWebhook#examples
#[inline]
pub fn embeds(&mut self, embeds: Vec<CreateEmbed>) -> &mut Self {
pub fn embeds(mut self, embeds: Vec<CreateEmbed>) -> Self {
self.embeds = Some(embeds);
self
}

/// Set the allowed mentions for the message.
pub fn allowed_mentions<F>(&mut self, f: F) -> &mut Self
pub fn allowed_mentions<F>(mut self, f: F) -> Self
where
F: FnOnce(&mut CreateAllowedMentions) -> &mut CreateAllowedMentions,
{
Expand All @@ -57,7 +95,7 @@ impl EditWebhookMessage {
///
/// [`WebhookType::Application`]: crate::model::webhook::WebhookType
/// [`WebhookType::Incoming`]: crate::model::webhook::WebhookType
pub fn components<F>(&mut self, f: F) -> &mut Self
pub fn components<F>(mut self, f: F) -> Self
where
F: FnOnce(&mut CreateComponents) -> &mut CreateComponents,
{
Expand All @@ -67,4 +105,22 @@ impl EditWebhookMessage {
self.components = Some(components);
self
}

/// Edits the webhook message.
///
/// # Errors
///
/// Returns an [`Error::Model`] if the token field of the current webhook is `None`.
///
/// May also return an [`Error::Http`] if the content is malformed, the webhook's token is
/// invalid, or the given message Id does not belong to the current webhook.
///
/// Or may return an [`Error::Json`] if there is an error deserialising Discord's response.
#[cfg(feature = "http")]
pub async fn execute(self, http: impl AsRef<Http>) -> Result<Message> {
let token = self.webhook.token.as_ref().ok_or(ModelError::NoTokenSet)?;
http.as_ref()
.edit_webhook_message(self.webhook.id.into(), token, self.message_id.into(), &self)
.await
}
}
31 changes: 3 additions & 28 deletions src/model/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,34 +419,9 @@ impl Webhook {
http.as_ref().get_webhook_message(self.id.get(), token, message_id.get()).await
}

/// Edits a webhook message with the fields set via the given builder.
///
/// # Errors
///
/// Returns an [`Error::Model`] if the [`Self::token`] is [`None`].
///
/// May also return an [`Error::Http`] if the content is malformed, the webhook's token is invalid, or
/// the given message Id does not belong to the current webhook.
///
/// Or may return an [`Error::Json`] if there is an error deserialising Discord's response.
///
/// [`Error::Model`]: crate::error::Error::Model
/// [`Error::Http`]: crate::error::Error::Http
/// [`Error::Json`]: crate::error::Error::Json
pub async fn edit_message<F>(
&self,
http: impl AsRef<Http>,
message_id: MessageId,
f: F,
) -> Result<Message>
where
F: FnOnce(&mut EditWebhookMessage) -> &mut EditWebhookMessage,
{
let token = self.token.as_ref().ok_or(ModelError::NoTokenSet)?;
let mut builder = EditWebhookMessage::default();
f(&mut builder);

http.as_ref().edit_webhook_message(self.id.get(), token, message_id.get(), &builder).await
/// Returns a request builder that edits a webhook message when executed.
pub fn edit_message(&self, message_id: MessageId) -> EditWebhookMessage<'_> {
EditWebhookMessage::new(self, message_id)
}

/// Deletes a webhook message.
Expand Down

0 comments on commit 060addd

Please sign in to comment.