use crate::Client;
use crate::errors::{Error, Result};
use crate::methods::accepted;
use crate::raw::functions;
use crate::types::PeerRef;
#[must_use = "a request does nothing until it is awaited"]
pub struct SetManagedBotAccessSettings<'a> {
client: &'a Client,
bot_id: PeerRef,
access: Option<Access>,
}
enum Access {
Everyone,
Only(Vec<PeerRef>),
}
impl Client {
pub fn set_managed_bot_access_settings(&self, bot_id: impl Into<PeerRef>) -> SetManagedBotAccessSettings<'_> {
SetManagedBotAccessSettings { client: self, bot_id: bot_id.into(), access: None }
}
}
impl SetManagedBotAccessSettings<'_> {
pub fn allow_everyone(mut self) -> Self {
self.access = Some(Access::Everyone);
self
}
pub fn restrict_to(mut self, users: impl IntoIterator<Item = impl Into<PeerRef>>) -> Self {
self.access = Some(Access::Only(users.into_iter().map(Into::into).collect()));
self
}
pub fn restrict_to_owner(mut self) -> Self {
self.access = Some(Access::Only(Vec::new()));
self
}
async fn send(self) -> Result<()> {
let Some(access) = self.access else {
return Err(Error::InvalidArgument(
"set_managed_bot_access_settings needs allow_everyone, restrict_to or restrict_to_owner",
));
};
let bot = self.client.resolve_user(&self.bot_id).await?;
let add_users = match access {
Access::Only(users) => Some(self.client.resolve_users(users).await?),
Access::Everyone => None,
};
let request = functions::bots::EditAccessSettings { restricted: add_users.is_some(), bot, add_users };
accepted(self.client.invoke(&request).await?)
}
}
into_future!(SetManagedBotAccessSettings, ());