Implement RoleObject::get()

This commit is contained in:
Flori Weber 2023-06-10 18:48:37 +02:00
parent 2652ae68e0
commit f0e3933419
1 changed files with 46 additions and 0 deletions

View File

@ -52,6 +52,52 @@ impl types::RoleObject {
Ok(Some(roles))
}
/// Retrieves a single role for a given guild.
///
/// # Arguments
///
/// * `user` - A mutable reference to a [`UserMeta`] instance.
/// * `guild_id` - The ID of the guild to retrieve the role from.
/// * `role_id` - The ID of the role to retrieve.
///
/// # Returns
///
/// A `Result` containing the retrieved [`RoleObject`] if successful, or a [`ChorusLibError`] if the request fails or if the response is invalid.
///
/// # Errors
///
/// Returns a [`ChorusLibError`] if the request fails or if the response is invalid.
pub async fn get(
user: &mut UserMeta,
guild_id: &str,
role_id: &str,
) -> Result<RoleObject, crate::errors::ChorusLibError> {
let mut belongs_to = user.belongs_to.borrow_mut();
let url = format!(
"{}/guilds/{}/roles/{}/",
belongs_to.urls.get_api(),
guild_id,
role_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 role: RoleObject = from_str(&requester.text().await.unwrap()).unwrap();
Ok(role)
}
/// Creates a new role for a given guild.
///
/// # Arguments