#[cfg(feature = "http")]
use super::Builder;
use super::CreateAttachment;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
use crate::model::prelude::*;
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateWebhook<'a> {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
avatar: Option<String>,
#[serde(skip)]
audit_log_reason: Option<&'a str>,
}
impl<'a> CreateWebhook<'a> {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
avatar: None,
audit_log_reason: None,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
pub fn avatar(mut self, avatar: &CreateAttachment) -> Self {
self.avatar = Some(avatar.to_base64());
self
}
pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
self.audit_log_reason = Some(reason);
self
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for CreateWebhook<'_> {
type Context<'ctx> = ChannelId;
type Built = Webhook;
async fn execute(
self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
if self.name.len() < 2 {
return Err(Error::Model(ModelError::NameTooShort));
} else if self.name.len() > 100 {
return Err(Error::Model(ModelError::NameTooLong));
}
cache_http.http().create_webhook(ctx, &self, self.audit_log_reason).await
}
}