Skip to main content

stoat/builders/
edit_webhook.rs

1use stoat_models::v0::{DataEditWebhook, FieldsWebhook, Webhook};
2
3use crate::{HttpClient, error::Error};
4
5pub struct EditWebhookBuilder {
6    http: HttpClient,
7    webhook_id: String,
8    token: Option<String>,
9    data: DataEditWebhook,
10}
11
12impl EditWebhookBuilder {
13    pub fn new(http: HttpClient, webhook_id: String, token: Option<String>) -> Self {
14        Self {
15            http,
16            webhook_id,
17            token,
18            data: DataEditWebhook {
19                name: None,
20                avatar: None,
21                permissions: None,
22                remove: Vec::new(),
23            },
24        }
25    }
26
27    pub fn avatar(&mut self, avatar: Option<String>) -> &mut Self {
28        if avatar.is_some() {
29            self.data.avatar = avatar;
30        } else {
31            self.data.remove.push(FieldsWebhook::Avatar);
32        };
33
34        self
35    }
36
37    pub fn name(&mut self, name: String) -> &mut Self {
38        self.data.name = Some(name);
39
40        self
41    }
42
43    pub fn permissions(&mut self, permissions: u64) -> &mut Self {
44        self.data.permissions = Some(permissions);
45
46        self
47    }
48
49    pub async fn build(&self) -> Result<Webhook, Error> {
50        if let Some(token) = &self.token {
51            self.http
52                .edit_webhook_token(&self.webhook_id, &self.data, token)
53                .await
54        } else {
55            self.http.edit_webhook(&self.webhook_id, &self.data).await
56        }
57    }
58}