From 6ed9eaf2fcc8b1ac1c529cbfde4de9dd98fa6674 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 9 Jun 2023 16:50:03 +0200 Subject: [PATCH] Implement RoleObject::position_update() --- src/api/guilds/roles.rs | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/api/guilds/roles.rs b/src/api/guilds/roles.rs index 482a859..a65b36d 100644 --- a/src/api/guilds/roles.rs +++ b/src/api/guilds/roles.rs @@ -106,4 +106,59 @@ impl types::RoleObject { }; Ok(role) } + + /// Updates the position of a role in the guild's hierarchy. + /// + /// # Arguments + /// + /// * `user` - A mutable reference to a [`UserMeta`] instance. + /// * `guild_id` - The ID of the guild to update the role position in. + /// * `role_position_update_schema` - A [`RolePositionUpdateSchema`] instance containing the new position of the role. + /// + /// # Returns + /// + /// A `Result` containing the updated [`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 position_update( + user: &mut UserMeta, + guild_id: &str, + role_position_update_schema: types::RolePositionUpdateSchema, + ) -> Result { + let mut belongs_to = user.belongs_to.borrow_mut(); + let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id); + let body = match to_string(&role_position_update_schema) { + Ok(body) => body, + Err(e) => { + return Err(ChorusLibError::FormCreationError { + error: e.to_string(), + }) + } + }; + let request = Client::new() + .patch(url) + .bearer_auth(user.token()) + .body(body); + let response = LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Guild, + &mut belongs_to.limits, + &mut user.limits, + ) + .await + .unwrap(); + let role: RoleObject = match from_str(&response.text().await.unwrap()) { + Ok(role) => role, + Err(e) => { + return Err(ChorusLibError::InvalidResponseError { + error: e.to_string(), + }) + } + }; + Ok(role) + } }