feat!: add discriminator parameter to get_user_by_username

This commit is contained in:
kozabrada123 2024-08-08 08:37:08 +02:00
parent 0a1c51dddd
commit e6a4cc30a6
1 changed files with 40 additions and 14 deletions

View File

@ -13,7 +13,8 @@ use crate::{
ratelimiter::ChorusRequest, ratelimiter::ChorusRequest,
types::{ types::{
DeleteDisableUserSchema, LimitType, PublicUser, Snowflake, User, UserModifyProfileSchema, DeleteDisableUserSchema, LimitType, PublicUser, Snowflake, User, UserModifyProfileSchema,
UserModifySchema, UserProfile, UserProfileMetadata, UserSettings, VerifyUserEmailChangeResponse, VerifyUserEmailChangeSchema, UserModifySchema, UserProfile, UserProfileMetadata, UserSettings,
VerifyUserEmailChangeResponse, VerifyUserEmailChangeSchema,
}, },
}; };
@ -44,6 +45,11 @@ impl ChorusUser {
/// ///
/// As of 2024/07/28, Spacebar does not yet implement this endpoint. /// As of 2024/07/28, Spacebar does not yet implement this endpoint.
/// ///
/// If fetching with a pomelo username, discriminator should be set to None.
///
/// This route also permits fetching users with their old pre-pomelo username#discriminator
/// combo.
///
/// Note: /// Note:
/// ///
/// "Unless the target user is a bot, you must be able to add /// "Unless the target user is a bot, you must be able to add
@ -56,8 +62,12 @@ impl ChorusUser {
/// ///
/// # Reference /// # Reference
/// See <https://docs.discord.sex/resources/user#get-user-by-username> /// See <https://docs.discord.sex/resources/user#get-user-by-username>
pub async fn get_user_by_username(&mut self, username: &String) -> ChorusResult<PublicUser> { pub async fn get_user_by_username(
User::get_by_username(self, username).await &mut self,
username: &String,
discriminator: Option<&String>,
) -> ChorusResult<PublicUser> {
User::get_by_username(self, username, discriminator).await
} }
/// Gets the user's settings. /// Gets the user's settings.
@ -187,8 +197,8 @@ impl ChorusUser {
/// Initiates the email change process. /// Initiates the email change process.
/// ///
/// Sends a verification code to the current user's email. /// Sends a verification code to the current user's email.
/// ///
/// Should be followed up with [Self::verify_email_change] /// Should be followed up with [Self::verify_email_change]
/// ///
/// # Reference /// # Reference
/// See <https://docs.discord.sex/resources/user#modify-user-email> /// See <https://docs.discord.sex/resources/user#modify-user-email>
@ -206,28 +216,33 @@ impl ChorusUser {
chorus_request.handle_request_as_result(self).await chorus_request.handle_request_as_result(self).await
} }
/// Verifies a code sent to change the current user's email. /// Verifies a code sent to change the current user's email.
/// ///
/// Should be the follow-up to [Self::initiate_email_change] /// Should be the follow-up to [Self::initiate_email_change]
/// ///
/// This endpoint returns a token which can be used with [Self::modify] /// This endpoint returns a token which can be used with [Self::modify]
/// to set a new email address (email_token). /// to set a new email address (email_token).
/// ///
/// # Reference /// # Reference
/// See <https://docs.discord.sex/resources/user#modify-user-email> /// See <https://docs.discord.sex/resources/user#modify-user-email>
pub async fn verify_email_change(&mut self, schema: VerifyUserEmailChangeSchema) -> ChorusResult<VerifyUserEmailChangeResponse> { pub async fn verify_email_change(
&mut self,
schema: VerifyUserEmailChangeSchema,
) -> ChorusResult<VerifyUserEmailChangeResponse> {
let request = Client::new() let request = Client::new()
.post(format!( .post(format!(
"{}/users/@me/email/verify-code", "{}/users/@me/email/verify-code",
self.belongs_to.read().unwrap().urls.api self.belongs_to.read().unwrap().urls.api
)) ))
.header("Authorization", self.token()) .header("Authorization", self.token())
.json(&schema); .json(&schema);
let chorus_request = ChorusRequest { let chorus_request = ChorusRequest {
request, request,
limit_type: LimitType::default(), limit_type: LimitType::default(),
}; };
chorus_request.deserialize_response::<VerifyUserEmailChangeResponse>(self).await chorus_request
.deserialize_response::<VerifyUserEmailChangeResponse>(self)
.await
} }
} }
@ -272,6 +287,11 @@ impl User {
/// ///
/// As of 2024/07/28, Spacebar does not yet implement this endpoint. /// As of 2024/07/28, Spacebar does not yet implement this endpoint.
/// ///
/// If fetching with a pomelo username, discriminator should be set to None.
///
/// This route also permits fetching users with their old pre-pomelo username#discriminator
/// combo.
///
/// Note: /// Note:
/// ///
/// "Unless the target user is a bot, you must be able to add /// "Unless the target user is a bot, you must be able to add
@ -284,12 +304,18 @@ impl User {
pub async fn get_by_username( pub async fn get_by_username(
user: &mut ChorusUser, user: &mut ChorusUser,
username: &String, username: &String,
discriminator: Option<&String>,
) -> ChorusResult<PublicUser> { ) -> ChorusResult<PublicUser> {
let url_api = user.belongs_to.read().unwrap().urls.api.clone(); let url_api = user.belongs_to.read().unwrap().urls.api.clone();
let url = format!("{}/users/username/{username}", url_api); let url = format!("{}/users/username/{username}", url_api);
let request = reqwest::Client::new() let mut request = reqwest::Client::new()
.get(url) .get(url)
.header("Authorization", user.token()); .header("Authorization", user.token());
if let Some(some_discriminator) = discriminator {
request = request.query(&[("discriminator", some_discriminator)]);
}
let chorus_request = ChorusRequest { let chorus_request = ChorusRequest {
request, request,
limit_type: LimitType::Global, limit_type: LimitType::Global,