Fix some gateway deserialization errors

This commit is contained in:
kozabrada123 2023-05-14 19:55:43 +02:00
parent fcbca459dc
commit 7e6507c206
2 changed files with 25 additions and 14 deletions

View File

@ -9,7 +9,7 @@ use std::collections::HashMap;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::from_value; use serde_json::from_value;
use serde_aux::field_attributes::deserialize_option_number_from_string; use serde_aux::{field_attributes::deserialize_option_number_from_string, prelude::deserialize_string_from_number};
use crate::{api::limits::Limits, instance::Instance}; use crate::{api::limits::Limits, instance::Instance};
@ -323,8 +323,8 @@ pub struct RoleObject {
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq, Hash)] #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct UserObject { pub struct UserObject {
pub id: String, pub id: String,
username: String, username: Option<String>,
discriminator: String, discriminator: Option<String>,
avatar: Option<String>, avatar: Option<String>,
bot: Option<bool>, bot: Option<bool>,
system: Option<bool>, system: Option<bool>,
@ -335,6 +335,7 @@ pub struct UserObject {
email: Option<String>, email: Option<String>,
/// This field comes as either a string or a number as a string /// This field comes as either a string or a number as a string
/// So we need to account for that /// So we need to account for that
#[serde(default)]
#[serde(deserialize_with = "deserialize_option_number_from_string")] #[serde(deserialize_with = "deserialize_option_number_from_string")]
flags: Option<i32>, flags: Option<i32>,
premium_since: Option<String>, premium_since: Option<String>,
@ -401,7 +402,7 @@ pub struct Message {
edited_timestamp: Option<String>, edited_timestamp: Option<String>,
tts: bool, tts: bool,
mention_everyone: bool, mention_everyone: bool,
mentions: Vec<UserObject>, mentions: Option<Vec<UserObject>>,
mention_roles: Vec<String>, mention_roles: Vec<String>,
mention_channels: Option<Vec<ChannelMention>>, mention_channels: Option<Vec<ChannelMention>>,
pub attachments: Vec<DiscordFileAttachment>, pub attachments: Vec<DiscordFileAttachment>,
@ -434,7 +435,7 @@ pub struct MessageCreate {
message: Message, message: Message,
guild_id: Option<String>, guild_id: Option<String>,
member: Option<GuildMember>, member: Option<GuildMember>,
mentions: Vec<MessageCreateUser>, mentions: Option<Vec<MessageCreateUser>>,
} }
#[derive(Debug, Serialize, Deserialize, Default)] #[derive(Debug, Serialize, Deserialize, Default)]
@ -513,7 +514,7 @@ pub struct MessageUpdate {
message: PartialMessage, message: PartialMessage,
guild_id: Option<String>, guild_id: Option<String>,
member: Option<GuildMember>, member: Option<GuildMember>,
mentions: Vec<(UserObject, GuildMember)>, // Not sure if this is correct: https://discord.com/developers/docs/topics/gateway-events#message-create mentions: Option<Vec<(UserObject, GuildMember)>>, // Not sure if this is correct: https://discord.com/developers/docs/topics/gateway-events#message-create
} }
impl WebSocketEvent for MessageUpdate {} impl WebSocketEvent for MessageUpdate {}
@ -766,7 +767,7 @@ pub struct GuildMember {
pub user: Option<UserObject>, pub user: Option<UserObject>,
pub nick: Option<String>, pub nick: Option<String>,
pub avatar: Option<String>, pub avatar: Option<String>,
pub roles: Vec<RoleObject>, pub roles: Vec<String>,
pub joined_at: String, pub joined_at: String,
pub premium_since: Option<String>, pub premium_since: Option<String>,
pub deaf: bool, pub deaf: bool,
@ -829,8 +830,13 @@ pub struct Tag {
pub struct PermissionOverwrite { pub struct PermissionOverwrite {
pub id: String, pub id: String,
#[serde(rename = "type")] #[serde(rename = "type")]
pub overwrite_type: u8, #[serde(deserialize_with = "deserialize_string_from_number")]
pub overwrite_type: String,
#[serde(default)]
#[serde(deserialize_with = "deserialize_string_from_number")]
pub allow: String, pub allow: String,
#[serde(default)]
#[serde(deserialize_with = "deserialize_string_from_number")]
pub deny: String, pub deny: String,
} }
@ -1009,7 +1015,7 @@ pub struct GatewayIdentifyConnectionProps {
/// See https://discord.com/developers/docs/topics/gateway-events#presence-update-presence-update-event-fields /// See https://discord.com/developers/docs/topics/gateway-events#presence-update-presence-update-event-fields
pub struct PresenceUpdate { pub struct PresenceUpdate {
pub user: UserObject, pub user: UserObject,
pub guild_id: String, pub guild_id: Option<String>,
pub status: String, pub status: String,
pub activities: Vec<Activity>, pub activities: Vec<Activity>,
pub client_status: ClientStatusObject, pub client_status: ClientStatusObject,
@ -1608,7 +1614,7 @@ pub struct MessageACK {
} }
impl WebSocketEvent for MessageACK {} impl WebSocketEvent for MessageACK {}
#[derive(Debug, Default, Deserialize, Serialize)] #[derive(Debug, Default, Deserialize, Serialize, Clone)]
pub struct GatewayPayload { pub struct GatewayPayload {
pub op: u8, pub op: u8,
pub d: Option<serde_json::Value>, pub d: Option<serde_json::Value>,

View File

@ -194,7 +194,7 @@ impl Gateway {
// Dispatch // Dispatch
// An event was dispatched, we need to look at the gateway event name t // An event was dispatched, we need to look at the gateway event name t
0 => { 0 => {
let gateway_payload_t = gateway_payload.t.unwrap(); let gateway_payload_t = gateway_payload.clone().t.unwrap();
println!("GW: Received {}..", gateway_payload_t); println!("GW: Received {}..", gateway_payload_t);
@ -387,6 +387,8 @@ impl Gateway {
let new_data: PresenceUpdate = serde_json::from_value(gateway_payload.d.unwrap()).unwrap(); let new_data: PresenceUpdate = serde_json::from_value(gateway_payload.d.unwrap()).unwrap();
self.events.lock().await.user.presence_update.update_data(new_data).await; self.events.lock().await.user.presence_update.update_data(new_data).await;
} }
// What is this?
"PASSIVE_UPDATE_V1" => {}
"STAGE_INSTANCE_CREATE" => {} "STAGE_INSTANCE_CREATE" => {}
"STAGE_INSTANCE_UPDATE" => {} "STAGE_INSTANCE_UPDATE" => {}
"STAGE_INSTANCE_DELETE" => {} "STAGE_INSTANCE_DELETE" => {}
@ -408,7 +410,10 @@ impl Gateway {
let new_data: WebhooksUpdate = serde_json::from_value(gateway_payload.d.unwrap()).unwrap(); let new_data: WebhooksUpdate = serde_json::from_value(gateway_payload.d.unwrap()).unwrap();
self.events.lock().await.webhooks.update.update_data(new_data).await; self.events.lock().await.webhooks.update.update_data(new_data).await;
} }
_ => {panic!("Invalid gateway event ({})", &gateway_payload_t)} _ => {
//panic!("Invalid gateway event ({})", &gateway_payload_t)
println!("New gateway event ({})", &gateway_payload_t);
}
} }
} }
// Heartbeat // Heartbeat
@ -429,7 +434,7 @@ impl Gateway {
println!("GW: Received Heartbeat ACK"); println!("GW: Received Heartbeat ACK");
} }
2 | 3 | 4 | 6 | 8 => {panic!("Received Gateway op code that's meant to be sent, not received ({})", gateway_payload.op)} 2 | 3 | 4 | 6 | 8 => {panic!("Received Gateway op code that's meant to be sent, not received ({})", gateway_payload.op)}
_ => {panic!("Received Invalid Gateway op code ({})", gateway_payload.op)} _ => {println!("Received new Gateway op code ({})", gateway_payload.op)}
} }
// If we have an active heartbeat thread and we received a seq number we should let it know // If we have an active heartbeat thread and we received a seq number we should let it know