djs-model 0.1.0-alpha.1

Discord API v10 types — users, guilds, channels, messages, interactions and gateway events.
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
//! [Users](https://docs.discord.com/developers/resources/user).

use std::collections::BTreeMap;

use compact_str::CompactString;
use serde::{Deserialize, Serialize};

use crate::{
    guild::PartialIntegration,
    id::{GuildId, SkuId, UserId},
    macros::{int_enum, int_flags, str_enum},
    util::{ImageHash, Locale},
};

/// 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](crate::guild). 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](https://docs.discord.com/developers/resources/user#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");
/// # Ok::<(), serde_json::Error>(())
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct User {
    /// The user's ID.
    pub id: UserId,
    /// The user's username. Not unique across the platform for bots; unique for migrated users.
    pub username: CompactString,
    /// 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.
    #[serde(with = "discriminator")]
    pub discriminator: u16,
    /// The user's display name, if set. For bots this is the application name.
    pub global_name: Option<CompactString>,
    /// The user's [avatar hash](https://docs.discord.com/developers/reference#image-formatting).
    pub avatar: Option<ImageHash>,
    /// Whether the user belongs to an `OAuth2` application (is a bot).
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub bot: bool,
    /// Whether the user is an official Discord system user (part of the urgent message system).
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub system: bool,
    /// The user's [banner hash](https://docs.discord.com/developers/reference#image-formatting).
    /// Only present when the user is fetched directly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub banner: Option<ImageHash>,
    /// The banner color as an RGB integer (`0xRRGGBB`), used when there is no banner image.
    /// Only present when the user is fetched directly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub accent_color: Option<u32>,
    /// The user's public [flags](UserFlags) (badges).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub public_flags: Option<UserFlags>,
    /// The user's avatar decoration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub avatar_decoration_data: Option<Box<AvatarDecorationData>>,
    /// The user's collectibles (nameplate, …).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub collectibles: Option<Box<Collectibles>>,
    /// The guild whose server tag the user displays.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub primary_guild: Option<Box<PrimaryGuild>>,
}

impl User {
    /// Returns the name to display for this user: the global display name if set, otherwise
    /// the username.
    ///
    /// discord.js counterpart: `User#displayName`.
    #[must_use]
    pub fn display_name(&self) -> &str {
        display_name(&self.username, self.global_name.as_deref())
    }

    /// 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");
    /// # Ok::<(), serde_json::Error>(())
    /// ```
    #[must_use]
    pub fn tag(&self) -> CompactString {
        tag(&self.username, self.discriminator)
    }
}

/// The user account the client is logged in as, with the fields Discord only sends for it
/// (D-052).
///
/// Received in [`Ready`](crate::gateway::payload::Ready) and from `GET /users/@me`. With an
/// `OAuth2` bearer token, `email` and `verified` require the `email` scope. Bots receive these
/// fields too (`email` is then `None`).
///
/// See the [user object](https://docs.discord.com/developers/resources/user#user-object).
///
/// discord.js counterpart: `ClientUser`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CurrentUser {
    /// The user's ID.
    pub id: UserId,
    /// The user's username.
    pub username: CompactString,
    /// The legacy four-digit Discord tag, or `0` for migrated users. See
    /// [`User::discriminator`].
    #[serde(with = "discriminator")]
    pub discriminator: u16,
    /// The user's display name, if set.
    pub global_name: Option<CompactString>,
    /// The user's [avatar hash](https://docs.discord.com/developers/reference#image-formatting).
    pub avatar: Option<ImageHash>,
    /// Whether the user is a bot.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub bot: bool,
    /// Whether the user is an official Discord system user.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub system: bool,
    /// Whether the account has two-factor authentication enabled.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub mfa_enabled: bool,
    /// The user's [banner hash](https://docs.discord.com/developers/reference#image-formatting).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub banner: Option<ImageHash>,
    /// The banner color as an RGB integer (`0xRRGGBB`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub accent_color: Option<u32>,
    /// The language the user chose in the client.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub locale: Option<Locale>,
    /// Whether the account's email address is verified.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub verified: bool,
    /// The account's email address (`OAuth2` `email` scope; always `None` for bots).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email: Option<CompactString>,
    /// All [flags](UserFlags) on the account, including non-public ones.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<UserFlags>,
    /// The account's Nitro subscription.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub premium_type: Option<PremiumType>,
    /// The public [flags](UserFlags) (badges).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub public_flags: Option<UserFlags>,
    /// The user's avatar decoration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub avatar_decoration_data: Option<Box<AvatarDecorationData>>,
    /// The user's collectibles.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub collectibles: Option<Box<Collectibles>>,
    /// The guild whose server tag the user displays.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub primary_guild: Option<Box<PrimaryGuild>>,
}

impl CurrentUser {
    /// Returns the global display name if set, otherwise the username.
    ///
    /// discord.js counterpart: `User#displayName`.
    #[must_use]
    pub fn display_name(&self) -> &str {
        display_name(&self.username, self.global_name.as_deref())
    }

    /// Returns the tag: `username#1234` for legacy users, `username` for migrated users.
    ///
    /// discord.js counterpart: `User#tag`.
    #[must_use]
    pub fn tag(&self) -> CompactString {
        tag(&self.username, self.discriminator)
    }
}

impl From<CurrentUser> for User {
    /// Keeps the fields every user has and drops the account-only ones.
    fn from(user: CurrentUser) -> Self {
        Self {
            id: user.id,
            username: user.username,
            discriminator: user.discriminator,
            global_name: user.global_name,
            avatar: user.avatar,
            bot: user.bot,
            system: user.system,
            banner: user.banner,
            accent_color: user.accent_color,
            public_flags: user.public_flags,
            avatar_decoration_data: user.avatar_decoration_data,
            collectibles: user.collectibles,
            primary_guild: user.primary_guild,
        }
    }
}

fn display_name<'a>(username: &'a str, global_name: Option<&'a str>) -> &'a str {
    global_name.unwrap_or(username)
}

fn tag(username: &str, discriminator: u16) -> CompactString {
    if discriminator == 0 {
        username.into()
    } else {
        compact_str::format_compact!("{username}#{discriminator:04}")
    }
}

/// An [avatar decoration](https://docs.discord.com/developers/resources/user#avatar-decoration-data-object):
/// the animated frame shown around a user's avatar.
///
/// discord.js counterpart: `AvatarDecorationData`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AvatarDecorationData {
    /// The [decoration's image hash](https://docs.discord.com/developers/reference#image-formatting).
    pub asset: ImageHash,
    /// The SKU the decoration belongs to.
    pub sku_id: SkuId,
}

/// The [collectibles](https://docs.discord.com/developers/resources/user#collectibles) a user
/// has equipped.
///
/// discord.js counterpart: `Collectibles`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Collectibles {
    /// The nameplate shown behind the user's name in member lists.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nameplate: Option<Nameplate>,
}

/// A [nameplate](https://docs.discord.com/developers/resources/user#nameplate): the background
/// shown behind a user's name in member lists.
///
/// discord.js counterpart: `APINameplateData` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Nameplate {
    /// The nameplate's SKU.
    pub sku_id: SkuId,
    /// The path of the [nameplate asset](https://docs.discord.com/developers/reference#image-formatting).
    pub asset: CompactString,
    /// The nameplate's label (currently unused by Discord).
    pub label: CompactString,
    /// The background color.
    pub palette: NameplatePalette,
}

str_enum! {
    /// The background color of a [`Nameplate`].
    ///
    /// discord.js counterpart: `NameplatePalette` (discord-api-types).
    pub enum NameplatePalette {
        /// Crimson.
        Crimson = "crimson",
        /// Berry.
        Berry = "berry",
        /// Sky.
        Sky = "sky",
        /// Teal.
        Teal = "teal",
        /// Forest.
        Forest = "forest",
        /// Bubble gum.
        BubbleGum = "bubble_gum",
        /// Violet.
        Violet = "violet",
        /// Cobalt (seen in the docs' example user; not in the documented list).
        Cobalt = "cobalt",
        /// Clover.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        Clover = "clover",
        /// Lemon.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        Lemon = "lemon",
        /// White.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        White = "white",
    }
}

/// The [primary guild](https://docs.discord.com/developers/resources/user#user-object-user-primary-guild)
/// of a user: the guild whose server tag is shown next to their name.
///
/// discord.js counterpart: `APIUserPrimaryGuild` (discord-api-types).
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PrimaryGuild {
    /// The guild's ID.
    pub identity_guild_id: Option<GuildId>,
    /// Whether the user displays the server tag. `None` when Discord cleared the identity (for
    /// example because the guild no longer supports tags).
    pub identity_enabled: Option<bool>,
    /// The server tag (up to 4 characters).
    pub tag: Option<CompactString>,
    /// The [server tag badge hash](https://docs.discord.com/developers/reference#image-formatting).
    pub badge: Option<ImageHash>,
}

int_enum! {
    /// The [Nitro subscription](https://docs.discord.com/developers/resources/user#user-object-premium-types)
    /// of a user.
    ///
    /// discord.js counterpart: `UserPremiumType`.
    pub enum PremiumType: u8 {
        /// No Nitro.
        None = 0,
        /// Nitro Classic.
        NitroClassic = 1,
        /// Nitro.
        Nitro = 2,
        /// Nitro Basic.
        NitroBasic = 3,
    }
}

/// Like [`discriminator`] for `Option<u16>` (partial users: absent / `null` = `None`).
pub(crate) mod option_discriminator {
    use serde::{Deserialize, Deserializer, Serializer};

    #[derive(Deserialize)]
    struct Discriminator(#[serde(with = "super::discriminator")] u16);

    #[expect(
        clippy::ref_option,
        clippy::trivially_copy_pass_by_ref,
        reason = "signature required by serde(with)"
    )]
    pub(crate) fn serialize<S: Serializer>(
        value: &Option<u16>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match value {
            Some(value) => super::discriminator::serialize(value, serializer),
            None => serializer.serialize_none(),
        }
    }

    pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<u16>, D::Error> {
        Option::<Discriminator>::deserialize(deserializer).map(|value| value.map(|value| value.0))
    }
}

/// (De)serializes a discriminator: a string of digits on the wire, `u16` in memory.
pub(crate) mod discriminator {
    use core::fmt;

    use serde::{
        Deserializer, Serializer,
        de::{Error, Unexpected, Visitor},
    };

    #[expect(
        clippy::trivially_copy_pass_by_ref,
        reason = "signature required by serde(with)"
    )]
    pub(crate) fn serialize<S: Serializer>(value: &u16, serializer: S) -> Result<S::Ok, S::Error> {
        if *value == 0 {
            serializer.serialize_str("0")
        } else {
            serializer.collect_str(&format_args!("{value:04}"))
        }
    }

    pub(crate) fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u16, D::Error> {
        struct DiscriminatorVisitor;

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

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("a discriminator of up to four digits")
            }

            fn visit_str<E: Error>(self, value: &str) -> Result<u16, E> {
                if value.is_empty() || value.len() > 4 || !value.bytes().all(|b| b.is_ascii_digit())
                {
                    return Err(E::invalid_value(Unexpected::Str(value), &self));
                }
                value
                    .parse()
                    .map_err(|_| E::invalid_value(Unexpected::Str(value), &self))
            }

            fn visit_u64<E: Error>(self, value: u64) -> Result<u16, E> {
                u16::try_from(value)
                    .ok()
                    .filter(|value| *value <= 9999)
                    .ok_or_else(|| E::invalid_value(Unexpected::Unsigned(value), &self))
            }
        }

        deserializer.deserialize_any(DiscriminatorVisitor)
    }
}

int_flags! {
    /// [Flags on a user](https://docs.discord.com/developers/resources/user#user-object-user-flags),
    /// most of them shown as profile badges.
    ///
    /// Users carry these in `public_flags` (visible to everyone) and, for the current user
    /// with the right OAuth2 scope, in `flags`.
    ///
    /// discord.js counterpart: `UserFlags` / `UserFlagsBitField`.
    ///
    /// # Examples
    ///
    /// ```
    /// use djs_model::user::UserFlags;
    ///
    /// let flags: UserFlags = serde_json::from_str("65536").unwrap();
    /// assert_eq!(flags, UserFlags::VERIFIED_BOT);
    /// ```
    pub struct UserFlags: u64 {
        /// Discord employee.
        const STAFF = 1 << 0;
        /// Owner of a partnered server.
        const PARTNER = 1 << 1;
        /// Member of HypeSquad Events.
        const HYPESQUAD = 1 << 2;
        /// Bug Hunter, level 1.
        const BUG_HUNTER_LEVEL_1 = 1 << 3;
        /// The account has SMS two-factor authentication enabled.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const MFA_SMS = 1 << 4;
        /// The user dismissed a Nitro promotion.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const PREMIUM_PROMO_DISMISSED = 1 << 5;
        /// Member of HypeSquad house Bravery.
        const HYPESQUAD_ONLINE_HOUSE_1 = 1 << 6;
        /// Member of HypeSquad house Brilliance.
        const HYPESQUAD_ONLINE_HOUSE_2 = 1 << 7;
        /// Member of HypeSquad house Balance.
        const HYPESQUAD_ONLINE_HOUSE_3 = 1 << 8;
        /// Early Nitro supporter.
        const PREMIUM_EARLY_SUPPORTER = 1 << 9;
        /// The "user" is a [team](https://docs.discord.com/developers/topics/teams) account.
        const TEAM_PSEUDO_USER = 1 << 10;
        /// The user has unread urgent messages from Discord.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const HAS_UNREAD_URGENT_MESSAGES = 1 << 13;
        /// Bug Hunter, level 2.
        const BUG_HUNTER_LEVEL_2 = 1 << 14;
        /// A verified bot.
        const VERIFIED_BOT = 1 << 16;
        /// Early verified bot developer.
        const VERIFIED_DEVELOPER = 1 << 17;
        /// Alumnus of the Discord Moderator Programs.
        const CERTIFIED_MODERATOR = 1 << 18;
        /// A bot that only uses HTTP interactions; it is shown in the online member list.
        const BOT_HTTP_INTERACTIONS = 1 << 19;
        /// The account is flagged as a spammer.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const SPAMMER = 1 << 20;
        /// Nitro features are disabled for the account.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const DISABLE_PREMIUM = 1 << 21;
        /// Holder of the Active Developer badge.
        ///
        /// Discord no longer grants this flag.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const ACTIVE_DEVELOPER = 1 << 22;
        /// The account is quarantined.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const QUARANTINED = 1 << 44;
        /// The user is a collaborator on an application team.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const COLLABORATOR = 1 << 50;
        /// The user is a restricted collaborator on an application team.
        ///
        /// **Undocumented:** known from discord-api-types, not in the official docs; may
        /// change without notice.
        const RESTRICTED_COLLABORATOR = 1 << 51;
    }
}

/// A third-party account [connected](https://docs.discord.com/developers/resources/user#connection-object)
/// to the current user (`GET /users/@me/connections`, `OAuth2` scope `connections`).
///
/// discord.js counterpart: none (discord-api-types `APIConnection`).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Connection {
    /// The account's ID on the service (not a snowflake).
    pub id: CompactString,
    /// The account's username on the service.
    pub name: CompactString,
    /// The service.
    #[serde(rename = "type")]
    pub kind: ConnectionService,
    /// Whether the connection is revoked.
    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
    pub revoked: bool,
    /// The guild integrations that use the connection (partial objects).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub integrations: Vec<PartialIntegration>,
    /// Whether the connection is verified.
    pub verified: bool,
    /// Whether friend sync is enabled.
    pub friend_sync: bool,
    /// Whether activities of the service are shown in presence updates.
    pub show_activity: bool,
    /// Whether Discord holds an `OAuth2` token for the service account.
    pub two_way_link: bool,
    /// Who can see the connection.
    pub visibility: ConnectionVisibility,
}

str_enum! {
    /// A [connection service](https://docs.discord.com/developers/resources/user#connection-object-services).
    ///
    /// discord-api-types counterpart: `ConnectionService`.
    ///
    /// discord.js counterpart: `ConnectionService` (discord-api-types).
    #[doc(alias = "Services")]
    pub enum ConnectionService {
        /// Amazon Music.
        AmazonMusic = "amazon-music",
        /// Battle.net. **Deprecated:** Battle.net connections were removed on 2026-09-22.
        ///
        /// **Undocumented:** known from discord-api-types, no longer in the official docs.
        BattleNet = "battlenet",
        /// Bluesky.
        Bluesky = "bluesky",
        /// Bungie.net.
        BungieNet = "bungie",
        /// Crunchyroll.
        Crunchyroll = "crunchyroll",
        /// A domain name.
        Domain = "domain",
        /// eBay.
        Ebay = "ebay",
        /// Epic Games.
        EpicGames = "epicgames",
        /// Facebook.
        Facebook = "facebook",
        /// GitHub.
        GitHub = "github",
        /// Instagram. Can no longer be added by users.
        Instagram = "instagram",
        /// League of Legends. **Deprecated:** no longer returned by Discord.
        ///
        /// **Undocumented:** known from discord-api-types, no longer in the official docs.
        LeagueOfLegends = "leagueoflegends",
        /// Mastodon.
        Mastodon = "mastodon",
        /// PayPal.
        PayPal = "paypal",
        /// PlayStation Network.
        PlayStationNetwork = "playstation",
        /// Reddit.
        Reddit = "reddit",
        /// Riot Games. **Deprecated:** no longer returned by Discord.
        ///
        /// **Undocumented:** known from discord-api-types, no longer in the official docs.
        RiotGames = "riotgames",
        /// Roblox.
        Roblox = "roblox",
        /// Spotify.
        Spotify = "spotify",
        /// Skype. Can no longer be added by users.
        Skype = "skype",
        /// Steam.
        Steam = "steam",
        /// TikTok.
        TikTok = "tiktok",
        /// Twitch.
        Twitch = "twitch",
        /// X (formerly Twitter).
        #[doc(alias = "Twitter")]
        X = "twitter",
        /// Xbox.
        Xbox = "xbox",
        /// YouTube.
        YouTube = "youtube",
    }
}

int_enum! {
    /// [Who can see](https://docs.discord.com/developers/resources/user#connection-object-visibility-types)
    /// a [`Connection`].
    ///
    /// discord-api-types counterpart: `ConnectionVisibility`.
    ///
    /// discord.js counterpart: `ConnectionVisibility` (discord-api-types).
    pub enum ConnectionVisibility: u8 {
        /// Only the user.
        None = 0,
        /// Everyone.
        Everyone = 1,
    }
}

/// The [role connection](https://docs.discord.com/developers/resources/user#application-role-connection-object)
/// an app stored for a user (linked roles).
///
/// discord.js counterpart: none (discord-api-types `APIApplicationRoleConnection`).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ApplicationRoleConnection {
    /// The vanity name of the connected platform (max 50 characters).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub platform_name: Option<CompactString>,
    /// The user's name on the connected platform (max 100 characters).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub platform_username: Option<CompactString>,
    /// The user's values, keyed by
    /// [`ApplicationRoleConnectionMetadata::key`](crate::application_role_connection_metadata::ApplicationRoleConnectionMetadata::key)
    /// (each value stringified, max 100 characters; D-058).
    #[serde(
        default,
        skip_serializing_if = "BTreeMap::is_empty",
        deserialize_with = "crate::serde_util::null_as_default"
    )]
    pub metadata: BTreeMap<CompactString, CompactString>,
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The example user object from the Discord API documentation.
    const DOCS_EXAMPLE: &str = r#"{
        "id": "80351110224678912",
        "username": "Nelly",
        "global_name": null,
        "discriminator": "1337",
        "avatar": "8342729096ea3675442027381ff50dfe",
        "verified": true,
        "email": "nelly@discord.com",
        "flags": 64,
        "banner": "06c16474723fe537c283b8efa61a30c8",
        "accent_color": 16711680,
        "premium_type": 0,
        "public_flags": 64,
        "avatar_decoration_data": {
            "sku_id": "1144058844004233369",
            "asset": "a_fed43ab12698df65902ba06727e20c0e"
        },
        "collectibles": {
            "nameplate": {
                "sku_id": "2247558840304243311",
                "asset": "nameplates/nameplates/twilight/",
                "label": "",
                "palette": "cobalt"
            }
        },
        "primary_guild": {
            "identity_guild_id": "1234647491267808778",
            "identity_enabled": true,
            "tag": "DISC",
            "badge": "7d1734ae5a615e82bc7a4033b98fade8"
        }
    }"#;

    #[test]
    fn deserialize_documented_example_as_user() {
        let user: User = serde_json::from_str(DOCS_EXAMPLE).unwrap();
        assert_eq!(user.id.get(), 80_351_110_224_678_912);
        assert_eq!(user.username, "Nelly");
        assert_eq!(user.discriminator, 1337);
        assert_eq!(user.global_name, None);
        assert_eq!(
            user.avatar.unwrap().to_string(),
            "8342729096ea3675442027381ff50dfe"
        );
        assert!(!user.bot);
        assert_eq!(user.accent_color, Some(0xFF_0000));
        assert_eq!(user.public_flags, Some(UserFlags::HYPESQUAD_ONLINE_HOUSE_1));
        let decoration = user.avatar_decoration_data.as_ref().unwrap();
        assert_eq!(decoration.sku_id.get(), 1_144_058_844_004_233_369);
        assert!(decoration.asset.is_animated());
        let nameplate = user
            .collectibles
            .as_ref()
            .unwrap()
            .nameplate
            .as_ref()
            .unwrap();
        assert_eq!(nameplate.palette, NameplatePalette::Cobalt);
        assert_eq!(nameplate.asset, "nameplates/nameplates/twilight/");
        let guild = user.primary_guild.as_ref().unwrap();
        assert_eq!(guild.tag.as_deref(), Some("DISC"));
        assert_eq!(guild.identity_enabled, Some(true));
        assert_eq!(user.tag(), "Nelly#1337");
        assert_eq!(user.display_name(), "Nelly");
    }

    #[test]
    fn deserialize_documented_example_as_current_user() {
        let user: CurrentUser = serde_json::from_str(DOCS_EXAMPLE).unwrap();
        assert!(user.verified);
        assert_eq!(user.email.as_deref(), Some("nelly@discord.com"));
        assert_eq!(user.flags, Some(UserFlags::HYPESQUAD_ONLINE_HOUSE_1));
        assert_eq!(user.premium_type, Some(PremiumType::None));
        assert!(!user.mfa_enabled);
        assert_eq!(user.locale, None);
        let plain = User::from(user.clone());
        assert_eq!(plain, serde_json::from_str::<User>(DOCS_EXAMPLE).unwrap());
    }

    #[test]
    fn serialization_keeps_discord_shape() {
        let json = r#"{"id":"1","username":"bot","discriminator":"0007","global_name":"Bot","avatar":null,"bot":true}"#;
        let user: User = serde_json::from_str(json).unwrap();
        // `?type` fields are written as null, absent `field?` fields are skipped (D-049).
        assert_eq!(serde_json::to_string(&user).unwrap(), json);
    }

    #[test]
    fn missing_nullable_fields_are_accepted() {
        // Some partial user payloads omit `global_name`/`avatar` entirely.
        let user: User =
            serde_json::from_str(r#"{"id":"1","username":"a","discriminator":"0"}"#).unwrap();
        assert_eq!(user.global_name, None);
        assert_eq!(user.avatar, None);
    }

    #[test]
    fn migrated_user_discriminator() {
        let json =
            r#"{"id":"1","username":"a","discriminator":"0","global_name":null,"avatar":null}"#;
        let user: User = serde_json::from_str(json).unwrap();
        assert_eq!(user.discriminator, 0);
        assert!(
            serde_json::to_string(&user)
                .unwrap()
                .contains(r#""discriminator":"0""#)
        );
    }

    #[test]
    fn rejects_bad_discriminator() {
        for bad in [r#""12345""#, r#""""#, r#""12a""#] {
            let json =
                format!(r#"{{"id":"1","username":"a","discriminator":{bad},"avatar":null}}"#);
            assert!(
                serde_json::from_str::<User>(&json).is_err(),
                "{bad} should be rejected"
            );
        }
    }

    #[test]
    fn hot_type_sizes() {
        // Guard against accidental growth of the message author type.
        assert!(
            size_of::<User>() <= 256,
            "User is {} bytes",
            size_of::<User>()
        );
    }
}