From b044b6d5d4ae0a9472fb783797516f9becaee848 Mon Sep 17 00:00:00 2001 From: "Alex M. M" Date: Mon, 19 Oct 2020 17:57:22 +0200 Subject: [PATCH] Deserialize the correct data from the `GUILD_DELETE` event (#1028) --- src/cache/mod.rs | 25 ++----------------------- src/client/event_handler.rs | 12 ++++++++++-- src/model/event.rs | 6 +++--- src/model/guild/mod.rs | 5 ++--- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index ecf168faee3..10e4601a896 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -983,7 +983,6 @@ mod test { cache::{Cache, CacheUpdate, Settings}, model::prelude::*, }; - use crate::model::guild::PremiumTier::Tier2; #[tokio::test] async fn test_cache_messages() { @@ -1135,29 +1134,9 @@ mod test { assert!(cache.update(&mut event).await.is_none()); let mut guild_delete = GuildDeleteEvent { - guild: PartialGuild { + guild: GuildUnavailable { id: GuildId(1), - afk_channel_id: None, - afk_timeout: 0, - default_message_notifications: DefaultMessageNotificationLevel::All, - embed_channel_id: None, - embed_enabled: false, - emojis: HashMap::new(), - features: vec![], - icon: None, - mfa_level: MfaLevel::None, - name: String::new(), - owner_id: UserId(3), - region: String::new(), - roles: HashMap::new(), - splash: None, - verification_level: VerificationLevel::Low, - description: None, - premium_tier: Tier2, - premium_subscription_count: 12, - banner: None, - vanity_url_code: Some("bruhmoment".to_string()), - _nonexhaustive: (), + unavailable: false, }, _nonexhaustive: (), }; diff --git a/src/client/event_handler.rs b/src/client/event_handler.rs index 93803f49409..718a65ea790 100644 --- a/src/client/event_handler.rs +++ b/src/client/event_handler.rs @@ -99,14 +99,22 @@ pub trait EventHandler: Send + Sync { /// /// Provides the partial data of the guild sent by discord, /// and the full data from the cache, if available. + /// + /// The `unavailable` flag in the partial data determines the status of the guild. + /// If the flag is false, the bot was removed from the guild, either by being + /// kicked or banned. If the flag is true, the guild went offline. #[cfg(feature = "cache")] - async fn guild_delete(&self, _ctx: Context, _incomplete: PartialGuild, _full: Option) {} + async fn guild_delete(&self, _ctx: Context, _incomplete: GuildUnavailable, _full: Option) {} /// Dispatched when a guild is deleted. /// /// Provides the partial data of the guild sent by discord. + /// + /// The `unavailable` flag in the partial data determines the status of the guild. + /// If the flag is false, the bot was removed from the guild, either by being + /// kicked or banned. If the flag is true, the guild went offline. #[cfg(not(feature = "cache"))] - async fn guild_delete(&self, _ctx: Context, _incomplete: PartialGuild) {} + async fn guild_delete(&self, _ctx: Context, _incomplete: GuildUnavailable) {} /* the emojis were updated. */ diff --git a/src/model/event.rs b/src/model/event.rs index 7dca1680dea..a6ee184edff 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -339,7 +339,7 @@ impl Serialize for GuildCreateEvent { #[derive(Clone, Debug)] pub struct GuildDeleteEvent { - pub guild: PartialGuild, + pub guild: GuildUnavailable, pub(crate) _nonexhaustive: (), } @@ -369,7 +369,7 @@ impl CacheUpdate for GuildDeleteEvent { impl<'de> Deserialize<'de> for GuildDeleteEvent { fn deserialize>(deserializer: D) -> StdResult { Ok(Self { - guild: PartialGuild::deserialize(deserializer)?, + guild: GuildUnavailable::deserialize(deserializer)?, _nonexhaustive: (), }) } @@ -378,7 +378,7 @@ impl<'de> Deserialize<'de> for GuildDeleteEvent { impl Serialize for GuildDeleteEvent { fn serialize(&self, serializer: S) -> StdResult where S: Serializer { - PartialGuild::serialize(&self.guild, serializer) + GuildUnavailable::serialize(&self.guild, serializer) } } diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 8eb0f54b244..a0d9e1a5d1e 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -2059,13 +2059,12 @@ impl InviteGuild { /// Data for an unavailable guild. #[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub struct GuildUnavailable { - /// The Id of the [`Guild`] that is unavailable. + /// The Id of the [`Guild`] that may be unavailable. /// /// [`Guild`]: struct.Guild.html pub id: GuildId, /// Indicator of whether the guild is unavailable. - /// - /// This should always be `true`. + #[serde(default)] pub unavailable: bool, }