From 8164119cac63d0c0a0134ddb2c5eb974a692bceb Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Tue, 27 Sep 2016 21:44:16 -0700 Subject: [PATCH] Support a very light state in all cases to support b1nzy's upcoming PR --- discord.go | 1 + state.go | 85 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/discord.go b/discord.go index 776f143..77bde70 100644 --- a/discord.go +++ b/discord.go @@ -237,6 +237,7 @@ func (s *Session) initialize() { s.AddHandler(s.onResumed) s.AddHandler(s.onVoiceServerUpdate) s.AddHandler(s.onVoiceStateUpdate) + s.AddHandler(s.State.onReady) s.AddHandler(s.State.onInterface) } diff --git a/state.go b/state.go index d2d90f3..798c18c 100644 --- a/state.go +++ b/state.go @@ -55,33 +55,6 @@ func NewState() *State { } } -// OnReady takes a Ready event and updates all internal state. -func (s *State) OnReady(r *Ready) error { - if s == nil { - return ErrNilState - } - - s.Lock() - defer s.Unlock() - - s.Ready = *r - - for _, g := range s.Guilds { - s.guildMap[g.ID] = g - - for _, c := range g.Channels { - c.GuildID = g.ID - s.channelMap[c.ID] = c - } - } - - for _, c := range s.PrivateChannels { - s.channelMap[c.ID] = c - } - - return nil -} - // GuildAdd adds a guild to the current world state, or // updates it if it already exists. func (s *State) GuildAdd(guild *Guild) error { @@ -619,6 +592,59 @@ func (s *State) Message(channelID, messageID string) (*Message, error) { return nil, errors.New("Message not found.") } +// OnReady takes a Ready event and updates all internal state. +func (s *State) onReady(se *Session, r *Ready) (err error) { + // We must track at least the current user for Voice, if state is + // either nil or disabled, we will set a very simple state item. + if s == nil || !se.StateEnabled { + ready := Ready{ + Version: r.Version, + SessionID: r.SessionID, + HeartbeatInterval: r.HeartbeatInterval, + User: r.User, + } + + if s == nil { + *s = State{ + TrackChannels: false, + TrackEmojis: false, + TrackMembers: false, + TrackRoles: false, + TrackVoice: false, + guildMap: make(map[string]*Guild), + channelMap: make(map[string]*Channel), + } + } + + s.Lock() + defer s.Unlock() + + s.Ready = ready + + return nil + } + + s.Lock() + defer s.Unlock() + + s.Ready = *r + + for _, g := range s.Guilds { + s.guildMap[g.ID] = g + + for _, c := range g.Channels { + c.GuildID = g.ID + s.channelMap[c.ID] = c + } + } + + for _, c := range s.PrivateChannels { + s.channelMap[c.ID] = c + } + + return nil +} + // onInterface handles all events related to states. func (s *State) onInterface(se *Session, i interface{}) (err error) { if s == nil { @@ -629,8 +655,6 @@ func (s *State) onInterface(se *Session, i interface{}) (err error) { } switch t := i.(type) { - case *Ready: - err = s.OnReady(t) case *GuildCreate: err = s.GuildAdd(t.Guild) case *GuildUpdate: @@ -702,6 +726,9 @@ func (s *State) onInterface(se *Session, i interface{}) (err error) { // userID : The ID of the user to calculate permissions for. // channelID : The ID of the channel to calculate permission for. func (s *State) UserChannelPermissions(userID, channelID string) (apermissions int, err error) { + if s == nil { + return 0, ErrNilState + } channel, err := s.Channel(channelID) if err != nil {