Skip to content

Commit

Permalink
Add Webhook::get_message for retrieving messages sent by a webhook (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Apr 1, 2022
1 parent 4cd6caa commit a2b388f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/http/client.rs
Expand Up @@ -1960,6 +1960,25 @@ impl Http {
response.json::<Message>().await.map(Some).map_err(From::from)
}

// Gets a webhook's message by Id
pub async fn get_webhook_message(
&self,
webhook_id: u64,
token: &str,
message_id: u64,
) -> Result<Message> {
self.fire(Request {
body: None,
headers: None,
route: RouteInfo::GetWebhookMessage {
token,
webhook_id,
message_id,
},
})
.await
}

/// Edits a webhook's message by Id.
pub async fn edit_webhook_message(
&self,
Expand Down
14 changes: 14 additions & 0 deletions src/http/routing.rs
Expand Up @@ -1485,6 +1485,11 @@ pub enum RouteInfo<'a> {
token: &'a str,
webhook_id: u64,
},
GetWebhookMessage {
token: &'a str,
webhook_id: u64,
message_id: u64,
},
KickMember {
guild_id: u64,
user_id: u64,
Expand Down Expand Up @@ -2103,6 +2108,15 @@ impl<'a> RouteInfo<'a> {
Route::WebhooksId(webhook_id),
Cow::from(Route::webhook_with_token(webhook_id, token)),
),
RouteInfo::GetWebhookMessage {
token,
webhook_id,
message_id,
} => (
LightMethod::Get,
Route::WebhooksIdMessagesId(webhook_id),
Cow::from(Route::webhook_message(webhook_id, token, message_id)),
),
RouteInfo::EditWebhookMessage {
token,
webhook_id,
Expand Down
24 changes: 24 additions & 0 deletions src/model/webhook.rs
Expand Up @@ -316,6 +316,30 @@ impl Webhook {
}
}

/// Gets a previously sent message from the webhook.
///
/// # Errors
///
/// Returns an [`Error::Model`] if the [`Self::token`] is [`None`].
///
/// May also return [`Error::Http`] if 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 get_message(
&self,
http: impl AsRef<Http>,
message_id: MessageId,
) -> Result<Message> {
let token = self.token.as_ref().ok_or(ModelError::NoTokenSet)?;

http.as_ref().get_webhook_message(self.id.0, token, message_id.0).await
}

/// Edits a webhook message with the fields set via the given builder.
///
/// # Errors
Expand Down

0 comments on commit a2b388f

Please sign in to comment.