Skip to content

Commit

Permalink
Support VoiceSpeakingUpdates on VoiceConnection.
Browse files Browse the repository at this point in the history
  • Loading branch information
iopred committed Mar 18, 2016
1 parent 7bbcfe7 commit 584b06a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
6 changes: 1 addition & 5 deletions discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ func (s *Session) AddHandler(handler interface{}) func() {

h := reflect.ValueOf(handler)

handlers := s.handlers[eventType]
if handlers == nil {
handlers = []reflect.Value{}
}
s.handlers[eventType] = append(handlers, h)
s.handlers[eventType] = append(s.handlers[eventType], h)

// This must be done as we need a consistent reference to the
// reflected value, otherwise a RemoveHandler method would have
Expand Down
41 changes: 33 additions & 8 deletions voice.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ type VoiceConnection struct {

op4 voiceOP4
op2 voiceOP2

voiceSpeakingUpdateHandlers []VoiceSpeakingUpdateHandler
}

type VoiceSpeakingUpdateHandler func(vc *VoiceConnection, vs *VoiceSpeakingUpdate)

// Speaking sends a speaking notification to Discord over the voice websocket.
// This must be sent as true prior to sending audio and should be set to false
// once finished sending audio.
Expand Down Expand Up @@ -151,6 +155,21 @@ func (v *VoiceConnection) Close() {
}
}

// Adds a Handler for VoiceSpeakingUpdate events.
func (v *VoiceConnection) AddHandler(h VoiceSpeakingUpdateHandler) {
v.Lock()
defer v.Unlock()

v.voiceSpeakingUpdateHandlers = append(v.voiceSpeakingUpdateHandlers, h)
}

// VoiceSpeakingUpdate is a struct for a VoiceSpeakingUpdate event.
type VoiceSpeakingUpdate struct {
UserID string `json:"user_id"`
SSRC int `json:"ssrc"`
Speaking bool `json:"speaking"`
}

// ------------------------------------------------------------------------------------------------
// Unexported Internal Functions Below.
// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -345,14 +364,20 @@ func (v *VoiceConnection) wsEvent(messageType int, message []byte) {
return

case 5:
// SPEAKING TRUE/FALSE NOTIFICATION
/*
{
"user_id": "1238921738912",
"ssrc": 2,
"speaking": false
}
*/
if len(v.voiceSpeakingUpdateHandlers) == 0 {
return
}

voiceSpeakingUpdate := &VoiceSpeakingUpdate{}
if err := json.Unmarshal(e.RawData, voiceSpeakingUpdate); err != nil {
fmt.Println("voiceWS.onEvent VoiceSpeakingUpdate Unmarshal error: ", err)
printJSON(e.RawData)
return
}

for _, h := range v.voiceSpeakingUpdateHandlers {
h(v, voiceSpeakingUpdate)
}

default:
fmt.Println("UNKNOWN VOICE OP: ", e.Operation)
Expand Down

0 comments on commit 584b06a

Please sign in to comment.