Skip to main content

User

Struct User 

Source
#[non_exhaustive]
pub struct User {
Show 13 fields pub id: UserId, pub username: CompactString, pub discriminator: u16, pub global_name: Option<CompactString>, pub avatar: Option<ImageHash>, pub bot: bool, pub system: bool, pub banner: Option<ImageHash>, pub accent_color: Option<u32>, pub public_flags: Option<UserFlags>, pub avatar_decoration_data: Option<Box<AvatarDecorationData>>, pub collectibles: Option<Box<Collectibles>>, pub primary_guild: Option<Box<PrimaryGuild>>,
}
Expand description

A Discord user — a person or a bot account.

Users are global: they are not tied to a guild. Guild-specific data (nickname, roles, …) lives on guild members. This type holds the fields Discord sends for any user; the logged-in user’s own account is a CurrentUser (D-052).

Several fields are only present in some payloads: banner and accent_color only when a user is fetched directly (GET /users/{user.id}), and message authors of webhooks carry only the basic fields.

See the user object.

discord.js counterpart: User.

§Examples

use djs_model::user::User;

let user: User = serde_json::from_str(
    r#"{"id":"80351110224678912","username":"nelly","discriminator":"0",
        "global_name":"Nelly","avatar":null,"public_flags":64}"#,
)?;
assert_eq!(user.display_name(), "Nelly");
assert_eq!(user.tag(), "nelly");

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§id: UserId

The user’s ID.

§username: CompactString

The user’s username. Not unique across the platform for bots; unique for migrated users.

§discriminator: u16

The legacy four-digit Discord tag, or 0 for users migrated to unique usernames.

Discord sends this as a string ("0", "0420"); it is stored as an integer.

§global_name: Option<CompactString>

The user’s display name, if set. For bots this is the application name.

§avatar: Option<ImageHash>

The user’s avatar hash.

§bot: bool

Whether the user belongs to an OAuth2 application (is a bot).

§system: bool

Whether the user is an official Discord system user (part of the urgent message system).

§banner: Option<ImageHash>

The user’s banner hash. Only present when the user is fetched directly.

§accent_color: Option<u32>

The banner color as an RGB integer (0xRRGGBB), used when there is no banner image. Only present when the user is fetched directly.

§public_flags: Option<UserFlags>

The user’s public flags (badges).

§avatar_decoration_data: Option<Box<AvatarDecorationData>>

The user’s avatar decoration.

§collectibles: Option<Box<Collectibles>>

The user’s collectibles (nameplate, …).

§primary_guild: Option<Box<PrimaryGuild>>

The guild whose server tag the user displays.

Implementations§

Source§

impl User

Source

pub fn display_name(&self) -> &str

Returns the name to display for this user: the global display name if set, otherwise the username.

discord.js counterpart: User#displayName.

Source

pub fn tag(&self) -> CompactString

Returns the user’s tag: username#1234 for legacy users, or just username for users migrated to unique usernames (discriminator 0).

discord.js counterpart: User#tag.

§Examples
use djs_model::user::User;

let legacy: User = serde_json::from_str(
    r#"{"id":"1","username":"nelly","discriminator":"0042","global_name":null,"avatar":null}"#,
)?;
assert_eq!(legacy.tag(), "nelly#0042");

Trait Implementations§

Source§

impl Clone for User

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for User

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for User

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for User

Source§

impl From<CurrentUser> for User

Source§

fn from(user: CurrentUser) -> Self

Keeps the fields every user has and drops the account-only ones.

Source§

impl From<User> for Mention

Source§

fn from(user: User) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for User

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for User

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for User

Auto Trait Implementations§

§

impl Freeze for User

§

impl RefUnwindSafe for User

§

impl Send for User

§

impl Sync for User

§

impl Unpin for User

§

impl UnsafeUnpin for User

§

impl UnwindSafe for User

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.