From 833c3733d7db7a91646a296caab53dbe0d170901 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 14 May 2023 14:16:21 +0200 Subject: [PATCH] Add guild delete route --- src/api/guilds/guilds.rs | 56 ++++++++++++++++++++++++++++++++++++++++ src/errors.rs | 1 + 2 files changed, 57 insertions(+) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 7e1bb94..bd08075 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -2,6 +2,7 @@ use serde_json::to_string; use crate::api::schemas; use crate::api::types; +use crate::errors::InstanceServerError; impl<'a> types::Guild { /// Creates a new guild with the given parameters. @@ -67,4 +68,59 @@ impl<'a> types::Guild { } }); } + + /// Deletes a guild. + /// + /// # Arguments + /// + /// * `user` - A mutable reference to a `User` instance. + /// * `instance` - A mutable reference to an `Instance` instance. + /// * `guild_id` - A `String` representing the ID of the guild to delete. + /// + /// # Returns + /// + /// An `Option` containing an `InstanceServerError` if an error occurred during the request, otherwise `None`. + /// + /// # Example + /// + /// ```rust + /// let mut user = User::new(); + /// let mut instance = Instance::new(); + /// let guild_id = String::from("1234567890"); + /// + /// match Guild::delete(&mut user, &mut instance, guild_id) { + /// Some(e) => println!("Error deleting guild: {:?}", e), + /// None => println!("Guild deleted successfully"), + /// } + /// ``` + pub async fn delete( + user: &mut types::User<'a>, + instance: &mut crate::instance::Instance, + guild_id: String, + ) -> Option { + let url = format!( + "{}/guilds/{}/delete/", + instance.urls.get_api().to_string(), + guild_id + ); + let limits_user = user.limits.get_as_mut(); + let limits_instance = instance.limits.get_as_mut(); + let request = reqwest::Client::new() + .post(url.clone()) + .bearer_auth(user.token.clone()); + let mut requester = crate::limit::LimitedRequester::new().await; + let result = requester + .send_request( + request, + crate::api::limits::LimitType::Guild, + limits_instance, + limits_user, + ) + .await; + if result.is_err() { + Some(result.err().unwrap()) + } else { + None + } + } } diff --git a/src/errors.rs b/src/errors.rs index 599b262..d449559 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -21,6 +21,7 @@ custom_error! { MultipartCreationError{error: String} = "Got an error whilst creating the form: {}", TokenExpired = "Token expired, invalid or not found.", NoPermission = "You do not have the permissions needed to perform this action.", + NotFound{error: String} = "The provided resource hasn't been found: {}", } custom_error! {