twilight_model/application/interaction/callback/
response.rs

1use serde::Deserialize;
2
3use super::{InteractionCallback, resource::InteractionCallbackResource};
4
5/// Included when creating an interaction response with a response
6///
7/// See [Discord Docs/Interaction Callback Response Object]
8///
9/// [Discord Docs/Interaction Callback Response Object]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-response-object
10#[derive(Clone, Debug, Deserialize, PartialEq)]
11pub struct InteractionCallbackResponse {
12    /// The interaction object associated with the interaction response.
13    pub interaction: InteractionCallback,
14    /// The resource that was created by the interaction response.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub resource: Option<InteractionCallbackResource>,
17}
18
19#[cfg(test)]
20mod tests {
21
22    use serde_test::Token;
23
24    use crate::{
25        application::interaction::{
26            InteractionType,
27            callback::{InteractionCallback, resource::InteractionCallbackResource},
28        },
29        http::interaction::InteractionResponseType,
30        id::Id,
31    };
32
33    use super::InteractionCallbackResponse;
34
35    #[test]
36    #[allow(clippy::too_many_lines, deprecated)]
37    fn test_response_full() {
38        let value = InteractionCallbackResponse {
39            interaction: InteractionCallback {
40                id: Id::new(1),
41                kind: InteractionType::ApplicationCommand,
42                activity_instance_id: None,
43                response_message_id: Some(Id::new(1)),
44                response_message_loading: Some(false),
45                response_message_ephemeral: Some(false),
46            },
47            resource: Some(InteractionCallbackResource {
48                kind: InteractionResponseType::ChannelMessageWithSource,
49                activity_instance: None,
50                message: None,
51            }),
52        };
53
54        serde_test::assert_de_tokens(
55            &value,
56            &[
57                Token::Struct {
58                    name: "InteractionCallbackResponse",
59                    len: 2,
60                },
61                Token::Str("interaction"),
62                Token::Struct {
63                    name: "InteractionCallback",
64                    len: 5,
65                },
66                Token::Str("id"),
67                Token::NewtypeStruct { name: "Id" },
68                Token::Str("1"),
69                Token::Str("type"),
70                Token::U8(InteractionType::ApplicationCommand as u8),
71                Token::Str("response_message_id"),
72                Token::Some,
73                Token::NewtypeStruct { name: "Id" },
74                Token::Str("1"),
75                Token::Str("response_message_loading"),
76                Token::Some,
77                Token::Bool(false),
78                Token::Str("response_message_ephemeral"),
79                Token::Some,
80                Token::Bool(false),
81                Token::StructEnd,
82                Token::Str("resource"),
83                Token::Some,
84                Token::Struct {
85                    name: "InteractionCallbackResource",
86                    len: 3,
87                },
88                Token::Str("type"),
89                Token::U8(InteractionResponseType::ChannelMessageWithSource as u8),
90                Token::StructEnd,
91                Token::StructEnd,
92            ],
93        );
94    }
95}