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
use serde::{
    de::{Error as DeError, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt::{Display, Formatter, Result as FmtResult};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorCode {
    /// General error (such as a malformed request body, amongst other things)
    GeneralError,
    /// Unknown account
    UnknownAccount,
    /// Unknown application
    UnknownApplication,
    /// Unknown channel
    UnknownChannel,
    /// Unknown guild
    UnknownGuild,
    /// Unknown integration
    UnknownIntegration,
    /// Unknown invite
    UnknownInvite,
    /// Unknown member
    UnknownMember,
    /// Unknown message
    UnknownMessage,
    /// Unknown permission overwrite
    UnknownPermissionOverwrite,
    /// Unknown provider
    UnknownProvider,
    /// Unknown role
    UnknownRole,
    /// Unknown token
    UnknownToken,
    /// Unknown user
    UnknownUser,
    /// Unknown emoji
    UnknownEmoji,
    /// Unknown webhook
    UnknownWebhook,
    /// Unknown webhook service
    UnknownWebhookService,
    /// Unknown session
    UnknownSession,
    /// Unknown ban
    UnknownBan,
    /// Unknown SKU
    #[allow(clippy::upper_case_acronyms)]
    UnknownSKU,
    /// Unknown Store Listing
    UnknownStoreListing,
    /// Unknown entitlement
    UnknownEntitlement,
    /// Unknown build
    UnknownBuild,
    /// Unknown lobby
    UnknownLobby,
    /// Unknown branch
    UnknownBranch,
    /// Unknown store directory layout
    UnknownStoreDirectoryLayout,
    /// Unknown redistributable
    UnknownRedistributable,
    /// Unknown gift code
    UnknownGiftCode,
    /// Unknown guild template
    UnknownGuildTemplate,
    /// Unknown discoverable server category
    UnknownDiscoverableServerCategory,
    /// Unknown sticker
    UnknownSticker,
    /// Unknown interaction
    UnknownInteraction,
    /// Unknown application command
    UnknownApplicationCommand,
    /// Unknown application command permissions
    UnknownApplicationCommandPermissions,
    /// Unknown Stage Instance
    UnknownStageInstance,
    /// Unknown Guild Member Verification Form
    UnknownGuildMemberVerificationForm,
    /// Unknown Guild Welcome Screen
    UnknownGuildWelcomeScreen,
    /// Bots cannot use this endpoint
    BotsCannotUseEndpoint,
    /// Only bots can use this endpoint
    OnlyBotsCanUseEndpoint,
    /// Explicit content cannot be sent to the desired recipient(s)
    ExplicitContentSendingBlocked,
    /// You are not authorized to perform this action on this application
    UnauthorizedApplicationAction,
    /// This action cannot be performed due to slowmode rate limit
    SlowModeRateLimitReached,
    /// Only the owner of this account can perform this action
    NotAccountOwner,
    /// Message cannot be edited due to announcement rate limits
    AnnouncementRateLimitReached,
    /// The channel you are writing has hit the write rate limit
    ChannelRateLimitReached,
    /// Your Stage topic contains words that are not allowed for public Stages
    UnallowedWordsForPublicStage,
    /// Maximum number of guilds reached (100)
    MaximumGuildsReached,
    /// Maximum number of friends reached (1000)
    MaximumFriendsReached,
    /// Maximum number of pins reached for the channel (50)
    MaximumPinsReached,
    /// Maximum number of recipients reached (10)
    MaximumRecipientsReached,
    /// Maximum number of guild roles reached (250)
    MaximumRolesReached,
    /// Maximum number of webhooks reached (10)
    MaximumWebhooksReached,
    /// Maximum number of emojis reached
    MaximumEmojisReached,
    /// Maximum number of reactions reached (20)
    MaximumReactionsReached,
    /// Maximum number of guild channels reached (500)
    MaximumGuildChannelsReached,
    /// Maximum number of attachments in a message reached (10)
    MaximumAttachmentsReached,
    /// Maximum number of invites reached (1000)
    MaximumInvitesReached,
    /// Maximum number of animated emojis reached
    MaximumAnimatedEmojisReached,
    /// Maximum number of server members reached
    MaximumGuildMembersReached,
    /// Maximum number of server categories has been reached
    MaximumServerCategoriesReached,
    /// Guild already has a template
    GuildTemplateAlreadyExist,
    /// Maximum number of bans for non-guild members have been exceeded
    MaximumNonGuildBansReached,
    /// Maximum number of bans fetches has been reached
    MaximumGuildBansFetchesReached,
    /// Maximum number of stickers reached
    MaximumStickersReached,
    /// Unauthorized. Provide a valid token and try again
    Unauthorized,
    /// You need to verify your account in order to perform this action
    AccountNeedsVerification,
    /// You are opening direct messages too fast
    OpeningDirectMessageRateLimitReached,
    /// Request entity too large. Try sending something smaller in size
    RequestEntityTooLarge,
    /// This feature has been temporarily disabled server-side
    FeatureTemporarilyDisabled,
    /// The user is banned from this guild
    UserBannedFromGuild,
    /// Target user is not connected to voice
    UserNotInVoice,
    /// This message has already been crossposted
    MessageAlreadyCrossposted,
    /// An application command with that name already exists
    CommandNameAlreadyExists,
    /// Missing access
    Missingaccess,
    /// Invalid account type
    InvalidAccountType,
    /// Cannot execute action on a DM channel
    #[allow(clippy::upper_case_acronyms)]
    InvalidDMChannelAction,
    /// Guild widget disabled
    GuildWidgetDisabled,
    /// Cannot edit a message authored by another user
    MessageNotAuthoredByUser,
    /// Cannot send an empty message
    EmptyMessage,
    /// Cannot send messages to this user
    CannotSendMessageToUser,
    /// Cannot send messages in a voice channel
    CannotSendMessagesInVoiceChannel,
    /// Channel verification level is too high for you to gain access
    VerificationLevelTooHigh,
    /// OAuth2 application does not have a bot
    OAuthApplicationHasNoBot,
    /// OAuth2 application limit reached
    OAuthApplicationLimitReached,
    /// Invalid OAuth2 state
    InvalidOAuthSstate,
    /// You lack permissions to perform that action
    PermissionsLacking,
    /// Invalid authentication token provided
    InvalidAuthenticationTokenProvided,
    /// Note was too long
    NoteTooLong,
    /// Provided too few or too many messages to delete. Must provide at least 2 and fewer than 100 messages to delete
    InvalidMessageDeleteRange,
    /// A message can only be pinned to the channel it was sent in
    MessagePinnedInWrongChannel,
    /// Invite code was either invalid or taken
    InviteCodeInvalidOrTaken,
    /// Cannot execute action on a system message
    InvalidActionOnSystemMessage,
    /// Cannot execute action on this channel type
    CannotExecuteActionOnChannelType,
    /// Invalid OAuth2 access token provided
    InvalidOAuthAccessToken,
    /// Missing required OAuth2 scope
    MissingOAuthScope,
    /// Invalid webhook token provided
    InvalidWebhookToken,
    /// Invalid role
    InvalidRole,
    /// Invalid recipient(s)
    InvalidRecipient,
    /// A message provided was too old to bulk delete
    MessageTooOldToBulkDelete,
    /// Invalid form body (returned for both application/json and multipart/form-data bodies), or invalid Content-Type provided
    InvalidFormBodyOrContentType,
    /// An invite was accepted to a guild the application's bot is not in
    InviteAcceptedToGuildBotNotIn,
    /// Invalid API version provided
    InvalidApiVersion,
    /// Cannot self-redeem this gift
    CannotSelfRedeemGift,
    /// Payment source required to redeem gift
    PaymentRequiredForGift,
    /// Cannot delete a channel required for Community guilds
    CommunityGuildRequired,
    /// Invalid sticker sent
    InvalidStickerSent,
    /// Two factor is required for this operation.
    TwoFactorRequired,
    /// No users with DiscordTag exist
    NoSuchUser,
    /// Reaction was blocked
    ReactionBlocked,
    /// API resource is currently overloaded. Try again a little later
    ApiResourceOverloaded,
    /// The Stage is already open
    StageAlreadyOpen,
    /// A status code that Twilight doesn't have registered.
    ///
    /// Please report the number if you see this variant!
    Other(u64),
}

impl ErrorCode {
    #[allow(clippy::too_many_lines)]
    pub const fn num(&self) -> u64 {
        match self {
            Self::GeneralError => 0,
            Self::UnknownAccount => 10001,
            Self::UnknownApplication => 10002,
            Self::UnknownChannel => 10003,
            Self::UnknownGuild => 10004,
            Self::UnknownIntegration => 10005,
            Self::UnknownInvite => 10006,
            Self::UnknownMember => 10007,
            Self::UnknownMessage => 10008,
            Self::UnknownPermissionOverwrite => 10009,
            Self::UnknownProvider => 10010,
            Self::UnknownRole => 10011,
            Self::UnknownToken => 10012,
            Self::UnknownUser => 10013,
            Self::UnknownEmoji => 10014,
            Self::UnknownWebhook => 10015,
            Self::UnknownWebhookService => 10016,
            Self::UnknownSession => 10020,
            Self::UnknownBan => 10026,
            Self::UnknownSKU => 10027,
            Self::UnknownStoreListing => 10028,
            Self::UnknownEntitlement => 10029,
            Self::UnknownBuild => 10030,
            Self::UnknownLobby => 10031,
            Self::UnknownBranch => 10032,
            Self::UnknownStoreDirectoryLayout => 10033,
            Self::UnknownRedistributable => 10036,
            Self::UnknownGiftCode => 10038,
            Self::UnknownGuildTemplate => 10057,
            Self::UnknownDiscoverableServerCategory => 10059,
            Self::UnknownSticker => 10060,
            Self::UnknownInteraction => 10062,
            Self::UnknownApplicationCommand => 10063,
            Self::UnknownApplicationCommandPermissions => 10066,
            Self::UnknownStageInstance => 10067,
            Self::UnknownGuildMemberVerificationForm => 10068,
            Self::UnknownGuildWelcomeScreen => 10069,
            Self::BotsCannotUseEndpoint => 20001,
            Self::OnlyBotsCanUseEndpoint => 20002,
            Self::ExplicitContentSendingBlocked => 20009,
            Self::UnauthorizedApplicationAction => 20012,
            Self::SlowModeRateLimitReached => 20016,
            Self::NotAccountOwner => 20018,
            Self::AnnouncementRateLimitReached => 20022,
            Self::ChannelRateLimitReached => 20028,
            Self::UnallowedWordsForPublicStage => 20031,
            Self::MaximumGuildsReached => 30001,
            Self::MaximumFriendsReached => 30002,
            Self::MaximumPinsReached => 30003,
            Self::MaximumRecipientsReached => 30004,
            Self::MaximumRolesReached => 30005,
            Self::MaximumWebhooksReached => 30007,
            Self::MaximumEmojisReached => 30008,
            Self::MaximumReactionsReached => 30010,
            Self::MaximumGuildChannelsReached => 30013,
            Self::MaximumAttachmentsReached => 30015,
            Self::MaximumInvitesReached => 30016,
            Self::MaximumAnimatedEmojisReached => 30018,
            Self::MaximumGuildMembersReached => 30019,
            Self::MaximumServerCategoriesReached => 30030,
            Self::GuildTemplateAlreadyExist => 30031,
            Self::MaximumNonGuildBansReached => 30035,
            Self::MaximumGuildBansFetchesReached => 30037,
            Self::MaximumStickersReached => 30039,
            Self::Unauthorized => 40001,
            Self::AccountNeedsVerification => 40002,
            Self::OpeningDirectMessageRateLimitReached => 40003,
            Self::RequestEntityTooLarge => 40005,
            Self::FeatureTemporarilyDisabled => 40006,
            Self::UserBannedFromGuild => 40007,
            Self::UserNotInVoice => 40032,
            Self::MessageAlreadyCrossposted => 40033,
            Self::CommandNameAlreadyExists => 40041,
            Self::Missingaccess => 50001,
            Self::InvalidAccountType => 50002,
            Self::InvalidDMChannelAction => 50003,
            Self::GuildWidgetDisabled => 50004,
            Self::MessageNotAuthoredByUser => 50005,
            Self::EmptyMessage => 50006,
            Self::CannotSendMessageToUser => 50007,
            Self::CannotSendMessagesInVoiceChannel => 50008,
            Self::VerificationLevelTooHigh => 50009,
            Self::OAuthApplicationHasNoBot => 50010,
            Self::OAuthApplicationLimitReached => 50011,
            Self::InvalidOAuthSstate => 50012,
            Self::PermissionsLacking => 50013,
            Self::InvalidAuthenticationTokenProvided => 50014,
            Self::NoteTooLong => 50015,
            Self::InvalidMessageDeleteRange => 50016,
            Self::MessagePinnedInWrongChannel => 50019,
            Self::InviteCodeInvalidOrTaken => 50020,
            Self::InvalidActionOnSystemMessage => 50021,
            Self::CannotExecuteActionOnChannelType => 50024,
            Self::InvalidOAuthAccessToken => 50025,
            Self::MissingOAuthScope => 50026,
            Self::InvalidWebhookToken => 50027,
            Self::InvalidRole => 50028,
            Self::InvalidRecipient => 50033,
            Self::MessageTooOldToBulkDelete => 50034,
            Self::InvalidFormBodyOrContentType => 50035,
            Self::InviteAcceptedToGuildBotNotIn => 50036,
            Self::InvalidApiVersion => 50041,
            Self::CannotSelfRedeemGift => 50054,
            Self::PaymentRequiredForGift => 50070,
            Self::CommunityGuildRequired => 50074,
            Self::InvalidStickerSent => 50081,
            Self::TwoFactorRequired => 60003,
            Self::NoSuchUser => 80004,
            Self::ReactionBlocked => 90001,
            Self::ApiResourceOverloaded => 130_000,
            Self::StageAlreadyOpen => 150_006,
            Self::Other(other) => *other,
        }
    }
}

impl From<u64> for ErrorCode {
    #[allow(clippy::too_many_lines)]
    fn from(int: u64) -> Self {
        match int {
            0 => Self::GeneralError,
            10001 => Self::UnknownAccount,
            10002 => Self::UnknownApplication,
            10003 => Self::UnknownChannel,
            10004 => Self::UnknownGuild,
            10005 => Self::UnknownIntegration,
            10006 => Self::UnknownInvite,
            10007 => Self::UnknownMember,
            10008 => Self::UnknownMessage,
            10009 => Self::UnknownPermissionOverwrite,
            10010 => Self::UnknownProvider,
            10011 => Self::UnknownRole,
            10012 => Self::UnknownToken,
            10013 => Self::UnknownUser,
            10014 => Self::UnknownEmoji,
            10015 => Self::UnknownWebhook,
            10016 => Self::UnknownWebhookService,
            10020 => Self::UnknownSession,
            10026 => Self::UnknownBan,
            10027 => Self::UnknownSKU,
            10028 => Self::UnknownStoreListing,
            10029 => Self::UnknownEntitlement,
            10030 => Self::UnknownBuild,
            10031 => Self::UnknownLobby,
            10032 => Self::UnknownBranch,
            10033 => Self::UnknownStoreDirectoryLayout,
            10036 => Self::UnknownRedistributable,
            10038 => Self::UnknownGiftCode,
            10057 => Self::UnknownGuildTemplate,
            10059 => Self::UnknownDiscoverableServerCategory,
            10060 => Self::UnknownSticker,
            10062 => Self::UnknownInteraction,
            10063 => Self::UnknownApplicationCommand,
            10066 => Self::UnknownApplicationCommandPermissions,
            10067 => Self::UnknownStageInstance,
            10068 => Self::UnknownGuildMemberVerificationForm,
            10069 => Self::UnknownGuildWelcomeScreen,
            20001 => Self::BotsCannotUseEndpoint,
            20002 => Self::OnlyBotsCanUseEndpoint,
            20022 => Self::AnnouncementRateLimitReached,
            20009 => Self::ExplicitContentSendingBlocked,
            20012 => Self::UnauthorizedApplicationAction,
            20016 => Self::SlowModeRateLimitReached,
            20018 => Self::NotAccountOwner,
            20028 => Self::ChannelRateLimitReached,
            20031 => Self::UnallowedWordsForPublicStage,
            30001 => Self::MaximumGuildsReached,
            30002 => Self::MaximumFriendsReached,
            30003 => Self::MaximumPinsReached,
            30004 => Self::MaximumRecipientsReached,
            30005 => Self::MaximumRolesReached,
            30007 => Self::MaximumWebhooksReached,
            30008 => Self::MaximumEmojisReached,
            30010 => Self::MaximumReactionsReached,
            30013 => Self::MaximumGuildChannelsReached,
            30015 => Self::MaximumAttachmentsReached,
            30016 => Self::MaximumInvitesReached,
            30018 => Self::MaximumAnimatedEmojisReached,
            30019 => Self::MaximumGuildMembersReached,
            30030 => Self::MaximumServerCategoriesReached,
            30031 => Self::GuildTemplateAlreadyExist,
            30035 => Self::MaximumNonGuildBansReached,
            30037 => Self::MaximumGuildBansFetchesReached,
            30039 => Self::MaximumStickersReached,
            40001 => Self::Unauthorized,
            40002 => Self::AccountNeedsVerification,
            40003 => Self::OpeningDirectMessageRateLimitReached,
            40005 => Self::RequestEntityTooLarge,
            40006 => Self::FeatureTemporarilyDisabled,
            40007 => Self::UserBannedFromGuild,
            40032 => Self::UserNotInVoice,
            40033 => Self::MessageAlreadyCrossposted,
            40041 => Self::CommandNameAlreadyExists,
            50001 => Self::Missingaccess,
            50002 => Self::InvalidAccountType,
            50003 => Self::InvalidDMChannelAction,
            50004 => Self::GuildWidgetDisabled,
            50005 => Self::MessageNotAuthoredByUser,
            50006 => Self::EmptyMessage,
            50007 => Self::CannotSendMessageToUser,
            50008 => Self::CannotSendMessagesInVoiceChannel,
            50009 => Self::VerificationLevelTooHigh,
            50010 => Self::OAuthApplicationHasNoBot,
            50011 => Self::OAuthApplicationLimitReached,
            50012 => Self::InvalidOAuthSstate,
            50013 => Self::PermissionsLacking,
            50014 => Self::InvalidAuthenticationTokenProvided,
            50015 => Self::NoteTooLong,
            50016 => Self::InvalidMessageDeleteRange,
            50019 => Self::MessagePinnedInWrongChannel,
            50020 => Self::InviteCodeInvalidOrTaken,
            50021 => Self::InvalidActionOnSystemMessage,
            50024 => Self::CannotExecuteActionOnChannelType,
            50025 => Self::InvalidOAuthAccessToken,
            50026 => Self::MissingOAuthScope,
            50027 => Self::InvalidWebhookToken,
            50028 => Self::InvalidRole,
            50033 => Self::InvalidRecipient,
            50034 => Self::MessageTooOldToBulkDelete,
            50035 => Self::InvalidFormBodyOrContentType,
            50036 => Self::InviteAcceptedToGuildBotNotIn,
            50041 => Self::InvalidApiVersion,
            50054 => Self::CannotSelfRedeemGift,
            50070 => Self::PaymentRequiredForGift,
            50074 => Self::CommunityGuildRequired,
            50081 => Self::InvalidStickerSent,
            60003 => Self::TwoFactorRequired,
            80004 => Self::NoSuchUser,
            90001 => Self::ReactionBlocked,
            130_000 => Self::ApiResourceOverloaded,
            150_006 => Self::StageAlreadyOpen,
            other => Self::Other(other),
        }
    }
}

impl Display for ErrorCode {
    #[allow(clippy::too_many_lines)]
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::GeneralError => f.write_str("General error (such as a malformed request body, amongst other things)"),
            Self::UnknownAccount => f.write_str("Unknown account"),
            Self::UnknownApplication => f.write_str("Unknown application"),
            Self::UnknownChannel => f.write_str("Unknown channel"),
            Self::UnknownGuild => f.write_str("Unknown guild"),
            Self::UnknownIntegration => f.write_str("Unknown integration"),
            Self::UnknownInvite => f.write_str("Unknown invite"),
            Self::UnknownMember => f.write_str("Unknown member"),
            Self::UnknownMessage => f.write_str("Unknown message"),
            Self::UnknownPermissionOverwrite => f.write_str("Unknown permission overwrite"),
            Self::UnknownProvider => f.write_str("Unknown provider"),
            Self::UnknownRole => f.write_str("Unknown role"),
            Self::UnknownToken => f.write_str("Unknown token"),
            Self::UnknownUser => f.write_str("Unknown user"),
            Self::UnknownEmoji => f.write_str("Unknown emoji"),
            Self::UnknownWebhook => f.write_str("Unknown webhook"),
            Self::UnknownWebhookService => f.write_str("Unknown webhook service"),
            Self::UnknownSession => f.write_str("Unknown session"),
            Self::UnknownBan => f.write_str("Unknown ban"),
            Self::UnknownSKU => f.write_str("Unknown SKU"),
            Self::UnknownStoreListing => f.write_str("Unknown Store Listing"),
            Self::UnknownEntitlement => f.write_str("Unknown entitlement"),
            Self::UnknownBuild => f.write_str("Unknown build"),
            Self::UnknownLobby => f.write_str("Unknown lobby"),
            Self::UnknownBranch => f.write_str("Unknown branch"),
            Self::UnknownStoreDirectoryLayout => f.write_str("Unknown store directory layout"),
            Self::UnknownRedistributable => f.write_str("Unknown redistributable"),
            Self::UnknownGiftCode => f.write_str("Unknown gift code"),
            Self::UnknownGuildTemplate => f.write_str("Unknown guild template"),
            Self::UnknownDiscoverableServerCategory => f.write_str("Unknown discoverable server category"),
            Self::UnknownSticker => f.write_str("Unknown sticker"),
            Self::UnknownInteraction => f.write_str("Unknown interaction"),
            Self::UnknownApplicationCommand => f.write_str("Unknown application command"),
            Self::UnknownApplicationCommandPermissions => f.write_str("Unknown application command permissions"),
            Self::UnknownStageInstance => f.write_str("Unknown Stage Instance"),
            Self::UnknownGuildMemberVerificationForm => f.write_str("Unknown Guild Member Verification Form"),
            Self::UnknownGuildWelcomeScreen => f.write_str("Unknown Guild Welcome Screen"),
            Self::BotsCannotUseEndpoint => f.write_str("Bots cannot use this endpoint"),
            Self::OnlyBotsCanUseEndpoint => f.write_str("Only bots can use this endpoint"),
            Self::ExplicitContentSendingBlocked => f.write_str("Explicit content cannot be sent to the desired recipient(s)"),
            Self::UnauthorizedApplicationAction => f.write_str("You are not authorized to perform this action on this application"),
            Self::SlowModeRateLimitReached => f.write_str("This action cannot be performed due to slowmode rate limit"),
            Self::NotAccountOwner => f.write_str("Only the owner of this account can perform this action"),
            Self::AnnouncementRateLimitReached => f.write_str("Message cannot be edited due to announcement rate limits"),
            Self::ChannelRateLimitReached => f.write_str("The channel you are writing has hit the write rate limit"),
            Self::UnallowedWordsForPublicStage => f.write_str("Your Stage topic contains words that are not allowed for public Stages"),
            Self::MaximumGuildsReached => f.write_str("Maximum number of guilds reached (100)"),
            Self::MaximumFriendsReached => f.write_str("Maximum number of friends reached (1000)"),
            Self::MaximumPinsReached => f.write_str("Maximum number of pins reached for the channel (50)"),
            Self::MaximumRecipientsReached => f.write_str("Maximum number of recipients reached (10)"),
            Self::MaximumRolesReached => f.write_str("Maximum number of guild roles reached (250)"),
            Self::MaximumWebhooksReached => f.write_str("Maximum number of webhooks reached (10)"),
            Self::MaximumEmojisReached => f.write_str("Maximum number of emojis reached"),
            Self::MaximumReactionsReached => f.write_str("Maximum number of reactions reached (20)"),
            Self::MaximumGuildChannelsReached => f.write_str("Maximum number of guild channels reached (500)"),
            Self::MaximumAttachmentsReached => f.write_str("Maximum number of attachments in a message reached (10)"),
            Self::MaximumInvitesReached => f.write_str("Maximum number of invites reached (1000)"),
            Self::MaximumAnimatedEmojisReached => f.write_str("Maximum animated emojis reached"),
            Self::MaximumGuildMembersReached => f.write_str("Maximum number of server members reached"),
            Self::MaximumServerCategoriesReached => f.write_str("Maximum number of server categories has been reached"),
            Self::GuildTemplateAlreadyExist => f.write_str("Guild already has a template"),
            Self::MaximumNonGuildBansReached => f.write_str("Maximum number of bans for non-guild members have been exceeded"),
            Self::MaximumGuildBansFetchesReached => f.write_str("Maximum number of bans fetches has been reached"),
            Self::MaximumStickersReached => f.write_str("Maximum number of stickers reached"),
            Self::Unauthorized => f.write_str("Unauthorized. Provide a valid token and try again"),
            Self::AccountNeedsVerification => f.write_str("You need to verify your account in order to perform this action"),
            Self::OpeningDirectMessageRateLimitReached => f.write_str("You are opening direct messages too fast"),
            Self::RequestEntityTooLarge => f.write_str("Request entity too large. Try sending something smaller in size"),
            Self::FeatureTemporarilyDisabled => f.write_str("This feature has been temporarily disabled server-side"),
            Self::UserBannedFromGuild => f.write_str("The user is banned from this guild"),
            Self::UserNotInVoice => f.write_str("Target user is not connected to voice"),
            Self::MessageAlreadyCrossposted => f.write_str("This message has already been crossposted"),
            Self::CommandNameAlreadyExists => f.write_str("An application command with that name already exists"),
            Self::Missingaccess => f.write_str("Missing access"),
            Self::InvalidAccountType => f.write_str("Invalid account type"),
            Self::InvalidDMChannelAction => f.write_str("Cannot execute action on a DM channel"),
            Self::GuildWidgetDisabled => f.write_str("Guild widget disabled"),
            Self::MessageNotAuthoredByUser => f.write_str("Cannot edit a message authored by another user"),
            Self::EmptyMessage => f.write_str("Cannot send an empty message"),
            Self::CannotSendMessageToUser => f.write_str("Cannot send messages to this user"),
            Self::CannotSendMessagesInVoiceChannel => f.write_str("Cannot send messages in a voice channel"),
            Self::VerificationLevelTooHigh => f.write_str("Channel verification level is too high for you to gain access"),
            Self::OAuthApplicationHasNoBot => f.write_str("OAuth2 application does not have a bot"),
            Self::OAuthApplicationLimitReached => f.write_str("OAuth2 application limit reached"),
            Self::InvalidOAuthSstate => f.write_str("Invalid OAuth2 state"),
            Self::PermissionsLacking => f.write_str("You lack permissions to perform that action"),
            Self::InvalidAuthenticationTokenProvided => f.write_str("Invalid authentication token provided"),
            Self::NoteTooLong => f.write_str("Note was too long"),
            Self::InvalidMessageDeleteRange => f.write_str("Provided too few or too many messages to delete. Must provide at least 2 and fewer than 100 messages to delete"),
            Self::MessagePinnedInWrongChannel => f.write_str("A message can only be pinned to the channel it was sent in"),
            Self::InviteCodeInvalidOrTaken => f.write_str("Invite code was either invalid or taken"),
            Self::InvalidActionOnSystemMessage => f.write_str("Cannot execute action on a system message"),
            Self::CannotExecuteActionOnChannelType => f.write_str("Cannot execute action on channel type"),
            Self::InvalidOAuthAccessToken => f.write_str("Invalid OAuth2 access token provided"),
            Self::MissingOAuthScope => f.write_str("Missing required OAuth2 scope"),
            Self::InvalidWebhookToken => f.write_str("Invalid webhook token provided."),
            Self::InvalidRole => f.write_str("Invalid role"),
            Self::InvalidRecipient => f.write_str("Invalid recipient(s)"),
            Self::MessageTooOldToBulkDelete => f.write_str("A message provided was too old to bulk delete"),
            Self::InvalidFormBodyOrContentType => f.write_str("Invalid form body (returned for both application/json and multipart/form-data bodies), or invalid Content-Type provided"),
            Self::InviteAcceptedToGuildBotNotIn => f.write_str("An invite was accepted to a guild the application's bot is not in"),
            Self::InvalidApiVersion => f.write_str("Invalid API version provided"),
            Self::CannotSelfRedeemGift => f.write_str("Cannot self-redeem this gift"),
            Self::PaymentRequiredForGift => f.write_str("Payment source required to redeem gift"),
            Self::CommunityGuildRequired => f.write_str("Cannot delete a channel required for Community guilds"),
            Self::InvalidStickerSent => f.write_str("Invalid sticker sent"),
            Self::TwoFactorRequired => f.write_str("Two factor is required for this operation"),
            Self::NoSuchUser => f.write_str("No users with DiscordTag exist"),
            Self::ReactionBlocked => f.write_str("Reaction was blocked"),
            Self::ApiResourceOverloaded => f.write_str("API resource is currently overloaded. Try again a little later"),
            Self::StageAlreadyOpen => f.write_str("The Stage is already open"),
            Self::Other(number) => {
                f.write_str("An error code Twilight doesn't have registered: ")?;

                Display::fmt(number, f)
            }
        }
    }
}

impl<'de> Deserialize<'de> for ErrorCode {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct ErrorCodeVisitor;

        impl<'de> Visitor<'de> for ErrorCodeVisitor {
            type Value = ErrorCode;

            fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
                f.write_str("a positive integer")
            }

            fn visit_u8<E: DeError>(self, value: u8) -> Result<Self::Value, E> {
                self.visit_u64(u64::from(value))
            }

            fn visit_u16<E: DeError>(self, value: u16) -> Result<Self::Value, E> {
                self.visit_u64(u64::from(value))
            }

            fn visit_u32<E: DeError>(self, value: u32) -> Result<Self::Value, E> {
                self.visit_u64(u64::from(value))
            }

            fn visit_u64<E: DeError>(self, int: u64) -> Result<Self::Value, E> {
                Ok(ErrorCode::from(int))
            }
        }

        deserializer.deserialize_u64(ErrorCodeVisitor)
    }
}

impl Serialize for ErrorCode {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u64(self.num())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(untagged)]
pub enum ApiError {
    General(GeneralApiError),
    /// Something was wrong with the input when sending a message.
    Message(MessageApiError),
    Ratelimited(RatelimitedApiError),
}

impl Display for ApiError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::General(inner) => Display::fmt(inner, f),
            Self::Message(inner) => Display::fmt(inner, f),
            Self::Ratelimited(inner) => Display::fmt(inner, f),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct GeneralApiError {
    pub code: ErrorCode,
    pub message: String,
}

impl Display for GeneralApiError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("Error code ")?;
        Display::fmt(&self.code.num(), f)?;
        f.write_str(": ")?;

        f.write_str(&self.message)
    }
}

/// Sending a message failed because the provided fields contained invalid
/// input.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct MessageApiError {
    /// Fields within a provided embed were invalid.
    pub embed: Option<Vec<MessageApiErrorEmbedField>>,
}

impl Display for MessageApiError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("message fields invalid: ")?;

        if let Some(embed) = &self.embed {
            f.write_str("embed (")?;

            let field_count = embed.len().saturating_sub(1);

            for (idx, field) in embed.iter().enumerate() {
                Display::fmt(field, f)?;

                if idx == field_count {
                    f.write_str(", ")?;
                }
            }

            f.write_str(")")?;
        }

        Ok(())
    }
}

/// Field within a [`MessageApiError`] [embed] list.
///
/// [embed]: MessageApiError::embed
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
pub enum MessageApiErrorEmbedField {
    /// Something was wrong with the provided fields.
    Fields,
    /// The provided timestamp wasn't a valid RFC3339 string.
    Timestamp,
}

impl Display for MessageApiErrorEmbedField {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str(match self {
            Self::Fields => "fields",
            Self::Timestamp => "timestamp",
        })
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct RatelimitedApiError {
    /// Whether the ratelimit is a global ratelimit.
    pub global: bool,
    /// Human readable message provided by the API.
    pub message: String,
    /// Amount of time to wait before retrying.
    pub retry_after: f64,
}

impl Display for RatelimitedApiError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("Got ")?;

        if self.global {
            f.write_str("global ")?;
        }

        f.write_str("ratelimited for ")?;
        Display::fmt(&self.retry_after, f)?;

        f.write_str("s")
    }
}

impl Eq for RatelimitedApiError {}

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

#[cfg(test)]
mod tests {
    use super::{
        ApiError, ErrorCode, GeneralApiError, MessageApiError, MessageApiErrorEmbedField,
        RatelimitedApiError,
    };
    use serde_test::Token;
    use static_assertions::assert_impl_all;
    use std::{convert::TryFrom, fmt::Debug};

    assert_impl_all!(ErrorCode: Clone, Copy, Debug, Eq, PartialEq, Send, Sync);

    #[derive(Clone, Copy)]
    struct AssertErrorCode<'a> {
        code: ErrorCode,
        display: &'a str,
        num: u64,
    }

    /// Test that the provided error code has the correct number and that the
    /// variant's Display implementation is correct.
    fn assert_error_code(details: AssertErrorCode<'_>) {
        let AssertErrorCode { code, display, num } = details;

        assert_eq!(num, code.num());
        assert_eq!(Ok(code), ErrorCode::try_from(num));
        assert_eq!(display, code.to_string());
    }

    #[test]
    fn test_api_error_deser() {
        let expected = GeneralApiError {
            code: ErrorCode::UnknownAccount,
            message: "Unknown account".to_owned(),
        };

        serde_test::assert_tokens(
            &expected,
            &[
                Token::Struct {
                    name: "GeneralApiError",
                    len: 2,
                },
                Token::Str("code"),
                Token::U64(10001),
                Token::Str("message"),
                Token::Str("Unknown account"),
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn test_api_error_message() {
        let expected = ApiError::Message(MessageApiError {
            embed: Some(
                [
                    MessageApiErrorEmbedField::Fields,
                    MessageApiErrorEmbedField::Timestamp,
                ]
                .to_vec(),
            ),
        });

        serde_test::assert_tokens(
            &expected,
            &[
                Token::Struct {
                    name: "MessageApiError",
                    len: 1,
                },
                Token::Str("embed"),
                Token::Some,
                Token::Seq { len: Some(2) },
                Token::UnitVariant {
                    name: "MessageApiErrorEmbedField",
                    variant: "fields",
                },
                Token::UnitVariant {
                    name: "MessageApiErrorEmbedField",
                    variant: "timestamp",
                },
                Token::SeqEnd,
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn test_api_error_ratelimited() {
        let expected = RatelimitedApiError {
            global: true,
            message: "You are being rate limited.".to_owned(),
            retry_after: 6.457,
        };

        serde_test::assert_tokens(
            &expected,
            &[
                Token::Struct {
                    name: "RatelimitedApiError",
                    len: 3,
                },
                Token::Str("global"),
                Token::Bool(true),
                Token::Str("message"),
                Token::Str("You are being rate limited."),
                Token::Str("retry_after"),
                Token::F64(6.457),
                Token::StructEnd,
            ],
        );
    }

    /// Test the values and display formatters of error codes.
    #[test]
    fn test_variants() {
        assert_error_code(AssertErrorCode {
            code: ErrorCode::UnknownDiscoverableServerCategory,
            display: "Unknown discoverable server category",
            num: 10059,
        });
        assert_error_code(AssertErrorCode {
            code: ErrorCode::UnknownSticker,
            display: "Unknown sticker",
            num: 10060,
        });
        assert_error_code(AssertErrorCode {
            code: ErrorCode::UnknownGuildMemberVerificationForm,
            display: "Unknown Guild Member Verification Form",
            num: 10068,
        });
        assert_error_code(AssertErrorCode {
            code: ErrorCode::UnknownGuildWelcomeScreen,
            display: "Unknown Guild Welcome Screen",
            num: 10069,
        });
        assert_error_code(AssertErrorCode {
            code: ErrorCode::MaximumServerCategoriesReached,
            display: "Maximum number of server categories has been reached",
            num: 30030,
        });
        assert_error_code(AssertErrorCode {
            code: ErrorCode::MaximumStickersReached,
            display: "Maximum number of stickers reached",
            num: 30039,
        });
    }
}