-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathedit_profile.rs
102 lines (93 loc) · 3.2 KB
/
edit_profile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#[cfg(feature = "http")]
use super::Builder;
use super::CreateAttachment;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
use crate::model::user::CurrentUser;
/// A builder to edit the current user's settings, to be used in conjunction with
/// [`CurrentUser::edit`].
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#modify-current-user)
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct EditProfile {
#[serde(skip_serializing_if = "Option::is_none")]
username: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
avatar: Option<Option<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
banner: Option<Option<String>>,
}
impl EditProfile {
/// Equivalent to [`Self::default`].
pub fn new() -> Self {
Self::default()
}
/// Set the avatar of the current user.
///
/// # Examples
///
/// ```rust,no_run
/// # use serenity::builder::{EditProfile, CreateAttachment};
/// # use serenity::prelude::*;
/// # use serenity::model::prelude::*;
/// # use serenity::http::Http;
/// #
/// # #[cfg(feature = "http")]
/// # async fn foo_(http: &Http, current_user: &mut CurrentUser) -> Result<(), SerenityError> {
/// let avatar = CreateAttachment::path("./my_image.jpg").await.expect("Failed to read image.");
/// current_user.edit(http, EditProfile::new().avatar(&avatar)).await?;
/// # Ok(())
/// # }
/// ```
pub fn avatar(mut self, avatar: &CreateAttachment) -> Self {
self.avatar = Some(Some(avatar.to_base64()));
self
}
/// Delete the current user's avatar, resetting it to the default logo.
pub fn delete_avatar(mut self) -> Self {
self.avatar = Some(None);
self
}
/// Modifies the current user's username.
///
/// When modifying the username, if another user has the same _new_ username and current
/// discriminator, a new unique discriminator will be assigned. If there are no available
/// discriminators with the requested username, an error will occur.
pub fn username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
/// Sets the banner of the current user.
pub fn banner(mut self, banner: &CreateAttachment) -> Self {
self.banner = Some(Some(banner.to_base64()));
self
}
/// Deletes the current user's banner, resetting it to the default.
pub fn delete_banner(mut self) -> Self {
self.banner = Some(None);
self
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for EditProfile {
type Context<'ctx> = ();
type Built = CurrentUser;
/// Edit the current user's profile with the fields set.
///
/// # Errors
///
/// Returns an [`Error::Http`] if an invalid value is set. May also return an [`Error::Json`]
/// if there is an error in deserializing the API response.
async fn execute(
self,
cache_http: impl CacheHttp,
_ctx: Self::Context<'_>,
) -> Result<Self::Built> {
cache_http.http().edit_profile(&self).await
}
}