serenity 0.12.5

A Rust library for the Discord API.
Documentation
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
//! User information-related models.

use std::fmt;
#[cfg(feature = "model")]
use std::fmt::Write;
use std::num::NonZeroU16;
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};

use super::prelude::*;
#[cfg(feature = "model")]
use crate::builder::{Builder, CreateMessage, EditProfile};
#[cfg(all(feature = "cache", feature = "model"))]
use crate::cache::{Cache, UserRef};
#[cfg(feature = "collector")]
use crate::collector::{MessageCollector, ReactionCollector};
#[cfg(feature = "collector")]
use crate::gateway::ShardMessenger;
#[cfg(feature = "model")]
use crate::http::CacheHttp;
#[cfg(feature = "model")]
use crate::internal::prelude::*;
#[cfg(feature = "model")]
use crate::json::json;
#[cfg(feature = "model")]
use crate::model::utils::{avatar_url, user_banner_url};

/// Used with `#[serde(with|deserialize_with|serialize_with)]`
///
/// # Examples
///
/// ```rust,ignore
/// use std::num::NonZeroU16;
///
/// #[derive(Deserialize, Serialize)]
/// struct A {
///     #[serde(with = "discriminator")]
///     id: Option<NonZeroU16>,
/// }
///
/// #[derive(Deserialize)]
/// struct B {
///     #[serde(deserialize_with = "discriminator::deserialize")]
///     id: Option<NonZeroU16>,
/// }
///
/// #[derive(Serialize)]
/// struct C {
///     #[serde(serialize_with = "discriminator::serialize")]
///     id: Option<NonZeroU16>,
/// }
/// ```
pub(crate) mod discriminator {
    use std::fmt;

    use serde::de::{Error, Visitor};

    struct DiscriminatorVisitor;

    impl Visitor<'_> for DiscriminatorVisitor {
        type Value = u16;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("string or integer discriminator")
        }

        fn visit_u64<E: Error>(self, value: u64) -> Result<Self::Value, E> {
            u16::try_from(value).map_err(Error::custom)
        }

        fn visit_str<E: Error>(self, s: &str) -> Result<Self::Value, E> {
            s.parse().map_err(Error::custom)
        }
    }

    use std::num::NonZeroU16;

    use serde::{Deserializer, Serializer};

    pub fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<NonZeroU16>, D::Error> {
        deserializer.deserialize_option(OptionalDiscriminatorVisitor)
    }

    #[allow(clippy::trivially_copy_pass_by_ref, clippy::ref_option)]
    pub fn serialize<S: Serializer>(
        value: &Option<NonZeroU16>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match value {
            Some(value) => serializer.serialize_some(&format_args!("{value:04}")),
            None => serializer.serialize_none(),
        }
    }

    struct OptionalDiscriminatorVisitor;

    impl<'de> Visitor<'de> for OptionalDiscriminatorVisitor {
        type Value = Option<NonZeroU16>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("optional string or integer discriminator")
        }

        fn visit_none<E: Error>(self) -> Result<Self::Value, E> {
            Ok(None)
        }

        fn visit_unit<E: Error>(self) -> Result<Self::Value, E> {
            Ok(None)
        }

        fn visit_some<D: Deserializer<'de>>(
            self,
            deserializer: D,
        ) -> Result<Self::Value, D::Error> {
            deserializer.deserialize_any(DiscriminatorVisitor).map(NonZeroU16::new)
        }
    }
}

/// Information about the current user.
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#user-object).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(transparent)]
pub struct CurrentUser(User);

impl Deref for CurrentUser {
    type Target = User;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for CurrentUser {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<CurrentUser> for User {
    fn from(user: CurrentUser) -> Self {
        user.0
    }
}

#[cfg(feature = "model")]
impl CurrentUser {
    /// Edits the current user's profile settings.
    ///
    /// This mutates the current user in-place.
    ///
    /// Refer to [`EditProfile`]'s documentation for its methods.
    ///
    /// # Examples
    ///
    /// Change the avatar:
    ///
    /// ```rust,no_run
    /// # use serenity::builder::{EditProfile, CreateAttachment};
    /// # use serenity::http::Http;
    /// # use serenity::model::user::CurrentUser;
    /// #
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// # let http: Http = unimplemented!();
    /// # let mut user = CurrentUser::default();
    /// let avatar = CreateAttachment::path("./avatar.png").await?;
    /// user.edit(&http, EditProfile::new().avatar(&avatar)).await;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # 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.
    pub async fn edit(&mut self, cache_http: impl CacheHttp, builder: EditProfile) -> Result<()> {
        *self = builder.execute(cache_http, ()).await?;
        Ok(())
    }
}

/// The representation of a user's status.
///
/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#update-presence-status-types).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(
    Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize,
)]
#[non_exhaustive]
pub enum OnlineStatus {
    #[serde(rename = "dnd")]
    DoNotDisturb,
    #[serde(rename = "idle")]
    Idle,
    #[serde(rename = "invisible")]
    Invisible,
    #[serde(rename = "offline")]
    Offline,
    #[serde(rename = "online")]
    #[default]
    Online,
}

impl OnlineStatus {
    #[must_use]
    pub fn name(&self) -> &str {
        match *self {
            OnlineStatus::DoNotDisturb => "dnd",
            OnlineStatus::Idle => "idle",
            OnlineStatus::Invisible => "invisible",
            OnlineStatus::Offline => "offline",
            OnlineStatus::Online => "online",
        }
    }
}

/// Information about a user.
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#user-object), existence of
/// additional partial member field documented [here](https://discord.com/developers/docs/topics/gateway-events#message-create).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[non_exhaustive]
pub struct User {
    /// The unique Id of the user. Can be used to calculate the account's creation date.
    pub id: UserId,
    /// The account's username. Changing username will trigger a discriminator
    /// change if the username+discriminator pair becomes non-unique. Unless the account has
    /// migrated to a next generation username, which does not have a discriminant.
    #[serde(rename = "username")]
    pub name: String,
    /// The account's discriminator to differentiate the user from others with
    /// the same [`Self::name`]. The name+discriminator pair is always unique.
    /// If the discriminator is not present, then this is a next generation username
    /// which is implicitly unique.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "discriminator")]
    pub discriminator: Option<NonZeroU16>,
    /// The account's display name, if it is set.
    /// For bots this is the application name.
    pub global_name: Option<String>,
    /// Optional avatar hash.
    pub avatar: Option<ImageHash>,
    /// Indicator of whether the user is a bot.
    #[serde(default)]
    pub bot: bool,
    /// Whether the user is an Official Discord System user (part of the urgent message system).
    #[serde(default)]
    pub system: bool,
    /// Whether the user has two factor enabled on their account
    #[serde(default)]
    pub mfa_enabled: bool,
    /// Optional banner hash.
    ///
    /// **Note**: This will only be present if the user is fetched via Rest API, e.g. with
    /// [`crate::http::Http::get_user`].
    pub banner: Option<ImageHash>,
    /// The user's banner colour encoded as an integer representation of hexadecimal colour code
    ///
    /// **Note**: This will only be present if the user is fetched via Rest API, e.g. with
    /// [`crate::http::Http::get_user`].
    #[serde(rename = "accent_color")]
    pub accent_colour: Option<Colour>,
    /// The user's chosen language option
    pub locale: Option<String>,
    /// Whether the email on this account has been verified
    ///
    /// Requires [`Scope::Email`]
    pub verified: Option<bool>,
    /// The user's email
    ///
    /// Requires [`Scope::Email`]
    pub email: Option<String>,
    /// The flags on a user's account
    #[serde(default)]
    pub flags: UserPublicFlags,
    /// The type of Nitro subscription on a user's account
    #[serde(default)]
    pub premium_type: PremiumType,
    /// The public flags on a user's account
    pub public_flags: Option<UserPublicFlags>,
    /// Only included in [`Message::mentions`] for messages from the gateway.
    ///
    /// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#message-create-message-create-extra-fields).
    // Box required to avoid infinitely recursive types
    pub member: Option<Box<PartialMember>>,
    /// The primary guild and tag the user has active.
    ///
    /// Note: just because this guild is populated does not mean the tag is visible.
    pub primary_guild: Option<PrimaryGuild>,
    /// Information about this user's avatar decoration.
    pub avatar_decoration_data: Option<AvatarDecorationData>,
    /// The collectibles the user currently has active, excluding avatar decorations and profile
    /// effects.
    pub collectibles: Option<Collectibles>,
}

enum_number! {
    /// Premium types denote the level of premium a user has. Visit the [Nitro](https://discord.com/nitro)
    /// page to learn more about the premium plans Discord currently offers.
    ///
    /// [Discord docs](https://discord.com/developers/docs/resources/user#user-object-premium-types).
    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
    #[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
    #[serde(from = "u8", into = "u8")]
    #[non_exhaustive]
    pub enum PremiumType {
        #[default]
        None = 0,
        NitroClassic = 1,
        Nitro = 2,
        NitroBasic = 3,
        _ => Unknown(u8),
    }
}

bitflags! {
    /// User's public flags
    ///
    /// [Discord docs](https://discord.com/developers/docs/resources/user#user-object-user-flags).
    #[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
    #[derive(Copy, Clone, Default, Debug, Eq, Hash, PartialEq)]
    pub struct UserPublicFlags: u32 {
        /// User's flag as discord employee
        const DISCORD_EMPLOYEE = 1 << 0;
        /// User's flag as partnered server owner
        const PARTNERED_SERVER_OWNER = 1 << 1;
        /// User's flag as hypesquad events
        const HYPESQUAD_EVENTS = 1 << 2;
        /// User's flag as bug hunter level 1
        const BUG_HUNTER_LEVEL_1 = 1 << 3;
        /// User's flag as house bravery
        const HOUSE_BRAVERY = 1 << 6;
        /// User's flag as house brilliance
        const HOUSE_BRILLIANCE = 1 << 7;
        /// User's flag as house balance
        const HOUSE_BALANCE = 1 << 8;
        /// User's flag as early supporter
        const EARLY_SUPPORTER = 1 << 9;
        /// User's flag as team user
        const TEAM_USER = 1 << 10;
        /// User's flag as system
        const SYSTEM = 1 << 12;
        /// User's flag as bug hunter level 2
        const BUG_HUNTER_LEVEL_2 = 1 << 14;
        /// User's flag as verified bot
        const VERIFIED_BOT = 1 << 16;
        /// User's flag as early verified bot developer
        const EARLY_VERIFIED_BOT_DEVELOPER = 1 << 17;
        /// User's flag as discord certified moderator
        const DISCORD_CERTIFIED_MODERATOR = 1 << 18;
        /// Bot's running with HTTP interactions
        const BOT_HTTP_INTERACTIONS = 1 << 19;
        /// User's flag for suspected spam activity.
        #[cfg(feature = "unstable_discord_api")]
        const SPAMMER = 1 << 20;
        /// User's flag as active developer
        const ACTIVE_DEVELOPER = 1 << 22;
    }
}

/// User's Primary Guild object
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#user-object-user-primary-guild)
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[non_exhaustive]
pub struct PrimaryGuild {
    /// The id of the user's primary guild.
    pub identity_guild_id: Option<GuildId>,
    /// Whether the user is displaying the primary guild's server tag. This can be null if the
    /// system clears the identity, e.g. because the server no longer supports tags.
    pub identity_enabled: Option<bool>,
    /// The text of the [`User`]'s server tag.
    pub tag: Option<String>,
    /// The hash of the server badge.
    pub badge: Option<ImageHash>,
}

#[cfg(feature = "model")]
impl PrimaryGuild {
    #[must_use]
    /// Returns the formatted URL of the badge's icon, if one exists.
    pub fn badge_url(&self) -> Option<String> {
        primary_guild_badge_url(self.identity_guild_id, self.badge.as_ref())
    }
}

#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[non_exhaustive]
/// The data for a [`User`]'s avatar decoration.
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#avatar-decoration-data-object).
pub struct AvatarDecorationData {
    /// The avatar decoration hash
    pub asset: ImageHash,
    /// id of the avatar decoration's SKU
    pub sku_id: SkuId,
}

#[cfg(feature = "model")]
impl AvatarDecorationData {
    #[must_use]
    /// Returns the formatted URL of the decoration.
    pub fn decoration_url(&self) -> String {
        avatar_decoration_url(&self.asset)
    }
}

/// The collectibles the user has, excluding Avatar Decorations and Profile Effects.
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#collectibles).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Collectibles {
    /// The [`User`]'s nameplate, if they have one.
    pub nameplate: Option<Nameplate>,
}

/// A nameplate, shown on the member list on official clients.
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#nameplate-nameplate-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Nameplate {
    /// Id of the nameplate SKU
    pub sku_id: SkuId,
    /// Path to the nameplate asset.
    pub asset: String,
    /// The label of this nameplate.
    pub label: String,
    /// Background color of the nameplate, one of: `crimson`, `berry`, `sky`, `teal`, `forest`,
    /// `bubble_gum`, `violet`, `cobalt`, `clover`, `lemon`, `white`
    pub palette: String,
}

#[cfg(all(feature = "unstable_discord_api", feature = "model"))]
impl Nameplate {
    /// Gets the static version of the nameplate's url.
    #[must_use]
    pub fn static_url(&self) -> String {
        static_nameplate_url(&self.asset)
    }

    /// Gets the animated version of the nameplate's url.
    #[must_use]
    pub fn url(&self) -> String {
        nameplate_url(&self.asset)
    }
}

use std::hash::{Hash, Hasher};

impl PartialEq for User {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for User {}

impl Hash for User {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.id.hash(hasher);
    }
}

#[cfg(feature = "model")]
impl User {
    /// Returns the formatted URL of the user's icon, if one exists.
    ///
    /// This will produce a WEBP image URL, or GIF if the user has a GIF avatar.
    #[inline]
    #[must_use]
    pub fn avatar_url(&self) -> Option<String> {
        avatar_url(None, self.id, self.avatar.as_ref())
    }

    /// Returns the formatted URL of the user's banner, if one exists.
    ///
    /// This will produce a WEBP image URL, or GIF if the user has a GIF banner.
    #[inline]
    #[must_use]
    pub fn banner_url(&self) -> Option<String> {
        user_banner_url(None, self.id, self.banner.as_ref())
    }

    /// Creates a direct message channel between the [current user] and the user. This can also
    /// retrieve the channel if one already exists.
    ///
    /// [current user]: CurrentUser
    ///
    /// # Errors
    ///
    /// See [`UserId::create_dm_channel`] for what errors may be returned.
    #[inline]
    pub async fn create_dm_channel(&self, cache_http: impl CacheHttp) -> Result<PrivateChannel> {
        if self.bot {
            return Err(Error::Model(ModelError::MessagingBot));
        }

        self.id.create_dm_channel(cache_http).await
    }

    /// Retrieves the time that this user was created at.
    #[inline]
    #[must_use]
    pub fn created_at(&self) -> Timestamp {
        self.id.created_at()
    }

    /// Returns the formatted URL to the user's default avatar URL.
    ///
    /// This will produce a PNG URL.
    #[inline]
    #[must_use]
    pub fn default_avatar_url(&self) -> String {
        default_avatar_url(self)
    }

    /// Sends a message to a user through a direct message channel. This is a channel that can only
    /// be accessed by you and the recipient.
    ///
    /// # Examples
    ///
    /// See [`UserId::direct_message`] for examples.
    ///
    /// # Errors
    ///
    /// See [`UserId::direct_message`] for errors.
    pub async fn direct_message(
        &self,
        cache_http: impl CacheHttp,
        builder: CreateMessage,
    ) -> Result<Message> {
        self.id.direct_message(cache_http, builder).await
    }

    /// Calculates the user's display name.
    ///
    /// The global name takes priority over the user's username if it exists.
    ///
    /// Note: Guild specific information is not included as this is only available on the [Member].
    #[inline]
    #[must_use]
    pub fn display_name(&self) -> &str {
        self.global_name.as_deref().unwrap_or(&self.name)
    }

    /// This is an alias of [`Self::direct_message`].
    #[allow(clippy::missing_errors_doc)]
    #[inline]
    pub async fn dm(&self, cache_http: impl CacheHttp, builder: CreateMessage) -> Result<Message> {
        self.direct_message(cache_http, builder).await
    }

    /// Retrieves the URL to the user's avatar, falling back to the default avatar if needed.
    ///
    /// This will call [`Self::avatar_url`] first, and if that returns [`None`], it then falls back
    /// to [`Self::default_avatar_url`].
    #[must_use]
    pub fn face(&self) -> String {
        self.avatar_url().unwrap_or_else(|| self.default_avatar_url())
    }

    /// Retrieves the URL to the static version of the user's avatar, falling back to the default
    /// avatar if needed.
    ///
    /// This will call [`Self::static_avatar_url`] first, and if that returns [`None`], it then
    /// falls back to [`Self::default_avatar_url`].
    #[must_use]
    pub fn static_face(&self) -> String {
        self.static_avatar_url().unwrap_or_else(|| self.default_avatar_url())
    }

    /// Check if a user has a [`Role`]. This will retrieve the [`Guild`] from the [`Cache`] if it
    /// is available, and then check if that guild has the given [`Role`].
    ///
    /// # Examples
    ///
    /// Check if a guild has a [`Role`] by Id:
    ///
    /// ```rust,ignore
    /// // Assumes a 'guild_id' and `role_id` have already been bound
    /// let _ = message.author.has_role(guild_id, role_id);
    /// ```
    ///
    /// [`Cache`]: crate::cache::Cache
    ///
    /// # Errors
    ///
    /// Returns an [`Error::Http`] if the given [`Guild`] is unavailable, if that [`Role`] does not
    /// exist in the given [`Guild`], or if the given [`User`] is not in that [`Guild`].
    ///
    /// May also return an [`Error::Json`] if there is an error in deserializing the API response.
    #[inline]
    pub async fn has_role(
        &self,
        cache_http: impl CacheHttp,
        guild_id: impl Into<GuildId>,
        role: impl Into<RoleId>,
    ) -> Result<bool> {
        guild_id.into().member(cache_http, self).await.map(|m| m.roles.contains(&role.into()))
    }

    /// Refreshes the information about the user.
    ///
    /// Replaces the instance with the data retrieved over the REST API.
    ///
    /// # Errors
    ///
    /// See [`UserId::to_user`] for what errors may be returned.
    #[inline]
    pub async fn refresh(&mut self, cache_http: impl CacheHttp) -> Result<()> {
        *self = self.id.to_user(cache_http).await?;

        Ok(())
    }

    /// Returns a static formatted URL of the user's icon, if one exists.
    ///
    /// This will always produce a WEBP image URL.
    #[inline]
    #[must_use]
    pub fn static_avatar_url(&self) -> Option<String> {
        static_avatar_url(self.id, self.avatar.as_ref())
    }

    /// Returns the "tag" for the user.
    ///
    /// The "tag" is defined as "username#discriminator", such as "zeyla#5479".
    ///
    /// # Examples
    ///
    /// Make a command to tell the user what their tag is:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # use serenity::model::prelude::*;
    /// # struct Handler;
    ///
    /// #[serenity::async_trait]
    /// # #[cfg(feature = "client")]
    /// impl EventHandler for Handler {
    ///     async fn message(&self, context: Context, msg: Message) {
    ///         if msg.content == "!mytag" {
    ///             let content = format!("Your tag is: {}", msg.author.tag());
    ///             let _ = msg.channel_id.say(&context.http, &content).await;
    ///         }
    ///     }
    /// }
    /// ```
    #[inline]
    #[must_use]
    pub fn tag(&self) -> String {
        tag(&self.name, self.discriminator)
    }

    /// Returns the user's nickname in the given `guild_id`.
    ///
    /// If none is used, it returns [`None`].
    #[inline]
    pub async fn nick_in(
        &self,
        cache_http: impl CacheHttp,
        guild_id: impl Into<GuildId>,
    ) -> Option<String> {
        let guild_id = guild_id.into();

        // This can't be removed because `GuildId::member` clones the entire `Member` struct if
        // it's present in the cache, which is expensive.
        #[cfg(feature = "cache")]
        {
            if let Some(cache) = cache_http.cache() {
                if let Some(guild) = guild_id.to_guild_cached(cache) {
                    if let Some(member) = guild.members.get(&self.id) {
                        return member.nick.clone();
                    }
                }
            }
        }

        // At this point we're guaranteed to do an API call.
        guild_id.member(cache_http, &self.id).await.ok().and_then(|member| member.nick)
    }

    /// Returns a builder which can be awaited to obtain a message or stream of messages sent by
    /// this user.
    #[cfg(feature = "collector")]
    pub fn await_reply(&self, shard_messenger: impl AsRef<ShardMessenger>) -> MessageCollector {
        MessageCollector::new(shard_messenger).author_id(self.id)
    }

    /// Same as [`Self::await_reply`].
    #[cfg(feature = "collector")]
    pub fn await_replies(&self, shard_messenger: impl AsRef<ShardMessenger>) -> MessageCollector {
        self.await_reply(shard_messenger)
    }

    /// Returns a builder which can be awaited to obtain a reaction or stream of reactions sent by
    /// this user.
    #[cfg(feature = "collector")]
    pub fn await_reaction(&self, shard_messenger: impl AsRef<ShardMessenger>) -> ReactionCollector {
        ReactionCollector::new(shard_messenger).author_id(self.id)
    }

    /// Same as [`Self::await_reaction`].
    #[cfg(feature = "collector")]
    pub fn await_reactions(
        &self,
        shard_messenger: impl AsRef<ShardMessenger>,
    ) -> ReactionCollector {
        self.await_reaction(shard_messenger)
    }
}

impl fmt::Display for User {
    /// Formats a string which will mention the user.
    // This is in the format of: `<@USER_ID>`
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.id.mention(), f)
    }
}

#[cfg(feature = "model")]
impl UserId {
    /// Creates a direct message channel between the [current user] and the user. This can also
    /// retrieve the channel if one already exists.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Http`] if a [`User`] with that [`UserId`] does not exist, or is otherwise
    /// unavailable.
    ///
    /// May also return an [`Error::Json`] if there is an error in deserializing the channel data
    /// returned by the Discord API.
    ///
    /// [current user]: CurrentUser
    pub async fn create_dm_channel(self, cache_http: impl CacheHttp) -> Result<PrivateChannel> {
        #[cfg(feature = "temp_cache")]
        if let Some(cache) = cache_http.cache() {
            if let Some(private_channel) = cache.temp_private_channels.get(&self) {
                return Ok(PrivateChannel::clone(&private_channel));
            }
        }

        let map = json!({
            "recipient_id": self,
        });

        let channel = cache_http.http().create_private_channel(&map).await?;

        #[cfg(feature = "temp_cache")]
        if let Some(cache) = cache_http.cache() {
            use crate::cache::MaybeOwnedArc;

            let cached_channel = MaybeOwnedArc::new(channel.clone());
            cache.temp_private_channels.insert(self, cached_channel);
        }

        Ok(channel)
    }

    /// Sends a message to a user through a direct message channel. This is a channel that can only
    /// be accessed by you and the recipient.
    ///
    /// # Examples
    ///
    /// When a user sends a message with a content of `"~help"`, DM the author a help message
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # use serenity::model::prelude::*;
    /// # struct Handler;
    /// use serenity::builder::CreateMessage;
    ///
    /// #[serenity::async_trait]
    /// # #[cfg(feature = "client")]
    /// impl EventHandler for Handler {
    ///     async fn message(&self, ctx: Context, msg: Message) {
    ///         if msg.content == "~help" {
    ///             let builder = CreateMessage::new().content("Helpful info here.");
    ///
    ///             if let Err(why) = msg.author.id.direct_message(&ctx, builder).await {
    ///                 println!("Err sending help: {why:?}");
    ///                 let _ = msg.reply(&ctx, "There was an error DMing you help.").await;
    ///             };
    ///         }
    ///     }
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a [`ModelError::MessagingBot`] if the user being direct messaged is a bot user.
    ///
    /// May also return an [`Error::Http`] if the user cannot be sent a direct message.
    ///
    /// Returns an [`Error::Json`] if there is an error deserializing the API response.
    pub async fn direct_message(
        self,
        cache_http: impl CacheHttp,
        builder: CreateMessage,
    ) -> Result<Message> {
        self.create_dm_channel(&cache_http).await?.send_message(cache_http, builder).await
    }

    /// This is an alias of [`Self::direct_message`].
    #[allow(clippy::missing_errors_doc)]
    #[inline]
    pub async fn dm(self, cache_http: impl CacheHttp, builder: CreateMessage) -> Result<Message> {
        self.direct_message(cache_http, builder).await
    }

    /// Attempts to find a [`User`] by its Id in the cache.
    #[cfg(feature = "cache")]
    #[inline]
    pub fn to_user_cached(self, cache: &impl AsRef<Cache>) -> Option<UserRef<'_>> {
        cache.as_ref().user(self)
    }

    /// First attempts to find a [`User`] by its Id in the cache, upon failure requests it via the
    /// REST API.
    ///
    /// **Note**: If the cache is not enabled, REST API will be used only.
    ///
    /// **Note**: If the cache is enabled, you might want to enable the `temp_cache` feature to
    /// cache user data retrieved by this function for a short duration.
    ///
    /// # Errors
    ///
    /// May return an [`Error::Http`] if a [`User`] with that [`UserId`] does not exist, or
    /// otherwise cannot be fetched.
    ///
    /// May also return an [`Error::Json`] if there is an error in deserializing the user.
    #[inline]
    pub async fn to_user(self, cache_http: impl CacheHttp) -> Result<User> {
        #[cfg(feature = "cache")]
        {
            if let Some(cache) = cache_http.cache() {
                if let Some(user) = cache.user(self) {
                    return Ok(user.clone());
                }
            }
        }

        let user = cache_http.http().get_user(self).await?;

        #[cfg(all(feature = "cache", feature = "temp_cache"))]
        {
            if let Some(cache) = cache_http.cache() {
                use crate::cache::MaybeOwnedArc;

                let cached_user = MaybeOwnedArc::new(user.clone());
                cache.temp_users.insert(cached_user.id, cached_user);
            }
        }

        Ok(user)
    }
}

impl From<Member> for UserId {
    /// Gets the Id of a [`Member`].
    fn from(member: Member) -> UserId {
        member.user.id
    }
}

impl From<&Member> for UserId {
    /// Gets the Id of a [`Member`].
    fn from(member: &Member) -> UserId {
        member.user.id
    }
}

impl From<User> for UserId {
    /// Gets the Id of a [`User`].
    fn from(user: User) -> UserId {
        user.id
    }
}

impl From<&User> for UserId {
    /// Gets the Id of a [`User`].
    fn from(user: &User) -> UserId {
        user.id
    }
}

#[cfg(feature = "model")]
fn default_avatar_url(user: &User) -> String {
    let avatar_id = if let Some(discriminator) = user.discriminator {
        discriminator.get() % 5 // Legacy username system
    } else {
        ((user.id.get() >> 22) % 6) as u16 // New username system
    };

    cdn!("/embed/avatars/{}.png", avatar_id)
}

#[cfg(feature = "model")]
fn static_avatar_url(user_id: UserId, hash: Option<&ImageHash>) -> Option<String> {
    hash.map(|hash| cdn!("/avatars/{}/{}.webp?size=1024", user_id, hash))
}

#[cfg(feature = "model")]
fn tag(name: &str, discriminator: Option<NonZeroU16>) -> String {
    // 32: max length of username
    // 1: `#`
    // 4: max length of discriminator
    let mut tag = String::with_capacity(37);
    tag.push_str(name);
    if let Some(discriminator) = discriminator {
        tag.push('#');
        write!(tag, "{discriminator:04}").expect("writing to a string should never fail");
    }
    tag
}

#[cfg(feature = "model")]
fn primary_guild_badge_url(guild_id: Option<GuildId>, hash: Option<&ImageHash>) -> Option<String> {
    if let Some(guild_id) = guild_id {
        return hash.map(|hash| cdn!("/guild-tag-badges/{}/{}.png?size=1024", guild_id, hash));
    }

    None
}

#[cfg(feature = "model")]
fn avatar_decoration_url(hash: &ImageHash) -> String {
    cdn!("/avatar-decoration-presets/{}.png?size=1024", hash)
}

#[cfg(all(feature = "unstable_discord_api", feature = "model"))]
fn nameplate_url(path: &str) -> String {
    cdn!("https://cdn.discordapp.com/assets/collectibles/{}/asset.webm", path)
}

#[cfg(all(feature = "unstable_discord_api", feature = "model"))]
#[cfg(feature = "model")]
fn static_nameplate_url(path: &str) -> String {
    cdn!("https://cdn.discordapp.com/assets/collectibles/{}/static.png", path)
}

#[cfg(test)]
mod test {
    use std::num::NonZeroU16;

    #[test]
    fn test_discriminator_serde() {
        use serde::{Deserialize, Serialize};

        use super::discriminator;
        use crate::json::{assert_json, json};

        #[derive(Debug, PartialEq, Deserialize, Serialize)]
        struct User {
            #[serde(default, skip_serializing_if = "Option::is_none", with = "discriminator")]
            discriminator: Option<NonZeroU16>,
        }

        let user = User {
            discriminator: NonZeroU16::new(123),
        };
        assert_json(&user, json!({"discriminator": "0123"}));

        let user_no_discriminator = User {
            discriminator: None,
        };
        assert_json(&user_no_discriminator, json!({}));
    }

    #[cfg(feature = "model")]
    mod model {
        use std::num::NonZeroU16;
        use std::str::FromStr;

        use crate::model::id::UserId;
        use crate::model::misc::ImageHash;
        use crate::model::user::User;

        #[test]
        fn test_core() {
            let mut user = User {
                id: UserId::new(210),
                avatar: Some(ImageHash::from_str("fb211703bcc04ee612c88d494df0272f").unwrap()),
                discriminator: NonZeroU16::new(1432),
                name: "test".to_string(),
                ..Default::default()
            };

            let expected = "/avatars/210/fb211703bcc04ee612c88d494df0272f.webp?size=1024";
            assert!(user.avatar_url().unwrap().ends_with(expected));
            assert!(user.static_avatar_url().unwrap().ends_with(expected));

            user.avatar = Some(ImageHash::from_str("a_fb211703bcc04ee612c88d494df0272f").unwrap());
            let expected = "/avatars/210/a_fb211703bcc04ee612c88d494df0272f.gif?size=1024";
            assert!(user.avatar_url().unwrap().ends_with(expected));
            let expected = "/avatars/210/a_fb211703bcc04ee612c88d494df0272f.webp?size=1024";
            assert!(user.static_avatar_url().unwrap().ends_with(expected));

            user.avatar = None;
            assert!(user.avatar_url().is_none());

            assert_eq!(user.tag(), "test#1432");
        }

        #[test]
        fn default_avatars() {
            let mut user = User {
                discriminator: None,
                id: UserId::new(737323631117598811),
                ..Default::default()
            };

            // New username system
            assert!(user.default_avatar_url().ends_with("5.png"));

            // Legacy username system
            user.discriminator = NonZeroU16::new(1);
            assert!(user.default_avatar_url().ends_with("1.png"));
            user.discriminator = NonZeroU16::new(2);
            assert!(user.default_avatar_url().ends_with("2.png"));
            user.discriminator = NonZeroU16::new(3);
            assert!(user.default_avatar_url().ends_with("3.png"));
            user.discriminator = NonZeroU16::new(4);
            assert!(user.default_avatar_url().ends_with("4.png"));
        }
    }
}