serenity/builder/
edit_webhook.rs

1#[cfg(feature = "http")]
2use super::Builder;
3use super::CreateAttachment;
4#[cfg(feature = "http")]
5use crate::http::CacheHttp;
6#[cfg(feature = "http")]
7use crate::internal::prelude::*;
8use crate::model::prelude::*;
9
10/// [Discord docs](https://discord.com/developers/docs/resources/webhook#modify-webhook)
11#[derive(Debug, Default, Clone, Serialize)]
12#[must_use]
13pub struct EditWebhook<'a> {
14    #[serde(skip_serializing_if = "Option::is_none")]
15    name: Option<String>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    avatar: Option<Option<String>>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    channel_id: Option<ChannelId>,
20
21    #[serde(skip)]
22    audit_log_reason: Option<&'a str>,
23}
24
25impl<'a> EditWebhook<'a> {
26    /// Equivalent to [`Self::default`].
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Set the webhook's name.
32    ///
33    /// This must be between 1-80 characters.
34    pub fn name(mut self, name: impl Into<String>) -> Self {
35        self.name = Some(name.into());
36        self
37    }
38
39    /// Set the channel to move the webhook to.
40    pub fn channel_id(mut self, channel_id: impl Into<ChannelId>) -> Self {
41        self.channel_id = Some(channel_id.into());
42        self
43    }
44
45    /// Set the webhook's default avatar.
46    pub fn avatar(mut self, avatar: &CreateAttachment) -> Self {
47        self.avatar = Some(Some(avatar.to_base64()));
48        self
49    }
50
51    /// Delete the webhook's avatar, resetting it to the default logo.
52    pub fn delete_avatar(mut self) -> Self {
53        self.avatar = Some(None);
54        self
55    }
56
57    /// Sets the request's audit log reason.
58    pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
59        self.audit_log_reason = Some(reason);
60        self
61    }
62}
63
64#[cfg(feature = "http")]
65#[async_trait::async_trait]
66impl Builder for EditWebhook<'_> {
67    type Context<'ctx> = (WebhookId, Option<&'ctx str>);
68    type Built = Webhook;
69
70    /// Edits the webhook corresponding to the provided [`WebhookId`] and token, and returns the
71    /// resulting new [`Webhook`].
72    ///
73    /// # Errors
74    ///
75    /// Returns [`Error::Http`] if the content is malformed, or if the token is invalid.
76    ///
77    /// Returns [`Error::Json`] if there is an error in deserialising Discord's response.
78    async fn execute(
79        self,
80        cache_http: impl CacheHttp,
81        ctx: Self::Context<'_>,
82    ) -> Result<Self::Built> {
83        match ctx.1 {
84            Some(token) => {
85                cache_http
86                    .http()
87                    .edit_webhook_with_token(ctx.0, token, &self, self.audit_log_reason)
88                    .await
89            },
90            None => cache_http.http().edit_webhook(ctx.0, &self, self.audit_log_reason).await,
91        }
92    }
93}