Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to deserialize gateway ready event due to missing fields #2216

Closed
rksm opened this issue Jun 1, 2023 · 15 comments
Closed

Unable to deserialize gateway ready event due to missing fields #2216

rksm opened this issue Jun 1, 2023 · 15 comments
Labels
c-model Affects the model crate i-invalid Already resolved or a non-issue.

Comments

@rksm
Copy link

rksm commented Jun 1, 2023

Hello, I was just testing the hello world example (https://github.com/twilight-rs/twilight/blob/main/examples/gateway-request-members.rs) and ran into an issue with the ready event not being deserializable. twilight expects two fields to be present that were not in my case, the following changes fixed it for me:

diff --git a/twilight-model/src/gateway/payload/incoming/ready.rs b/twilight-model/src/gateway/payload/incoming/ready.rs
index 3808e9d1e7..19e354620b 100644
--- a/twilight-model/src/gateway/payload/incoming/ready.rs
+++ b/twilight-model/src/gateway/payload/incoming/ready.rs
@@ -5,7 +5,8 @@ use serde::{Deserialize, Serialize};
 
 #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
 pub struct Ready {
-    pub application: PartialApplication,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub application: Option<PartialApplication>,
     pub guilds: Vec<UnavailableGuild>,
     pub resume_gateway_url: String,
     pub session_id: String,
@@ -43,10 +44,10 @@ mod tests {
         ];
 
         let ready = Ready {
-            application: PartialApplication {
+            application: Some(PartialApplication {
                 flags: ApplicationFlags::empty(),
                 id: Id::new(100),
-            },
+            }),
             guilds,
             resume_gateway_url: "wss://gateway.discord.gg".into(),
             session_id: "foo".to_owned(),
diff --git a/twilight-model/src/guild/unavailable_guild.rs b/twilight-model/src/guild/unavailable_guild.rs
index f4689e53e3..6198e5af91 100644
--- a/twilight-model/src/guild/unavailable_guild.rs
+++ b/twilight-model/src/guild/unavailable_guild.rs
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
 #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
 pub struct UnavailableGuild {
     pub id: Id<GuildMarker>,
+    #[serde(default)]
     pub unavailable: bool,
 }
 
diff --git a/twilight-standby/src/lib.rs b/twilight-standby/src/lib.rs
index c61dbcd2b9..d285811062 100644
--- a/twilight-standby/src/lib.rs
+++ b/twilight-standby/src/lib.rs
@@ -1366,10 +1366,10 @@ mod tests {
     #[tokio::test]
     async fn test_wait_for_event() {
         let ready = Ready {
-            application: PartialApplication {
+            application: Some(PartialApplication {
                 flags: ApplicationFlags::empty(),
                 id: Id::new(1),
-            },
+            }),
             guilds: Vec::new(),
             resume_gateway_url: "wss://gateway.discord.gg".into(),
             session_id: String::new(),
@rksm
Copy link
Author

rksm commented Jun 1, 2023

And a similar issue with PRESENCE_UPDATE, missing field guild_id, payload: {"t":"PRESENCE_UPDATE","s":3,"op":0,"d":{"user":{"username":"xyz","id":"123","global_name":null,"discriminator":"3218","avatar_decoration":null,"avatar":"0ed452de979d05d3be21f7a6549ba4ac"},"status":"offline","last_modified":1685661311926,"client_status":{},"broadcast":null,"activities":[]}}

@vilgotf vilgotf added t-bug Programming error in the library c-model Affects the model crate labels Jun 2, 2023
@rksm
Copy link
Author

rksm commented Jun 2, 2023

I wasn't able to pinpoint what exact fields needed fixing for the presence update but I got it to work by adding #[serde(default)] to all pub guild_id: Option<Id<GuildMarker>> fields: rksm@1ac78cb

@rksm
Copy link
Author

rksm commented Jun 3, 2023

And one more: 2023-06-03T01:05:25.614067Z WARN midjourney_api_v2: error receiving event gateway event could not be deserialized: source=missing field application_id at line 1 column 101, event={"t":"INTERACTION_CREATE","s":3,"op":0,"d":{"nonce":"1664800419089616897","id":"1114359151653568552"}}

It seems the fields token, type and application_id are all optional: rksm@f5a7f56

@Erk-
Copy link
Member

Erk- commented Jun 3, 2023

And one more: 2023-06-03T01:05:25.614067Z WARN midjourney_api_v2: error receiving event gateway event could not be deserialized: source=missing field application_id at line 1 column 101, event={"t":"INTERACTION_CREATE","s":3,"op":0,"d":{"nonce":"1664800419089616897","id":"1114359151653568552"}}

It seems the fields token, type and application_id are all optional: rksm@f5a7f56

Hmm that seems to go against what the documentation tells us https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-structure

@rksm
Copy link
Author

rksm commented Jun 3, 2023

For the background, I'm using twilight to interact with the midjourney bot. When posting a new /imagine interaction, the InteractionCreate event that comes back just contains an id and none field. twilight parses this as

InteractionCreate(
    InteractionCreate(
        Interaction {
            app_permissions: None,
            application_id: None,
            channel: None,
            channel_id: None,
            data: None,
            guild_id: None,
            guild_locale: None,
            id: Id<InteractionMarker>(1114365312637337740),
            kind: None,
            locale: None,
            member: None,
            message: None,
            token: None,
            user: None,
        },
    ),
)

@Erk-
Copy link
Member

Erk- commented Jun 3, 2023

For the background, I'm using twilight to interact with the midjourney bot. When posting a new /imagine interaction, the InteractionCreate event that comes back just contains an id and none field.

Does that mean that you are using it with a user token instead of a bot token?

@rksm
Copy link
Author

rksm commented Jun 3, 2023

Yes, I do.

@Erk- Erk- added i-invalid Already resolved or a non-issue. and removed t-bug Programming error in the library labels Jun 3, 2023
@Erk-
Copy link
Member

Erk- commented Jun 3, 2023

We generally only support the bot API, any other usage will be largely unsupported. As it is not documented and is against the Terms of Service of Discord.

@Erk- Erk- closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2023
@rksm
Copy link
Author

rksm commented Jun 3, 2023

So what about the remaining deserialization issues?

@Erk-
Copy link
Member

Erk- commented Jun 3, 2023

So what about the remaining deserialization issues?

Do they happen when you connect with a bot token?

@rksm
Copy link
Author

rksm commented Jun 3, 2023

I have no idea but this is about #[serde(default)] annotations of already optional fields. This stuff breaks because those fields are expected but not present.

@Erk-
Copy link
Member

Erk- commented Jun 3, 2023

@rksm
Copy link
Author

rksm commented Jun 3, 2023

Sorry to hear that. Anyway, thanks for your time.

@RedKinda

This comment was marked as off-topic.

@rksm

This comment was marked as off-topic.

@twilight-rs twilight-rs locked as off-topic and limited conversation to collaborators Jun 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c-model Affects the model crate i-invalid Already resolved or a non-issue.
Projects
None yet
Development

No branches or pull requests

4 participants