From 019d0fe9afc8198fb8e4bec659f7eed058f50dce Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 4 Apr 2016 13:45:41 -0700 Subject: [PATCH] Fix lazy-loading of guilds not working We're moving to lazy-loading guilds, meaning bots now recieve a READY packet with guilds that only have 'id' and 'unavailable' fields. Post READY, the client then lazyily recieves GUILD_CREATE's for the rest of the guilds it's in. Previously we assumed the first instance of the guild we got (from ready) had all the required information, now we need to use the GUILD_CREATE if the guild was marked as unavailable. --- state.go | 28 +++++++++++++++------------- structs.go | 1 + 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/state.go b/state.go index fbbd238..68267d3 100644 --- a/state.go +++ b/state.go @@ -57,24 +57,26 @@ func (s *State) GuildAdd(guild *Guild) error { s.Lock() defer s.Unlock() - // If the guild exists, replace it. - for i, g := range s.Guilds { - if g.ID == guild.ID { - // Don't stomp on properties that don't come in updates. - guild.Members = g.Members - guild.Presences = g.Presences - guild.Channels = g.Channels - guild.VoiceStates = g.VoiceStates - s.Guilds[i] = guild - return nil - } - } - // Otherwise, update the channels to point to the right guild for _, c := range guild.Channels { c.GuildID = guild.ID } + // If the guild exists, replace it. + for i, g := range s.Guilds { + if g.ID == guild.ID { + // If this guild already exists with data, don't stomp on props + if !g.Unavailable { + guild.Members = g.Members + guild.Presences = g.Presences + guild.Channels = g.Channels + guild.VoiceStates = g.VoiceStates + } + s.Guilds[i] = guild + return nil + } + } + s.Guilds = append(s.Guilds, guild) return nil } diff --git a/structs.go b/structs.go index 460a190..3f6cd35 100644 --- a/structs.go +++ b/structs.go @@ -178,6 +178,7 @@ type Guild struct { Presences []*Presence `json:"presences"` Channels []*Channel `json:"channels"` VoiceStates []*VoiceState `json:"voice_states"` + Unavailable bool `json:"unavailable"` } // A GuildParams stores all the data needed to update discord guild settings