composure 0.1.0

Discord bots over HTTP Interactions, no gateway: slash commands, components, modals and JSX-like views, on Cloudflare Workers (WASM) or any server
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
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
use serde::{Deserialize, Deserializer, de, de::DeserializeOwned};
use serde_json::value::RawValue;
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::types::{
    ApplicationIntegrationType, Attachment, Channel, Entitlement, InteractionContextType, Lazy,
    Member, Message, PartialChannel, PartialMember, Permissions, Role, Snowflake, SnowflakeMap,
    User,
};

/// A Command or autocomplete Interaction: chat input, user, or message Command data
pub type ApplicationCommandInteraction = DataInteraction<ApplicationCommandInteractionData>;
/// A button click or select Interaction
pub type MessageComponentInteraction = DataInteraction<MessageComponentData>;
/// A modal submit Interaction
pub type ModalSubmitInteraction = DataInteraction<ModalSubmitData>;

/// [Interaction Structure](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-structure)
#[derive(Debug, Clone)]
pub enum Interaction {
    /// Discord checking the endpoint; answered with a Pong
    Ping(PingInteraction),
    /// A Command was invoked
    ApplicationCommand(ApplicationCommandInteraction),
    /// A button was clicked or a select used
    MessageComponent(MessageComponentInteraction),
    /// The user is typing in an Option with autocomplete
    ApplicationCommandAutocomplete(ApplicationCommandInteraction),
    /// A modal was submitted
    ModalSubmit(ModalSubmitInteraction),
}

impl<'de> Deserialize<'de> for Interaction {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let envelope = Envelope::deserialize(deserializer)?;
        let common = InteractionCommon {
            id: envelope.id,
            application_id: envelope.application_id,
            guild_id: envelope.guild_id,
            channel: envelope.channel,
            channel_id: envelope.channel_id,
            member: envelope.member,
            user: envelope.user,
            token: envelope.token,
            version: envelope.version,
            message: envelope.message,
            app_permissions: envelope.app_permissions,
            guild_locale: envelope.guild_locale,
            entitlements: envelope.entitlements,
            authorizing_integration_owners: envelope.authorizing_integration_owners,
            context: envelope.context,
        };
        let (locale, data) = (envelope.locale, envelope.data);

        Ok(match envelope.t {
            1 => Interaction::Ping(PingInteraction { common }),
            2 => Interaction::ApplicationCommand(DataInteraction::new(common, locale, data)?),
            3 => Interaction::MessageComponent(DataInteraction::new(common, locale, data)?),
            4 => Interaction::ApplicationCommandAutocomplete(DataInteraction::new(
                common, locale, data,
            )?),
            5 => Interaction::ModalSubmit(DataInteraction::new(common, locale, data)?),
            _ => return Err(de::Error::custom("unknown interaction type")),
        })
    }
}

/// An Interaction read in one pass: the common fields typed, and `data` kept as raw JSON until
/// `type` says what it is (Discord does not send `type` first)
#[derive(Deserialize)]
struct Envelope {
    #[serde(rename = "type")]
    t: u8,
    locale: Option<String>,
    data: Option<Box<RawValue>>,

    id: Snowflake,
    application_id: Snowflake,
    guild_id: Option<Snowflake>,
    channel: Option<Lazy<Channel>>,
    channel_id: Option<Snowflake>,
    member: Option<Member>,
    user: Option<User>,
    token: String,
    version: u8,
    message: Option<Lazy<Message>>,
    app_permissions: Option<Permissions>,
    guild_locale: Option<String>,
    #[serde(default)]
    entitlements: Vec<Entitlement>,
    #[serde(default)]
    authorizing_integration_owners: IntegrationOwners,
    context: Option<InteractionContextType>,
}

/// The fields every Interaction has (besides `type` and `data`)
#[derive(Debug, Clone, Deserialize)]
pub struct InteractionCommon {
    /// ID of the interaction
    pub id: Snowflake,

    /// ID of the application this interaction is for
    pub application_id: Snowflake,

    /// Guild that the interaction was sent from
    pub guild_id: Option<Snowflake>,

    /// Channel that the interaction was sent from, parsed on first read
    pub channel: Option<Lazy<Channel>>,

    /// Channel that the interaction was sent from
    pub channel_id: Option<Snowflake>,

    /// Guild member data for the invoking user, including permissions
    pub member: Option<Member>,

    /// User object for the invoking user, if invoked in a DM. Use [`InteractionCommon::invoking_user`] to get the user in any context.
    pub user: Option<User>,

    /// Continuation token for responding to the interaction
    pub token: String,

    /// Read-only property, always 1
    pub version: u8,

    /// For components, the message they are attached to, parsed on first read; see
    /// [`InteractionCommon::message`]
    pub message: Option<Lazy<Message>>,

    /// Bitwise set of permissions the app or bot has within the channel the interaction was sent from
    pub app_permissions: Option<Permissions>,

    /// [Guild's preferred locale](https://discord.com/developers/docs/resources/guild#guild-object), if invoked in a guild
    pub guild_locale: Option<String>,

    /// Installation contexts the interaction is authorized for, mapped to the guild id (guild install;
    /// `"0"` in a DM with the bot) or the authorizing user's id (user install)
    #[serde(default)]
    pub authorizing_integration_owners: IntegrationOwners,

    /// Context the interaction was triggered from
    pub context: Option<InteractionContextType>,

    /// [Entitlements](https://discord.com/developers/docs/resources/entitlement) of the invoking
    /// user and guild for the app's premium SKUs
    #[serde(default)]
    pub entitlements: Vec<Entitlement>,
}

/// Installation contexts an Interaction is authorized for: Discord's
/// `authorizing_integration_owners`, keyed by [`ApplicationIntegrationType`]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
pub struct IntegrationOwners {
    /// Guild install: the guild's id, or `0` in a DM with the bot
    #[serde(rename = "0")]
    pub guild: Option<Snowflake>,

    /// User install: the authorizing user's id
    #[serde(rename = "1")]
    pub user: Option<Snowflake>,
}

impl IntegrationOwners {
    /// The id of the guild or user that installed the app for `integration_type`
    pub fn get(&self, integration_type: ApplicationIntegrationType) -> Option<Snowflake> {
        match integration_type {
            ApplicationIntegrationType::GuildInstall => self.guild,
            ApplicationIntegrationType::UserInstall => self.user,
        }
    }

    /// How many installation contexts authorized the Interaction
    pub fn len(&self) -> usize {
        usize::from(self.guild.is_some()) + usize::from(self.user.is_some())
    }

    /// Whether no installation context is listed
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl InteractionCommon {
    /// The user who invoked the interaction: `member.user` in a guild, `user` in DMs and private channels.
    /// `None` only for pings.
    pub fn invoking_user(&self) -> Option<&User> {
        self.member
            .as_ref()
            .map(|member| &member.user)
            .or(self.user.as_ref())
    }

    /// For components, the message they are attached to; parsed on the first call
    pub fn message(&self) -> Option<&Message> {
        self.message.as_ref()?.get()
    }

    /// The channel the interaction was sent from; parsed on the first call
    pub fn channel(&self) -> Option<&Channel> {
        self.channel.as_ref()?.get()
    }
}

/// Discord checking the Interactions Endpoint URL
#[derive(Debug, Clone)]
pub struct PingInteraction {
    /// Ids, token, version
    pub common: InteractionCommon,
}

/// An Interaction carrying data `D`: [`ApplicationCommandInteraction`], [`MessageComponentInteraction`]
/// or [`ModalSubmitInteraction`]
#[derive(Debug, Clone)]
pub struct DataInteraction<D> {
    /// Ids, token, user, member, guild, channel, ...
    pub common: InteractionCommon,
    /// The invoking user's language
    pub locale: Option<String>,
    /// What was invoked: Command options, component values, or modal fields
    pub data: D,
}

impl<D: DeserializeOwned> DataInteraction<D> {
    fn new<E: de::Error>(
        common: InteractionCommon,
        locale: Option<String>,
        data: Option<Box<RawValue>>,
    ) -> Result<Self, E> {
        let data = data.ok_or_else(|| E::missing_field("data"))?;
        Ok(Self {
            common,
            locale,
            // `from_slice` like the envelope, so serde_json's parser is compiled once
            data: serde_json::from_slice(data.get().as_bytes()).map_err(E::custom)?,
        })
    }
}

/// [Interaction Data](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-data)
#[derive(Debug, Clone, Deserialize)]
pub struct ApplicationCommandInteractionData {
    /// the [ID](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure) of the invoked command
    pub id: Snowflake,

    /// the [name](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure) of the invoked command
    pub name: String,

    /// the [type](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure) of the invoked command
    #[serde(rename = "type")]
    pub t: ApplicationCommandType,

    /// converted users + roles + channels + attachments
    pub resolved: Option<ResolvedData>,

    /// the params + values from the user
    pub options: Option<OptionList>,

    /// the id of the guild the command is registered to
    pub guild_id: Option<Snowflake>,

    /// id of the [user](https://discord.com/developers/docs/interactions/application-commands#user-commands) or [message](https://discord.com/developers/docs/interactions/application-commands#message-commands) targeted by a user or message command
    pub target_id: Option<Snowflake>,
}

impl ApplicationCommandInteractionData {
    /// The resolved user with this id
    pub fn resolved_user(&self, snowflake: &Snowflake) -> Option<&User> {
        self.resolved
            .as_ref()
            .and_then(|r| r.users.as_ref())
            .and_then(|u| u.get(snowflake))
    }

    /// The resolved guild member with this id (its user is in [`resolved_user`](Self::resolved_user))
    pub fn resolved_member(&self, snowflake: &Snowflake) -> Option<&PartialMember> {
        self.resolved
            .as_ref()
            .and_then(|r| r.members.as_ref())
            .and_then(|u| u.get(snowflake))
    }

    /// The resolved role with this id
    pub fn resolved_role(&self, snowflake: &Snowflake) -> Option<&Role> {
        self.resolved
            .as_ref()
            .and_then(|r| r.roles.as_ref())
            .and_then(|u| u.get(snowflake))
    }

    /// The resolved channel with this id
    pub fn resolved_channel(&self, snowflake: &Snowflake) -> Option<&PartialChannel> {
        self.resolved
            .as_ref()
            .and_then(|r| r.channels.as_ref())
            .and_then(|c| c.get(snowflake))
    }

    /// The resolved message with this id (the target of message Commands)
    pub fn resolved_message(&self, snowflake: &Snowflake) -> Option<&Message> {
        self.resolved
            .as_ref()
            .and_then(|r| r.messages.as_ref())
            .and_then(|m| m.get(snowflake))
    }

    /// The resolved attachment with this id
    pub fn resolved_attachment(&self, snowflake: &Snowflake) -> Option<&Attachment> {
        self.resolved
            .as_ref()
            .and_then(|r| r.attachments.as_ref())
            .and_then(|a| a.get(snowflake))
    }

    /// The only top-level option, if there is exactly one
    pub fn first_option(&self) -> Option<&ApplicationCommandInteractionDataOption> {
        self.options.as_ref().and_then(|o| o.single())
    }

    /// The option the user is typing in, for autocomplete interactions
    pub fn focused_option(&self) -> Option<&FocusedOption> {
        self.options.as_ref().and_then(|o| o.focused())
    }
}

/// [Message Component Data Structure](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-message-component-data-structure)
#[derive(Debug, Clone, Deserialize)]
pub struct MessageComponentData {
    /// the [custom_id](https://discord.com/developers/docs/interactions/message-components#custom-id) of the component
    pub custom_id: String,

    /// the [type](https://discord.com/developers/docs/interactions/message-components#component-object-component-types) of the component
    pub component_type: MessageComponentType,

    /// values the user selected in a [select menu](https://discord.com/developers/docs/interactions/message-components#select-menu-object) component:
    /// option values for string selects, ids for user/role/mentionable/channel selects
    pub values: Option<Vec<String>>,

    /// resolved entities for user, role, mentionable, and channel selects
    pub resolved: Option<ResolvedData>,
}

/// Types of the classic message components; [`Component`](crate::types::Component) covers every type
#[derive(Debug, Clone, Deserialize_repr)]
#[repr(u8)]
pub enum MessageComponentType {
    /// A row of components
    ActionRow = 1,
    /// A button
    Button = 2,
    /// A select of text options
    StringSelect = 3,
    /// Free text, in modals
    TextInput = 4,
    /// A user select
    UserSelect = 5,
    /// A role select
    RoleSelect = 6,
    /// A user-or-role select
    MentionableSelect = 7,
    /// A channel select
    ChannelSelect = 8,
}

/// [Modal Submit Data Structure](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-modal-submit-data-structure)
#[derive(Debug, Clone, Deserialize)]
pub struct ModalSubmitData {
    /// the [custom_id](https://discord.com/developers/docs/interactions/message-components#custom-id) of the modal
    pub custom_id: String,

    /// the values submitted by the user
    pub components: Vec<ModalSubmitComponent>,

    /// users, roles, channels, and attachments picked in selects and file uploads
    pub resolved: Option<ResolvedData>,
}

impl ModalSubmitData {
    /// The submitted text of the text input or radio group with `custom_id`, wherever it is
    /// nested
    pub fn value(&self, custom_id: &str) -> Option<&str> {
        self.find(custom_id).and_then(ModalSubmitComponent::text)
    }

    /// Whether the checkbox with `custom_id` was checked
    pub fn checked(&self, custom_id: &str) -> Option<bool> {
        self.find(custom_id).and_then(ModalSubmitComponent::checked)
    }

    /// The resolved user with this id
    pub fn resolved_user(&self, id: &Snowflake) -> Option<&User> {
        self.resolved.as_ref()?.users.as_ref()?.get(id)
    }

    /// The resolved guild member with this id
    pub fn resolved_member(&self, id: &Snowflake) -> Option<&PartialMember> {
        self.resolved.as_ref()?.members.as_ref()?.get(id)
    }

    /// The resolved role with this id
    pub fn resolved_role(&self, id: &Snowflake) -> Option<&Role> {
        self.resolved.as_ref()?.roles.as_ref()?.get(id)
    }

    /// The resolved channel with this id
    pub fn resolved_channel(&self, id: &Snowflake) -> Option<&PartialChannel> {
        self.resolved.as_ref()?.channels.as_ref()?.get(id)
    }

    /// The resolved attachment with this id
    pub fn resolved_attachment(&self, id: &Snowflake) -> Option<&Attachment> {
        self.resolved.as_ref()?.attachments.as_ref()?.get(id)
    }

    /// The submitted values of the select with `custom_id`, wherever it is nested
    pub fn values(&self, custom_id: &str) -> Option<&[String]> {
        self.find(custom_id).and_then(|c| c.values.as_deref())
    }

    /// The component with `custom_id`, wherever it is nested
    pub fn find(&self, custom_id: &str) -> Option<&ModalSubmitComponent> {
        self.components.iter().find_map(|c| c.find(custom_id))
    }
}

/// A component in a [modal submission](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-modal-submit-data-structure):
/// an action row or label wrapping inputs, or an input carrying the submitted value
#[derive(Debug, Clone, Deserialize)]
pub struct ModalSubmitComponent {
    /// [component type](https://discord.com/developers/docs/components/reference#component-object-component-types)
    #[serde(rename = "type")]
    pub t: u8,

    /// Developer-defined id of an input
    pub custom_id: Option<String>,

    /// Submitted text of a text input or radio group, or a checkbox's state
    pub value: Option<SubmittedValue>,

    /// Submitted values of a select
    pub values: Option<Vec<String>>,

    /// Children of an action row
    pub components: Option<Vec<ModalSubmitComponent>>,

    /// Child of a label
    pub component: Option<Box<ModalSubmitComponent>>,
}

/// A submitted `value`: text (text inputs, radio groups) or a checkbox's state
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(untagged)]
pub enum SubmittedValue {
    /// Text, from a text input or radio group
    Text(String),
    /// Whether a checkbox was checked
    Checked(bool),
}

impl ModalSubmitComponent {
    /// Submitted text of a text input or radio group
    pub fn text(&self) -> Option<&str> {
        match &self.value {
            Some(SubmittedValue::Text(text)) => Some(text),
            _ => None,
        }
    }

    /// A checkbox's state
    pub fn checked(&self) -> Option<bool> {
        match self.value {
            Some(SubmittedValue::Checked(checked)) => Some(checked),
            _ => None,
        }
    }

    fn find(&self, custom_id: &str) -> Option<&ModalSubmitComponent> {
        if self.custom_id.as_deref() == Some(custom_id) {
            return Some(self);
        }
        self.components
            .iter()
            .flatten()
            .chain(self.component.as_deref())
            .find_map(|c| c.find(custom_id))
    }
}

/// [Resolved Data Structure](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-resolved-data-structure)
#[derive(Debug, Clone, Deserialize)]
pub struct ResolvedData {
    /// the ids and User objects
    pub users: Option<SnowflakeMap<User>>,

    /// the ids and partial Member objects
    pub members: Option<SnowflakeMap<PartialMember>>,

    /// the ids and Role objects
    pub roles: Option<SnowflakeMap<Role>>,

    /// the ids and partial Channel objects
    pub channels: Option<SnowflakeMap<PartialChannel>>,

    /// the ids and partial Message objects
    pub messages: Option<SnowflakeMap<Message>>,

    /// the ids and attachment objects
    pub attachments: Option<SnowflakeMap<Attachment>>,
}

/// A string Option's value
pub type StringOption = ValueOption<String>;
/// An integer Option's value
pub type IntegerOption = ValueOption<i64>;
/// A boolean Option's value
pub type BooleanOption = ValueOption<bool>;
/// A user, channel, role, mentionable or attachment Option's value: the id, resolved in the
/// Interaction's resolved data
pub type SnowflakeOption = ValueOption<Snowflake>;
/// A number Option's value
pub type NumberOption = ValueOption<f64>;

/// [Application Command Interaction Data Option Structure](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-application-command-interaction-data-option-structure)
#[derive(Debug, Clone)]
pub enum ApplicationCommandInteractionDataOption {
    /// A subcommand, with its own options
    Subcommand(Subcommand),
    /// A subcommand group, with the chosen subcommand
    SubcommandGroup(SubcommandGroup),
    /// A string Option
    String(StringOption),
    /// An integer Option
    Integer(IntegerOption),
    /// A boolean Option
    Boolean(BooleanOption),
    /// A user Option
    User(SnowflakeOption),
    /// A channel Option
    Channel(SnowflakeOption),
    /// A role Option
    Role(SnowflakeOption),
    /// A mentionable Option
    Mentionable(SnowflakeOption),
    /// A number Option
    Number(NumberOption),
    /// Value is the attachment id; see [`ApplicationCommandInteractionData::resolved_attachment`]
    Attachment(SnowflakeOption),
    /// The option being typed in an autocomplete interaction, whatever its declared type
    Focused(FocusedOption),
}

/// The option a user is typing in an autocomplete interaction. Discord sends the partial input,
/// which for integer and number options may not parse yet, so it is kept as text.
#[derive(Debug, Clone)]
pub struct FocusedOption {
    /// The Option's name
    pub name: String,

    /// [Option type](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-type)
    pub t: u8,

    /// What the user has typed so far
    pub value: String,
}

impl ApplicationCommandInteractionDataOption {
    /// The option's name, whatever its type
    pub fn name(&self) -> &str {
        match self {
            Self::Subcommand(s) => &s.name,
            Self::SubcommandGroup(s) => &s.name,
            Self::String(s) => &s.name,
            Self::Integer(s) => &s.name,
            Self::Boolean(s) => &s.name,
            Self::User(s) | Self::Channel(s) | Self::Role(s) | Self::Mentionable(s) => &s.name,
            Self::Number(s) => &s.name,
            Self::Attachment(s) => &s.name,
            Self::Focused(s) => &s.name,
        }
    }
}

impl<'de> Deserialize<'de> for ApplicationCommandInteractionDataOption {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use ApplicationCommandInteractionDataOption as O;
        use RawOptionValue as V;

        let RawOption {
            name,
            t,
            value,
            options,
            focused,
        } = RawOption::deserialize(deserializer)?;

        if focused == Some(true) && t > 2 {
            let value = match value {
                Some(V::String(s)) => s,
                Some(V::Integer(i)) => i.to_string(),
                Some(V::Number(n)) => n.to_string(),
                Some(V::Boolean(b)) => b.to_string(),
                None => String::new(),
            };
            return Ok(O::Focused(FocusedOption { name, t, value }));
        }

        Ok(match (t, value) {
            (1, _) => O::Subcommand(Subcommand {
                name,
                options: OptionList(options.unwrap_or_default()),
                focused,
            }),
            (2, _) => match options.and_then(|o| o.into_iter().next()) {
                Some(O::Subcommand(subcommand)) => O::SubcommandGroup(SubcommandGroup {
                    name,
                    subcommand,
                    focused,
                }),
                _ => return Err(de::Error::custom("subcommand group without a subcommand")),
            },
            (3, Some(V::String(s))) => O::String(ValueOption::new(name, s, focused)),
            (4, Some(V::Integer(i))) => O::Integer(ValueOption::new(name, i, focused)),
            (5, Some(V::Boolean(b))) => O::Boolean(ValueOption::new(name, b, focused)),
            (10, Some(V::Number(n))) => O::Number(ValueOption::new(name, n, focused)),
            // Discord sends whole numbers without a fraction
            (10, Some(V::Integer(i))) => O::Number(ValueOption::new(name, i as f64, focused)),
            (6..=9 | 11, Some(V::String(s))) => {
                let Ok(id) = s.parse() else {
                    return Err(de::Error::custom(format_args!(
                        "option `{name}`: invalid id"
                    )));
                };
                let option = ValueOption::new(name, id, focused);
                match t {
                    6 => O::User(option),
                    7 => O::Channel(option),
                    8 => O::Role(option),
                    9 => O::Mentionable(option),
                    _ => O::Attachment(option),
                }
            }
            (3..=11, _) => {
                return Err(de::Error::custom(format_args!(
                    "option `{name}`: value does not match type {t}"
                )));
            }
            _ => return Err(de::Error::custom("unknown option type")),
        })
    }
}

/// An option read in one pass; turned into the variant its `type` names
#[derive(Deserialize)]
struct RawOption {
    name: String,
    #[serde(rename = "type")]
    t: u8,
    value: Option<RawOptionValue>,
    options: Option<Vec<ApplicationCommandInteractionDataOption>>,
    focused: Option<bool>,
}

/// An option `value` as JSON sent it
enum RawOptionValue {
    String(String),
    Integer(i64),
    Number(f64),
    Boolean(bool),
}

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

        impl de::Visitor<'_> for Visitor {
            type Value = RawOptionValue;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                f.write_str("a string, number, or boolean")
            }

            fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
                Ok(RawOptionValue::String(v.to_string()))
            }

            fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
                Ok(RawOptionValue::String(v))
            }

            fn visit_i64<E: de::Error>(self, v: i64) -> Result<Self::Value, E> {
                Ok(RawOptionValue::Integer(v))
            }

            fn visit_u64<E: de::Error>(self, v: u64) -> Result<Self::Value, E> {
                i64::try_from(v)
                    .map(RawOptionValue::Integer)
                    .map_err(|_| E::invalid_value(de::Unexpected::Unsigned(v), &self))
            }

            fn visit_f64<E: de::Error>(self, v: f64) -> Result<Self::Value, E> {
                Ok(RawOptionValue::Number(v))
            }

            fn visit_bool<E: de::Error>(self, v: bool) -> Result<Self::Value, E> {
                Ok(RawOptionValue::Boolean(v))
            }
        }

        deserializer.deserialize_any(Visitor)
    }
}

/// The options of a Command or subcommand, in the order Discord sent them
#[derive(Debug, Clone, Default)]
pub struct OptionList(Vec<ApplicationCommandInteractionDataOption>);

impl OptionList {
    /// No options
    pub const fn empty() -> Self {
        OptionList(Vec::new())
    }

    /// Every option
    pub fn iter(&self) -> impl Iterator<Item = &ApplicationCommandInteractionDataOption> {
        self.0.iter()
    }

    /// The option being typed in an autocomplete interaction, searching into subcommands
    pub fn focused(&self) -> Option<&FocusedOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Focused(f) => Some(f),
            ApplicationCommandInteractionDataOption::Subcommand(s) => s.options.focused(),
            ApplicationCommandInteractionDataOption::SubcommandGroup(g) => {
                g.subcommand.options.focused()
            }
            _ => None,
        })
    }

    /// The number Option named `name`
    pub fn get_number_option(&self, name: &str) -> Option<&NumberOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Number(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The attachment Option named `name`
    pub fn get_attachment_option(&self, name: &str) -> Option<&SnowflakeOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Attachment(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The first option
    pub fn single(&self) -> Option<&ApplicationCommandInteractionDataOption> {
        self.0.first()
    }

    /// The subcommand invoked, if this level holds one
    pub fn subcommand(&self) -> Option<&Subcommand> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Subcommand(s) => Some(s),
            _ => None,
        })
    }

    /// The subcommand group invoked, if this level holds one
    pub fn subcommand_group(&self) -> Option<&SubcommandGroup> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::SubcommandGroup(s) => Some(s),
            _ => None,
        })
    }

    /// The option named `name`, of any type
    pub fn get_option(&self, name: &str) -> Option<&ApplicationCommandInteractionDataOption> {
        self.0.iter().find(|o| o.name() == name)
    }

    /// The string Option named `name`
    pub fn get_string_option(&self, name: &str) -> Option<&StringOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::String(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The integer Option named `name`
    pub fn get_integer_option(&self, name: &str) -> Option<&IntegerOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Integer(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The boolean Option named `name`
    pub fn get_boolean_option(&self, name: &str) -> Option<&BooleanOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Boolean(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The user Option named `name`
    pub fn get_user_option(&self, name: &str) -> Option<&SnowflakeOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::User(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The channel Option named `name`
    pub fn get_channel_option(&self, name: &str) -> Option<&SnowflakeOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Channel(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The role Option named `name`
    pub fn get_role_option(&self, name: &str) -> Option<&SnowflakeOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Role(s) if s.name == name => Some(s),
            _ => None,
        })
    }

    /// The mentionable Option named `name`
    pub fn get_mentionable_option(&self, name: &str) -> Option<&SnowflakeOption> {
        self.0.iter().find_map(|o| match o {
            ApplicationCommandInteractionDataOption::Mentionable(s) if s.name == name => Some(s),
            _ => None,
        })
    }
}

impl<'de> Deserialize<'de> for OptionList {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(OptionList(
            Vec::<ApplicationCommandInteractionDataOption>::deserialize(deserializer)?,
        ))
    }
}

/// A subcommand being invoked
#[derive(Debug, Clone, Deserialize)]
pub struct Subcommand {
    /// Name of the parameter
    pub name: String,

    /// Present if this option is a group or subcommand
    #[serde(default)]
    pub options: OptionList,

    /// true if this option is the currently focused option for autocomplete
    pub focused: Option<bool>,
}

/// A subcommand group being invoked; Discord sends exactly one subcommand in it
#[derive(Debug, Clone)]
pub struct SubcommandGroup {
    /// Name of the parameter
    pub name: String,

    /// The command being called
    pub subcommand: Subcommand,

    /// true if this option is the currently focused option for autocomplete
    pub focused: Option<bool>,
}

impl<'de> Deserialize<'de> for SubcommandGroup {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct InnerData {
            name: String,
            options: Vec<ApplicationCommandInteractionDataOption>,
            focused: Option<bool>,
        }

        let InnerData {
            name,
            focused,
            options,
        } = InnerData::deserialize(deserializer)?;

        match options.into_iter().next() {
            Some(ApplicationCommandInteractionDataOption::Subcommand(subcommand)) => {
                Ok(SubcommandGroup {
                    name,
                    subcommand,
                    focused,
                })
            }
            _ => Err(serde::de::Error::custom("Not a subcommand")),
        }
    }
}

/// An Option's name and value
#[derive(Debug, Clone, Deserialize)]
pub struct ValueOption<T> {
    /// Name of the parameter
    pub name: String,

    /// Value of the option resulting from user input
    pub value: T,

    /// true if this option is the currently focused option for autocomplete
    pub focused: Option<bool>,
}

impl<T> ValueOption<T> {
    fn new(name: String, value: T, focused: Option<bool>) -> Self {
        Self {
            name,
            value,
            focused,
        }
    }
}

/// [Application Command Types](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum ApplicationCommandType {
    /// Slash commands; a text-based command that shows up when a user types /
    ChatInput = 1,

    /// A UI-based command that shows up when you right click or tap on a user
    User = 2,

    /// A UI-based command that shows up when you right click or tap on a message
    Message = 3,
}

/// [Application Command Data](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-application-command-data-structure)
#[derive(Debug, Clone, Deserialize)]
pub struct InteractionData {
    /// the [ID](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure) of the invoked command
    pub id: Snowflake,

    /// the [name](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure) of the invoked command
    pub name: String,

    /// the [type](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-structure) of the invoked command
    #[serde(rename = "type")]
    pub t: ApplicationCommandType,

    /// converted users + roles + channels + attachments
    pub resolved: Option<ResolvedData>,

    /// the params + values from the user
    pub options: Option<Vec<ApplicationCommandInteractionDataOption>>,

    /// the id of the guild the command is registered to
    pub guild_id: Option<Snowflake>,

    /// id of the [user](https://discord.com/developers/docs/interactions/application-commands#user-commands) or [message](https://discord.com/developers/docs/interactions/application-commands#message-commands) targeted by a user or message command
    pub target_id: Option<Snowflake>,
}

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

    fn button_click(message: serde_json::Value) -> MessageComponentInteraction {
        let json = serde_json::json!({
            "id": "1", "application_id": "2", "type": 3, "token": "t", "version": 1,
            "channel": { "id": "3", "type": 0, "name": "general" },
            "data": { "custom_id": "go", "component_type": 2 },
            "message": message,
        });
        match serde_json::from_value(json).unwrap() {
            Interaction::MessageComponent(click) => click,
            _ => unreachable!(),
        }
    }

    #[test]
    pub fn message_and_channel_parse_on_first_read() {
        let click = button_click(serde_json::json!({
            "id": "4", "channel_id": "3", "content": "hello", "timestamp": "2026-01-01T00:00:00Z",
            "edited_timestamp": null, "tts": false, "mention_everyone": false, "mentions": [],
            "mention_roles": [], "attachments": [], "embeds": [], "pinned": false, "type": 0,
            "author": { "id": "5", "username": "a", "discriminator": "0", "avatar": null },
            "components": [{ "type": 1, "components": [
                { "type": 2, "style": 1, "label": "Go", "custom_id": "go" },
            ] }],
        }));
        let message = click.common.message().unwrap();
        assert_eq!(message.content, "hello");
        assert!(matches!(
            message.components.as_deref(),
            Some([crate::types::Component::ActionRow(_)])
        ));
        assert!(std::ptr::eq(message, click.common.message().unwrap()));
        assert_eq!(
            click.common.channel().unwrap().name.as_deref(),
            Some("general")
        );
    }

    #[test]
    pub fn malformed_message_does_not_fail_the_interaction() {
        let click = button_click(serde_json::json!({ "id": "not a message" }));
        assert_eq!(click.data.custom_id, "go");
        assert!(click.common.message().is_none());
        assert!(click.common.message.as_ref().unwrap().parse().is_err());
    }

    #[test]
    pub fn ping_interaction() {
        let json = r#"{
            "application_id": "1052322265397739523",
			"type": 1,
			"token": "A_UNIQUE_TOKEN",
			"member": {
				"user": {
					"id": "53908232506183680",
					"username": "Mason",
					"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432",
					"discriminator": "1337",
					"public_flags": 131141
				},
				"roles": ["539082325061836999"],
				"premium_since": null,
				"permissions": "2147483647",
				"pending": false,
				"nick": null,
				"mute": false,
				"joined_at": "2017-03-13T19:19:14.040000+00:00",
				"is_pending": false,
				"deaf": false,
                "flags": 0
			},
			"id": "786008729715212338",
			"guild_id": "290926798626357999",
			"app_permissions": "442368",
			"guild_locale": "en-US",
			"locale": "en-US",
			"channel_id": "645027906669510667",
            "version": 1
		}"#;

        let res = serde_json::from_str::<Interaction>(json);

        assert!(res.is_ok());

        let interaction = res.unwrap();

        assert!(matches!(
            interaction,
            Interaction::Ping(PingInteraction { .. })
        ))
    }

    #[test]
    pub fn command_interaction() {
        let json = r#"{
            "application_id": "1052322265397739523",
            "version": 1,
			"type": 2,
			"token": "A_UNIQUE_TOKEN",
			"member": {
				"user": {
					"id": "53908232506183680",
					"username": "Mason",
					"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432",
					"discriminator": "1337",
					"public_flags": 131141
				},
				"roles": ["539082325061836999"],
				"premium_since": null,
				"permissions": "2147483647",
				"pending": false,
				"nick": null,
				"mute": false,
				"joined_at": "2017-03-13T19:19:14.040000+00:00",
				"is_pending": false,
				"deaf": false,
                "flags": 0
			},
			"id": "786008729715212338",
			"guild_id": "290926798626357999",
			"app_permissions": "442368",
			"guild_locale": "en-US",
			"locale": "en-US",
			"data": {
				"options": [{
					"type": 3,
					"name": "cardname",
					"value": "The Gitrog Monster"
				}],
				"type": 1,
				"name": "cardsearch",
				"id": "771825006014889984"
			},
			"channel_id": "645027906669510667"
		}"#;

        let res = serde_json::from_str::<Interaction>(json);

        assert!(res.is_ok());

        let interaction = res.unwrap();

        assert!(matches!(
            interaction,
            Interaction::ApplicationCommand(DataInteraction { .. })
        ))
    }

    #[test]
    pub fn real_interaction() {
        let json = r#"{
            "app_permissions": "137411140374081",
            "application_id": "1052322265397739523",
            "channel": {
                "flags": 0,
                "guild_id": "798662131062931547",
                "id": "941169456686723122",
                "last_message_id": "1100155827400229026",
                "name": "bot-stuff",
                "nsfw": false,
                "parent_id": "798662131678969866",
                "permissions": "140737488355327",
                "position": 1,
                "rate_limit_per_user": 0,
                "topic": null,
                "type": 0
            },
            "channel_id": "941169456686723122",
            "data": {
                "guild_id": "798662131062931547",
                "id": "1052358444704862218",
                "name": "ping",
                "type": 1
            },
            "entitlement_sku_ids": [],
            "entitlements": [],
            "guild_id": "798662131062931547",
            "guild_locale": "en-US",
            "id": "1100173248714518568",
            "locale": "en-US",
            "member": {
                "avatar": null,
                "communication_disabled_until": null,
                "deaf": false,
                "flags": 0,
                "is_pending": false,
                "joined_at": "2021-01-12T21:18:10.481000+00:00",
                "mute": false,
                "nick": null,
                "pending": false,
                "permissions": "140737488355327",
                "premium_since": null,
                "roles": [
                    "943607715639484456"
                ],
                "user": {
                    "avatar": "fa82e15e24ee16c9fcbf8dd34d10b4cc",
                    "avatar_decoration": null,
                    "discriminator": "9846",
                    "display_name": null,
                    "global_name": null,
                    "id": "282265607313817601",
                    "public_flags": 0,
                    "username": "BlueFrog"
                }
            },
            "token": "aW50ZXJhY3Rpb246MTEwMDE3MzI0ODcxNDUxODU2ODppVTFuSkNSbndrZ01Na3RCWk81MVhTWkdSbk8yTlBaM1U3Z3JlckR4YUZJMTZFTm9wc21nZnlaSnN4ZUZCTTd0Q0Jzc09ac3BHV1E1MGlBZGZnZzh0NDJmTElIcTB1M0FZQTJPS1BxcG1GTEtZUjNDWWFEamhEeTRPMWZnS0R4dQ",
            "type": 2,
            "version": 1
        }"#;

        let interaction = serde_json::from_str::<Interaction>(json);

        println!("{:#?}", interaction);

        assert!(interaction.is_ok());
    }

    #[test]
    pub fn current_format_interaction() {
        let json = r#"{
            "app_permissions": "2248473465835073",
            "application_id": "1052322265397739523",
            "attachment_size_limit": 10485760,
            "authorizing_integration_owners": { "0": "798662131062931547" },
            "channel": {
                "flags": 0,
                "guild_id": "798662131062931547",
                "id": "941169456686723122",
                "last_message_id": "1100155827400229026",
                "name": "bot-stuff",
                "nsfw": false,
                "parent_id": "798662131678969866",
                "permissions": "2251799813685247",
                "position": 1,
                "rate_limit_per_user": 0,
                "topic": null,
                "type": 0
            },
            "channel_id": "941169456686723122",
            "context": 0,
            "data": {
                "guild_id": "798662131062931547",
                "id": "1052358444704862218",
                "name": "ping",
                "type": 1
            },
            "entitlement_sku_ids": [],
            "entitlements": [],
            "guild": { "features": [], "id": "798662131062931547", "locale": "en-US" },
            "guild_id": "798662131062931547",
            "guild_locale": "en-US",
            "id": "1100173248714518568",
            "locale": "en-US",
            "member": {
                "avatar": null,
                "banner": null,
                "collectibles": null,
                "communication_disabled_until": null,
                "deaf": false,
                "flags": 0,
                "joined_at": "2021-01-12T21:18:10.481000+00:00",
                "mute": false,
                "nick": null,
                "pending": false,
                "permissions": "2251799813685247",
                "premium_since": null,
                "roles": ["943607715639484456"],
                "unusual_dm_activity_until": null,
                "user": {
                    "avatar": "fa82e15e24ee16c9fcbf8dd34d10b4cc",
                    "avatar_decoration_data": null,
                    "clan": null,
                    "collectibles": null,
                    "discriminator": "0",
                    "global_name": "Blue Frog",
                    "id": "282265607313817601",
                    "primary_guild": null,
                    "public_flags": 0,
                    "username": "bluefrog"
                }
            },
            "token": "A_UNIQUE_TOKEN",
            "type": 2,
            "version": 1
        }"#;

        let Interaction::ApplicationCommand(interaction) =
            serde_json::from_str::<Interaction>(json).unwrap()
        else {
            panic!("expected application command");
        };

        let member = interaction.common.member.unwrap();
        assert_eq!(member.user.discriminator, "0");
        assert_eq!(member.user.global_name.as_deref(), Some("Blue Frog"));
        assert_eq!(member.display_name(), "Blue Frog");
    }

    fn user_install_interaction(context: u8, location: &str) -> ApplicationCommandInteraction {
        let json = format!(
            r#"{{
                "app_permissions": "562949953601536",
                "application_id": "1052322265397739523",
                "attachment_size_limit": 10485760,
                "authorizing_integration_owners": {{ "1": "282265607313817601" }},
                "context": {context},
                "data": {{ "id": "1052358444704862218", "name": "ping", "type": 1 }},
                "entitlements": [],
                "id": "1100173248714518568",
                "locale": "en-US",
                {location},
                "token": "A_UNIQUE_TOKEN",
                "type": 2,
                "version": 1
            }}"#
        );

        match serde_json::from_str::<Interaction>(&json).unwrap() {
            Interaction::ApplicationCommand(interaction) => interaction,
            other => panic!("expected application command, got {other:?}"),
        }
    }

    const USER: &str = r#"{
        "avatar": null,
        "discriminator": "0",
        "global_name": "Blue Frog",
        "id": "282265607313817601",
        "public_flags": 0,
        "username": "bluefrog"
    }"#;

    #[test]
    pub fn user_install_guild_context() {
        let location = format!(
            r#""guild_id": "798662131062931547",
            "guild_locale": "en-US",
            "channel_id": "941169456686723122",
            "member": {{
                "avatar": null,
                "deaf": false,
                "flags": 0,
                "joined_at": "2021-01-12T21:18:10.481000+00:00",
                "mute": false,
                "nick": "Froggy",
                "pending": false,
                "permissions": "2251799813685247",
                "premium_since": null,
                "roles": [],
                "user": {USER}
            }}"#
        );
        let interaction = user_install_interaction(0, &location);
        let common = &interaction.common;

        assert_eq!(common.context, Some(InteractionContextType::Guild));
        assert_eq!(
            common
                .authorizing_integration_owners
                .get(ApplicationIntegrationType::UserInstall)
                .unwrap(),
            Snowflake::from_u64(282265607313817601)
        );
        assert!(common.user.is_none());
        assert_eq!(
            common.invoking_user().unwrap().id,
            Snowflake::from_u64(282265607313817601)
        );
    }

    #[test]
    pub fn bot_dm_context() {
        let location = format!(
            r#""channel": {{
                "flags": 0,
                "id": "1100155827400229026",
                "last_message_id": null,
                "recipients": [{USER}],
                "type": 1
            }},
            "channel_id": "1100155827400229026",
            "user": {USER}"#
        );
        let interaction = user_install_interaction(1, &location);
        let common = &interaction.common;

        assert_eq!(common.context, Some(InteractionContextType::BotDm));
        assert!(common.member.is_none());
        assert_eq!(common.invoking_user().unwrap().username, "bluefrog");
    }

    #[test]
    pub fn private_channel_context() {
        let location = format!(
            r#""channel": {{ "id": "1100155827400229026", "type": 3 }},
            "channel_id": "1100155827400229026",
            "user": {USER}"#
        );
        let interaction = user_install_interaction(2, &location);
        let common = &interaction.common;

        assert_eq!(common.context, Some(InteractionContextType::PrivateChannel));
        assert!(common.guild_id.is_none());
        assert!(common.member.is_none());
        assert_eq!(common.invoking_user().unwrap().display_name(), "Blue Frog");
    }

    #[test]
    pub fn guild_install_owners() {
        let json = r#"{
            "application_id": "1052322265397739523",
            "authorizing_integration_owners": { "0": "798662131062931547", "1": "282265607313817601" },
            "context": 0,
            "id": "1100173248714518568",
            "token": "A_UNIQUE_TOKEN",
            "type": 1,
            "version": 1
        }"#;

        let Interaction::Ping(ping) = serde_json::from_str::<Interaction>(json).unwrap() else {
            panic!("expected ping");
        };

        let owners = &ping.common.authorizing_integration_owners;
        assert_eq!(
            owners
                .get(ApplicationIntegrationType::GuildInstall)
                .unwrap(),
            Snowflake::from_u64(798662131062931547)
        );
        assert_eq!(
            owners.get(ApplicationIntegrationType::UserInstall).unwrap(),
            Snowflake::from_u64(282265607313817601)
        );
        assert!(ping.common.invoking_user().is_none());
    }
}