twilight: Unable to deserialize gateway ready event due to missing fields

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(),

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Userbotting is against ToS of both Discord and Midjourney, and is grounds for a permanent ban -signed Midjourney dev