11use serde:: de:: { Deserialize , Deserializer , Error as DeError } ;
22use serde:: ser:: { Serialize , Serializer } ;
33
4- #[ cfg( feature = "unstable_discord_api" ) ]
5- use super :: InstallationContext ;
6- use super :: { CommandInteraction , ComponentInteraction , ModalInteraction , PingInteraction } ;
4+ use super :: {
5+ CommandInteraction ,
6+ ComponentInteraction ,
7+ InstallationContext ,
8+ ModalInteraction ,
9+ PingInteraction ,
10+ } ;
711use crate :: internal:: prelude:: * ;
812use crate :: json:: from_value;
913use crate :: model:: guild:: PartialMember ;
10- use crate :: model:: id:: { ApplicationId , InteractionId } ;
11- #[ cfg( feature = "unstable_discord_api" ) ]
12- use crate :: model:: id:: { GuildId , MessageId , UserId } ;
14+ use crate :: model:: id:: { ApplicationId , GuildId , InteractionId , MessageId , UserId } ;
1315use crate :: model:: monetization:: Entitlement ;
1416use crate :: model:: user:: User ;
15- use crate :: model:: utils:: deserialize_val;
16- #[ cfg( feature = "unstable_discord_api" ) ]
17- use crate :: model:: utils:: StrOrInt ;
17+ use crate :: model:: utils:: { deserialize_val, remove_from_map, StrOrInt } ;
1818use crate :: model:: Permissions ;
1919
2020/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object)
@@ -299,7 +299,6 @@ bitflags! {
299299///
300300/// [Discord Docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-authorizing-integration-owners-object)
301301#[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
302- #[ cfg( feature = "unstable_discord_api" ) ]
303302#[ derive( Clone , Debug ) ]
304303#[ non_exhaustive]
305304pub enum AuthorizingIntegrationOwner {
@@ -315,12 +314,10 @@ pub enum AuthorizingIntegrationOwner {
315314}
316315
317316#[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
318- #[ cfg( feature = "unstable_discord_api" ) ]
319317#[ derive( Clone , Debug , Default ) ]
320318#[ repr( transparent) ]
321319pub struct AuthorizingIntegrationOwners ( pub Vec < AuthorizingIntegrationOwner > ) ;
322320
323- #[ cfg( feature = "unstable_discord_api" ) ]
324321impl < ' de > serde:: Deserialize < ' de > for AuthorizingIntegrationOwners {
325322 fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> StdResult < Self , D :: Error > {
326323 struct Visitor ;
@@ -366,7 +363,6 @@ impl<'de> serde::Deserialize<'de> for AuthorizingIntegrationOwners {
366363 }
367364}
368365
369- #[ cfg( feature = "unstable_discord_api" ) ]
370366impl serde:: Serialize for AuthorizingIntegrationOwners {
371367 fn serialize < S : Serializer > ( & self , serializer : S ) -> StdResult < S :: Ok , S :: Error > {
372368 use serde:: ser:: SerializeMap ;
@@ -419,28 +415,121 @@ pub struct MessageInteraction {
419415 pub member : Option < PartialMember > ,
420416}
421417
422- /// Metadata about the interaction, including the source of the interaction relevant server and
423- /// user IDs.
424418#[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
425- #[ derive( Clone , Debug , Deserialize , Serialize ) ]
426- #[ cfg ( feature = "unstable_discord_api" ) ]
427- pub struct MessageInteractionMetadata {
419+ #[ derive( Clone , Debug , serde :: Deserialize , serde :: Serialize ) ]
420+ #[ non_exhaustive ]
421+ pub struct MessageCommandInteractionMetadata {
428422 /// The ID of the interaction
429423 pub id : InteractionId ,
430- /// The type of interaction
431- #[ serde( rename = "type" ) ]
432- pub kind : InteractionType ,
433- /// The ID of the user who triggered the interaction
424+ /// The user who triggered the interaction
425+ pub user : User ,
426+ /// The IDs for installation context(s) related to an interaction.
427+ pub authorizing_integration_owners : AuthorizingIntegrationOwners ,
428+ /// The ID of the original response message, present only on follow-up messages.
429+ pub original_response_message_id : Option < MessageId > ,
430+ /// The user the command was run on, present only on user command interactions
431+ pub target_user : Option < User > ,
432+ /// The ID of the message the command was run on, present only on message command
433+ /// interactions.
434+ pub target_message_id : Option < MessageId > ,
435+ }
436+
437+ #[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
438+ #[ derive( Clone , Debug , serde:: Deserialize , serde:: Serialize ) ]
439+ #[ non_exhaustive]
440+ pub struct MessageComponentInteractionMetadata {
441+ /// The ID of the interaction
442+ pub id : InteractionId ,
443+ /// The user who triggered the interaction
444+ pub user : User ,
445+ /// The IDs for installation context(s) related to an interaction.
446+ pub authorizing_integration_owners : AuthorizingIntegrationOwners ,
447+ /// The ID of the original response message, present only on follow-up messages.
448+ pub original_response_message_id : Option < MessageId > ,
449+ /// The ID of the message that contained the interactive component
450+ pub interacted_message_id : MessageId ,
451+ }
452+
453+ #[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
454+ #[ derive( Clone , Debug , serde:: Deserialize , serde:: Serialize ) ]
455+ #[ non_exhaustive]
456+ pub struct MessageModalSubmitInteractionMetadata {
457+ /// The ID of the interaction
458+ pub id : InteractionId ,
459+ /// The user who triggered the interaction
434460 pub user : User ,
435461 /// The IDs for installation context(s) related to an interaction.
436- #[ serde( default ) ]
437462 pub authorizing_integration_owners : AuthorizingIntegrationOwners ,
438463 /// The ID of the original response message, present only on follow-up messages.
439464 pub original_response_message_id : Option < MessageId > ,
440- /// ID of the message that contained interactive component, present only on messages created
441- /// from component interactions.
442- pub interacted_message_id : Option < MessageId > ,
443- /// Metadata for the interaction that was used to open the modal, present only on modal submit
444- /// interactions
445- pub triggering_interaction_metadata : Option < Box < MessageInteractionMetadata > > ,
465+ /// Metadata for the interaction that was used to open the modal
466+ pub triggering_interaction_metadata : Box < MessageInteractionMetadata > ,
467+ }
468+
469+ /// Metadata about the interaction, including the source of the interaction relevant server and
470+ /// user IDs.
471+ #[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
472+ #[ derive( Clone , Debug ) ]
473+ #[ non_exhaustive]
474+ pub enum MessageInteractionMetadata {
475+ Command ( MessageCommandInteractionMetadata ) ,
476+ Component ( MessageComponentInteractionMetadata ) ,
477+ ModalSubmit ( MessageModalSubmitInteractionMetadata ) ,
478+ Unknown ( InteractionType ) ,
479+ }
480+
481+ impl < ' de > serde:: Deserialize < ' de > for MessageInteractionMetadata {
482+ fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> StdResult < Self , D :: Error > {
483+ let mut data = JsonMap :: deserialize ( deserializer) ?;
484+ let kind: InteractionType = remove_from_map ( & mut data, "type" ) ?;
485+
486+ match kind {
487+ InteractionType :: Command => deserialize_val ( Value :: from ( data) ) . map ( Self :: Command ) ,
488+ InteractionType :: Component => deserialize_val ( Value :: from ( data) ) . map ( Self :: Component ) ,
489+ InteractionType :: Modal => deserialize_val ( Value :: from ( data) ) . map ( Self :: ModalSubmit ) ,
490+
491+ unknown => Ok ( Self :: Unknown ( unknown) ) ,
492+ }
493+ }
494+ }
495+
496+ impl serde:: Serialize for MessageInteractionMetadata {
497+ fn serialize < S : Serializer > ( & self , serializer : S ) -> StdResult < S :: Ok , S :: Error > {
498+ #[ derive( serde:: Serialize ) ]
499+ struct WithType < T > {
500+ #[ serde( rename = "type" ) ]
501+ kind : InteractionType ,
502+ #[ serde( flatten) ]
503+ val : T ,
504+ }
505+
506+ fn serialize_with_type < S : Serializer , T : serde:: Serialize > (
507+ serializer : S ,
508+ val : T ,
509+ kind : InteractionType ,
510+ ) -> StdResult < S :: Ok , S :: Error > {
511+ let wrapper = WithType {
512+ kind,
513+ val,
514+ } ;
515+
516+ wrapper. serialize ( serializer)
517+ }
518+
519+ match self {
520+ MessageInteractionMetadata :: Command ( val) => {
521+ serialize_with_type ( serializer, val, InteractionType :: Command )
522+ } ,
523+ MessageInteractionMetadata :: Component ( val) => {
524+ serialize_with_type ( serializer, val, InteractionType :: Component )
525+ } ,
526+ MessageInteractionMetadata :: ModalSubmit ( val) => {
527+ serialize_with_type ( serializer, val, InteractionType :: Modal )
528+ } ,
529+ & MessageInteractionMetadata :: Unknown ( kind) => {
530+ tracing:: warn!( "Tried to serialize MessageInteractionMetadata::Unknown({}), serialising null instead" , u8 :: from( kind) ) ;
531+ serializer. serialize_none ( )
532+ } ,
533+ }
534+ }
446535}
0 commit comments