Procházet zdrojové kódy

Merge pull request #102 from iopred/voice

VoiceServerUpdate and VoiceStateUpdate. Closes #103
Bruce před 9 roky
rodič
revize
a9d0be6634
2 změnil soubory, kde provedl 16 přidání a 9 odebrání
  1. 1 0
      structs.go
  2. 15 9
      wsapi.go

+ 1 - 0
structs.go

@@ -46,6 +46,7 @@ type Session struct {
 	OnMessageAck              func(*Session, *MessageAck)
 	OnUserUpdate              func(*Session, *User)
 	OnPresenceUpdate          func(*Session, *PresenceUpdate)
+	OnVoiceServerUpdate       func(*Session, *VoiceServerUpdate)
 	OnVoiceStateUpdate        func(*Session, *VoiceState)
 	OnChannelCreate           func(*Session, *Channel)
 	OnChannelUpdate           func(*Session, *Channel)

+ 15 - 9
wsapi.go

@@ -316,12 +316,21 @@ func (s *Session) event(messageType int, message []byte) {
 			return
 		}
 	case "VOICE_SERVER_UPDATE":
-		// TEMP CODE FOR TESTING VOICE
+		if s.Voice == nil && s.OnVoiceServerUpdate == nil {
+			break
+		}
 		var st *VoiceServerUpdate
 		if err = unmarshalEvent(e, &st); err == nil {
-			s.onVoiceServerUpdate(st)
+			if s.Voice != nil {
+				s.onVoiceServerUpdate(st)
+			}
+			if s.OnVoiceServerUpdate != nil {
+				s.OnVoiceServerUpdate(s, st)
+			}
+		}
+		if s.OnVoiceServerUpdate != nil {
+			return
 		}
-		return
 	case "VOICE_STATE_UPDATE":
 		if s.Voice == nil && s.OnVoiceStateUpdate == nil {
 			break
@@ -333,9 +342,11 @@ func (s *Session) event(messageType int, message []byte) {
 			}
 			if s.OnVoiceStateUpdate != nil {
 				s.OnVoiceStateUpdate(s, st)
-				return
 			}
 		}
+		if s.OnVoiceStateUpdate != nil {
+			return
+		}
 	case "USER_UPDATE":
 		if s.OnUserUpdate != nil {
 			var st *User
@@ -782,11 +793,6 @@ func (s *Session) onVoiceStateUpdate(st *VoiceState) {
 // connection and should happen after the VOICE_STATE event.
 func (s *Session) onVoiceServerUpdate(st *VoiceServerUpdate) {
 
-	// This shouldn't ever be the case, I don't think.
-	if s.Voice == nil {
-		return
-	}
-
 	// Store values for later use
 	s.Voice.token = st.Token
 	s.Voice.endpoint = st.Endpoint