1use robespierre_models::{
2 bot::{Bot, BotField, PublicBot},
3 id::{ChannelId, ServerId, UserId},
4 users::User,
5};
6
7use super::impl_prelude::*;
8
9impl Http {
10 pub async fn create_bot(&self, name: &str) -> Result<Bot> {
11 #[derive(serde::Serialize)]
12 struct CreateBotRequest<'a> {
13 name: &'a str,
14 }
15
16 Ok(self
17 .client_user_session_auth_type()
18 .post(ep!(self, "/bots/create"))
19 .json(&CreateBotRequest { name })
20 .send()
21 .await?
22 .error_for_status()?
23 .json()
24 .await?)
25 }
26
27 pub async fn fetch_owned_bots(&self) -> Result<FetchOwnedBotsResponse> {
28 Ok(self
29 .client_user_session_auth_type()
30 .get(ep!(self, "/bots/@me"))
31 .send()
32 .await?
33 .error_for_status()?
34 .json()
35 .await?)
36 }
37
38 pub async fn fetch_bot(&self, bot: UserId) -> Result<FetchBotResponse> {
39 Ok(self
40 .client_user_session_auth_type()
41 .get(ep!(self, "/bots/{}" bot))
42 .send()
43 .await?
44 .error_for_status()?
45 .json()
46 .await?)
47 }
48
49 pub async fn edit_bot(
50 &self,
51 bot: UserId,
52 name: Option<&str>,
53 public: Option<bool>,
54 interactions_url: Option<&str>,
55 remove: Option<BotField>,
56 ) -> Result {
57 #[derive(serde::Serialize)]
58 struct EditBotRequest<'a> {
59 #[serde(skip_serializing_if = "Option::is_none")]
60 name: Option<&'a str>,
61 #[serde(skip_serializing_if = "Option::is_none")]
62 public: Option<bool>,
63 #[serde(skip_serializing_if = "Option::is_none")]
64 interactions_url: Option<&'a str>,
65 #[serde(skip_serializing_if = "Option::is_none")]
66 remove: Option<BotField>,
67 }
68
69 self.client_user_session_auth_type()
70 .patch(ep!(self, "/bots/{}" bot))
71 .json(&EditBotRequest {
72 name,
73 public,
74 interactions_url,
75 remove,
76 })
77 .send()
78 .await?
79 .error_for_status()?;
80 Ok(())
81 }
82
83 pub async fn delete_bot(&self, bot: UserId) -> Result {
84 self.client_user_session_auth_type()
85 .delete(ep!(self, "/bots/{}" bot))
86 .send()
87 .await?
88 .error_for_status()?;
89
90 Ok(())
91 }
92
93 pub async fn fetch_public_bot(&self, bot: UserId) -> Result<PublicBot> {
94 Ok(self
95 .client_user_session_auth_type()
96 .get(ep!(self, "/bots/{}/invite" bot))
97 .send()
98 .await?
99 .error_for_status()?
100 .json()
101 .await?)
102 }
103
104 pub async fn invite_bot(&self, bot: UserId, target: InviteBotTarget) -> Result {
105 self.client_user_session_auth_type()
106 .post(ep!(self, "/bots/{}/invite" bot))
107 .json(&target)
108 .send()
109 .await?
110 .error_for_status()?;
111
112 Ok(())
113 }
114}
115
116#[derive(serde::Deserialize)]
117pub struct FetchOwnedBotsResponse {
118 pub bots: Vec<Bot>,
119 pub users: Vec<User>,
120}
121
122#[derive(serde::Deserialize)]
123pub struct FetchBotResponse {
124 pub bot: Bot,
125 pub user: User,
126}
127
128pub enum InviteBotTarget {
129 Server(ServerId),
130 Group(ChannelId),
131}
132
133impl serde::Serialize for InviteBotTarget {
134 fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
135 where
136 S: serde::Serializer,
137 {
138 match *self {
139 InviteBotTarget::Server(server) => {
140 #[derive(serde::Serialize)]
141 struct TargetServer {
142 server: ServerId,
143 }
144
145 TargetServer { server }.serialize(serializer)
146 }
147 InviteBotTarget::Group(group) => {
148 #[derive(serde::Serialize)]
149 struct TargetGroup {
150 group: ChannelId,
151 }
152
153 TargetGroup { group }.serialize(serializer)
154 }
155 }
156 }
157}