Implement RoleObject::get_all()

This commit is contained in:
bitfl0wer 2023-06-07 23:03:24 +02:00
parent afe9663ded
commit 32503078d7
No known key found for this signature in database
GPG Key ID: 84BBB60DF895ABF2
1 changed files with 39 additions and 0 deletions

39
src/api/guilds/roles.rs Normal file
View File

@ -0,0 +1,39 @@
use reqwest::Client;
use serde_json::from_str;
use crate::{
instance::UserMeta,
limit::LimitedRequester,
types::{self, RoleObject},
};
impl types::RoleObject {
pub async fn get_all(
user: &mut UserMeta,
guild_id: &str,
) -> Result<Option<Vec<RoleObject>>, crate::errors::InstanceServerError> {
let mut belongs_to = user.belongs_to.borrow_mut();
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
let request = Client::new().get(url).bearer_auth(user.token());
let requester = match LimitedRequester::new()
.await
.send_request(
request,
crate::api::limits::LimitType::Guild,
&mut belongs_to.limits,
&mut user.limits,
)
.await
{
Ok(request) => request,
Err(e) => return Err(e),
};
let roles: Vec<RoleObject> = from_str(&requester.text().await.unwrap()).unwrap();
if roles.is_empty() {
return Ok(None);
}
Ok(Some(roles))
}
}