Add guild delete route

This commit is contained in:
bitfl0wer 2023-05-14 14:16:21 +02:00
parent 190def95da
commit 269d73ce87
No known key found for this signature in database
GPG Key ID: 84BBB60DF895ABF2
2 changed files with 57 additions and 0 deletions

View File

@ -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<InstanceServerError> {
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
}
}
}

View File

@ -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! {