serenity/builder/
edit_profile.rs

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