zaun_client/bots/
community.rs1use zaun_community_protocol::bits::Permissions;
5use zaun_community_protocol::command::app;
6use zaun_community_protocol::command::bot::{
7 self, Bots, Disable, Enable, GrantChoice, Install, Register, Remove, Target,
8};
9use zaun_community_protocol::command::object::{ApplicationCommand, BotCard, Capabilities};
10use zaun_community_protocol::ids::{AttachmentId, BotId};
11
12use crate::error::Result;
13use crate::objects::{Community, Member, Role};
14
15impl Community {
16 #[must_use]
18 pub fn commands(&self) -> Vec<ApplicationCommand> {
19 self.read(|c| c.commands.values().cloned().collect())
20 .unwrap_or_default()
21 }
22
23 #[must_use]
25 pub fn bots(&self) -> Vec<BotCard> {
26 self.read(|c| c.bots.values().cloned().collect())
27 .unwrap_or_default()
28 }
29
30 pub fn fetch_bots(&self) -> Result<Vec<BotCard>> {
35 let page: Bots = self.client.command(self.id, None, bot::List {})?;
36 Ok(page.bots)
37 }
38
39 pub fn bot(&self, bot: BotId) -> Result<BotCard> {
44 self.client.command(self.id, None, Target { bot_id: bot })
45 }
46
47 pub fn install_bot(
53 &self,
54 package: AttachmentId,
55 grants: Option<GrantChoice>,
56 role_position: Option<u32>,
57 ) -> Result<BotCard> {
58 let body = Install {
59 package,
60 grants,
61 role_position,
62 source: None,
63 };
64 self.client.command(self.id, None, body)
65 }
66
67 pub fn update_bot(&self, bot: BotId, package: AttachmentId) -> Result<BotCard> {
72 self.client.command(
73 self.id,
74 None,
75 bot::Update {
76 bot_id: bot,
77 package,
78 source: None,
79 },
80 )
81 }
82
83 pub fn grant_bot(
88 &self,
89 bot: BotId,
90 permissions: Option<Permissions>,
91 capabilities: Option<Capabilities>,
92 ) -> Result<BotCard> {
93 let body = bot::Grant {
94 bot_id: bot,
95 permissions,
96 capabilities,
97 };
98 self.client.command(self.id, None, body)
99 }
100
101 pub fn enable_bot(&self, bot: BotId) -> Result<BotCard> {
106 self.client.command(self.id, None, Enable { bot_id: bot })
107 }
108
109 pub fn disable_bot(&self, bot: BotId) -> Result<BotCard> {
114 self.client.command(self.id, None, Disable { bot_id: bot })
115 }
116
117 pub fn remove_bot(&self, bot: BotId, keep_data: bool) -> Result<()> {
122 let body = Remove {
123 bot_id: bot,
124 delete_data: Some(!keep_data),
125 };
126 self.client.command(self.id, None, body).map(|_| ())
127 }
128
129 pub fn register_bot(&self, member: &Member, name: Option<&str>) -> Result<BotCard> {
134 let body = Register {
135 member_id: member.id,
136 name: name.map(str::to_owned),
137 };
138 self.client.command(self.id, None, body)
139 }
140
141 pub fn set_commands(
146 &self,
147 commands: Vec<ApplicationCommand>,
148 ) -> Result<Vec<ApplicationCommand>> {
149 let set: app::Commands = self.client.command(self.id, None, app::Set { commands })?;
150 Ok(set.commands)
151 }
152}
153
154impl Member {
155 #[must_use]
157 pub fn bot_id(&self) -> Option<BotId> {
158 self.community
159 .read(|c| c.members.get(&self.id).and_then(|m| m.bot_id))
160 .flatten()
161 }
162
163 #[must_use]
165 pub fn is_bot(&self) -> bool {
166 self.bot_id().is_some()
167 }
168}
169
170impl Role {
171 #[must_use]
174 pub fn managed_by(&self) -> Option<BotId> {
175 self.community
176 .read(|c| c.roles.get(&self.id).and_then(|r| r.managed_by))
177 .flatten()
178 }
179}
180
181impl Member {
182 #[must_use]
184 pub fn can_use(&self, command: &ApplicationCommand, channel: &crate::Channel) -> bool {
185 let now = self.community.client.now_ms();
186 self.community
187 .read(|c| c.can_use(&self.id, &channel.id, command, now))
188 .unwrap_or(false)
189 }
190}