diff --git a/discord.go b/discord.go index 4f7d93c..b3415c5 100644 --- a/discord.go +++ b/discord.go @@ -207,6 +207,8 @@ func (s *Session) handle(event interface{}) { handlerParameters := []reflect.Value{reflect.ValueOf(s), reflect.ValueOf(event)} + s.onInterface(event) + if handlers, ok := s.handlers[nil]; ok { for _, handler := range handlers { go handler.Call(handlerParameters) @@ -226,24 +228,32 @@ func (s *Session) initialize() { s.log(LogInformational, "called") s.handlersMu.Lock() + defer s.handlersMu.Unlock() + if s.handlers != nil { - s.handlersMu.Unlock() return } s.handlers = map[interface{}][]reflect.Value{} - s.handlersMu.Unlock() +} - s.AddHandler(s.onReady) - s.AddHandler(s.onResumed) - s.AddHandler(s.onVoiceServerUpdate) - s.AddHandler(s.onVoiceStateUpdate) - s.AddHandler(s.State.onReady) - s.AddHandler(s.State.onInterface) +// onInterface handles all internal events and routes them to the appropriate internal handler. +func (s *Session) onInterface(i interface{}) { + switch t := i.(type) { + case *Ready: + s.onReady(t) + case *Resumed: + s.onResumed(t) + case *VoiceServerUpdate: + go s.onVoiceServerUpdate(t) + case *VoiceStateUpdate: + go s.onVoiceStateUpdate(t) + } + s.State.onInterface(s, i) } // onReady handles the ready event. -func (s *Session) onReady(se *Session, r *Ready) { +func (s *Session) onReady(r *Ready) { // Store the SessionID within the Session struct. s.sessionID = r.SessionID @@ -253,7 +263,7 @@ func (s *Session) onReady(se *Session, r *Ready) { } // onResumed handles the resumed event. -func (s *Session) onResumed(se *Session, r *Resumed) { +func (s *Session) onResumed(r *Resumed) { // Start the heartbeat to keep the connection alive. go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval) diff --git a/state.go b/state.go index 5b6ed07..fa5989b 100644 --- a/state.go +++ b/state.go @@ -639,6 +639,12 @@ func (s *State) onInterface(se *Session, i interface{}) (err error) { if s == nil { return ErrNilState } + + r, ok := i.(*Ready) + if ok { + return s.onReady(se, r) + } + if !se.StateEnabled { return nil } diff --git a/wsapi.go b/wsapi.go index ab4f3ad..449531c 100644 --- a/wsapi.go +++ b/wsapi.go @@ -509,7 +509,7 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *Voi } // onVoiceStateUpdate handles Voice State Update events on the data websocket. -func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) { +func (s *Session) onVoiceStateUpdate(st *VoiceStateUpdate) { // If we don't have a connection for the channel, don't bother if st.ChannelID == "" { @@ -523,7 +523,7 @@ func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) { } // We only care about events that are about us. - if se.State.User.ID != st.UserID { + if s.State.User.ID != st.UserID { return } @@ -537,7 +537,7 @@ func (s *Session) onVoiceStateUpdate(se *Session, st *VoiceStateUpdate) { // This is also fired if the Guild's voice region changes while connected // to a voice channel. In that case, need to re-establish connection to // the new region endpoint. -func (s *Session) onVoiceServerUpdate(se *Session, st *VoiceServerUpdate) { +func (s *Session) onVoiceServerUpdate(st *VoiceServerUpdate) { s.log(LogInformational, "called")