1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::prelude::*;
use rive_models::{
    channel::Channel,
    payload::{CreateChannelPayload, CreateServerPayload, EditServerPayload},
    server::Server,
};

impl Client {
    /// Create a new server.
    pub async fn create_server(&self, payload: CreateServerPayload) -> Result<Server> {
        Ok(self
            .client
            .post(ep!(self, "/servers/create"))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Deletes a server if owner otherwise leaves.
    pub async fn fetch_server(&self, id: impl Into<String>) -> Result<Server> {
        Ok(self
            .client
            .get(ep!(self, "/servers/{}", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Deletes a server if owner otherwise leaves.
    pub async fn delete_or_leave_server(&self, id: impl Into<String>) -> Result<()> {
        self.client
            .delete(ep!(self, "/servers/{}", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?;
        Ok(())
    }

    /// Edit a server by its id.
    pub async fn edit_server(
        &self,
        id: impl Into<String>,
        payload: EditServerPayload,
    ) -> Result<Server> {
        Ok(self
            .client
            .patch(ep!(self, "/servers/{}", id.into()))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Mark all channels in a server as read.
    pub async fn mark_server_as_read(&self, id: impl Into<String>) -> Result<()> {
        self.client
            .put(ep!(self, "/servers/{}/ack", id.into()))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?;
        Ok(())
    }

    /// Create a new Text or Voice channel
    pub async fn create_channel(
        &self,
        server_id: impl Into<String>,
        payload: CreateChannelPayload,
    ) -> Result<Channel> {
        Ok(self
            .client
            .post(ep!(self, "/servers/{}/channels", server_id.into()))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }
}