From 584b06aae5b7cabb4c9e6c52faa4fe81a55767f8 Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Fri, 18 Mar 2016 10:50:25 -0700 Subject: [PATCH 1/3] Support VoiceSpeakingUpdates on VoiceConnection. --- discord.go | 6 +----- voice.go | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/discord.go b/discord.go index f70efa7..c2899a0 100644 --- a/discord.go +++ b/discord.go @@ -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 diff --git a/voice.go b/voice.go index 7f56e89..5c093c6 100644 --- a/voice.go +++ b/voice.go @@ -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. @@ -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. // ------------------------------------------------------------------------------------------------ @@ -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) From 6e568533b26724ffc33d67c1b66e0ce10bbefe23 Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Sat, 19 Mar 2016 16:22:36 -0700 Subject: [PATCH 2/3] Early support for PresencesReplace --- events.go | 4 ++++ structs.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/events.go b/events.go index 4efe722..dc087bf 100644 --- a/events.go +++ b/events.go @@ -34,6 +34,7 @@ var eventToInterface = map[string]interface{}{ "MESSAGE_UPDATE": MessageUpdate{}, "MESSAGE_DELETE": MessageDelete{}, "PRESENCE_UPDATE": PresenceUpdate{}, + "PRESENCES_REPLACE": PresencesReplace{}, "READY": Ready{}, "USER_UPDATE": UserUpdate{}, "USER_SETTINGS_UPDATE": UserSettingsUpdate{}, @@ -129,6 +130,9 @@ type GuildRoleUpdate struct { *GuildRole } +// PresencesReplace is an array of Presences for an event. +type PresencesReplace []*Presence + // VoiceStateUpdate is a wrapper struct for an event. type VoiceStateUpdate struct { *VoiceState diff --git a/structs.go b/structs.go index d7a83bd..460a190 100644 --- a/structs.go +++ b/structs.go @@ -298,7 +298,7 @@ type TypingStart struct { Timestamp int `json:"timestamp"` } -// A PresenceUpdate stores data for the pressence update websocket event. +// A PresenceUpdate stores data for the presence update websocket event. type PresenceUpdate struct { User *User `json:"user"` Status string `json:"status"` From 7e7887a936f6e05e4a64be98591642b1afaca2de Mon Sep 17 00:00:00 2001 From: andrei Date: Sat, 19 Mar 2016 18:58:47 -0700 Subject: [PATCH 3/3] Fix channels inside a guild from GUILD_CREATE having a null GuildID --- state.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/state.go b/state.go index 17033b2..901fd31 100644 --- a/state.go +++ b/state.go @@ -73,6 +73,11 @@ func (s *State) GuildAdd(guild *Guild) error { } } + // Otherwise, update the channels to point to the right guild + for _, c := range guild.Channels { + c.GuildID = guild.ID + } + s.Guilds = append(s.Guilds, guild) return nil }