From 2652ae68e0344ee1b370386751399cd5a64297e7 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Sat, 10 Jun 2023 18:42:37 +0200 Subject: [PATCH] Add RoleObject::update() --- src/api/guilds/roles.rs | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/api/guilds/roles.rs b/src/api/guilds/roles.rs index a65b36d..9ebbb9c 100644 --- a/src/api/guilds/roles.rs +++ b/src/api/guilds/roles.rs @@ -161,4 +161,69 @@ impl types::RoleObject { }; Ok(role) } + + /// Updates a role in a guild. + /// + /// # Arguments + /// + /// * `user` - A mutable reference to a [`UserMeta`] instance. + /// * `guild_id` - The ID of the guild to update the role in. + /// * `role_id` - The ID of the role to update. + /// * `role_create_schema` - A [`RoleCreateModifySchema`] instance containing the new properties 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 update( + user: &mut UserMeta, + guild_id: &str, + role_id: &str, + role_create_schema: RoleCreateModifySchema, + ) -> Result { + let mut belongs_to = user.belongs_to.borrow_mut(); + let url = format!( + "{}/guilds/{}/roles/{}", + belongs_to.urls.get_api(), + guild_id, + role_id + ); + let body = match to_string::(&role_create_schema) { + Ok(string) => string, + Err(e) => { + return Err(ChorusLibError::FormCreationError { + error: e.to_string(), + }) + } + }; + let request = Client::new() + .patch(url) + .bearer_auth(user.token()) + .body(body); + let result = 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 = match from_str(&result.text().await.unwrap()) { + Ok(role) => role, + Err(e) => { + return Err(ChorusLibError::InvalidResponseError { + error: e.to_string(), + }) + } + }; + Ok(role) + } }