Simplify Guild.Unavailable and correctly merge GUILD_UPDATE events.

This commit is contained in:
Chris Rhodes 2016-09-08 19:39:29 -07:00
parent ae30611faf
commit 678756c9a9
2 changed files with 28 additions and 10 deletions

View file

@ -98,17 +98,35 @@ func (s *State) GuildAdd(guild *Guild) error {
s.channelMap[c.ID] = c
}
// If the guild exists, replace it.
if g, ok := s.guildMap[guild.ID]; ok {
// If this guild already exists with data, don't stomp on props.
if g.Unavailable != nil && !*g.Unavailable {
// The guild already exists in state.
if guild.Unavailable {
// If the new guild is unavailable, just update the `unavailable` property on the
// current guild.
g.Unavailable = guild.Unavailable
} else {
// We are about to replace `g` in the state with `guild`, but first we need to
// make sure we preserve any fields that the `guild` doesn't contain from `g`.
if guild.Roles == nil {
guild.Roles = g.Roles
}
if guild.Emojis == nil {
guild.Emojis = g.Emojis
}
if guild.Members == nil {
guild.Members = g.Members
}
if guild.Presences == nil {
guild.Presences = g.Presences
}
if guild.Channels == nil {
guild.Channels = g.Channels
}
if guild.VoiceStates == nil {
guild.VoiceStates = g.VoiceStates
}
*g = *guild
}
return nil
}

View file

@ -217,7 +217,7 @@ type Guild struct {
Presences []*Presence `json:"presences"`
Channels []*Channel `json:"channels"`
VoiceStates []*VoiceState `json:"voice_states"`
Unavailable *bool `json:"unavailable"`
Unavailable bool `json:"unavailable"`
}
// A GuildParams stores all the data needed to update discord guild settings