|
@@ -29,16 +29,18 @@ import (
|
|
|
|
|
|
|
|
|
type VoiceConnection struct {
|
|
|
- sync.Mutex
|
|
|
- Ready bool
|
|
|
- Debug bool
|
|
|
- Receive bool
|
|
|
- OP2 *voiceOP2
|
|
|
- OpusSend chan []byte
|
|
|
- OpusRecv chan *Packet
|
|
|
- GuildID string
|
|
|
- ChannelID string
|
|
|
- UserID string
|
|
|
+ sync.Mutex
|
|
|
+ Debug bool
|
|
|
+ Ready bool
|
|
|
+ GuildID string
|
|
|
+ ChannelID string
|
|
|
+ UserID string
|
|
|
+
|
|
|
+ OpusSend chan []byte
|
|
|
+ OpusRecv chan *Packet
|
|
|
+
|
|
|
+ Receive bool
|
|
|
+ OP2 *voiceOP2
|
|
|
|
|
|
|
|
|
|
|
@@ -94,9 +96,10 @@ type voiceHandshakeOp struct {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
func (v *VoiceConnection) Open() (err error) {
|
|
|
+
|
|
|
v.Lock()
|
|
|
defer v.Unlock()
|
|
|
|
|
@@ -131,19 +134,20 @@ func (v *VoiceConnection) Open() (err error) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
func (v *VoiceConnection) WaitUntilConnected() error {
|
|
|
|
|
|
i := 0
|
|
|
for {
|
|
|
+ if v.Ready {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
if i > 10 {
|
|
|
return fmt.Errorf("Timeout waiting for voice.")
|
|
|
}
|
|
|
|
|
|
- if v.Ready {
|
|
|
- return nil
|
|
|
- }
|
|
|
time.Sleep(1 * time.Second)
|
|
|
i++
|
|
|
}
|
|
@@ -151,6 +155,100 @@ func (v *VoiceConnection) WaitUntilConnected() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+type voiceSpeakingData struct {
|
|
|
+ Speaking bool `json:"speaking"`
|
|
|
+ Delay int `json:"delay"`
|
|
|
+}
|
|
|
+
|
|
|
+type voiceSpeakingOp struct {
|
|
|
+ Op int `json:"op"`
|
|
|
+ Data voiceSpeakingData `json:"d"`
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (v *VoiceConnection) Speaking(b bool) (err error) {
|
|
|
+
|
|
|
+ if v.wsConn == nil {
|
|
|
+ return fmt.Errorf("No VoiceConnection websocket.")
|
|
|
+ }
|
|
|
+
|
|
|
+ data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}}
|
|
|
+ err = v.wsConn.WriteJSON(data)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Speaking() write json error:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err error) {
|
|
|
+
|
|
|
+ data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}}
|
|
|
+ err = v.session.wsConn.WriteJSON(data)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (v *VoiceConnection) Disconnect() (err error) {
|
|
|
+
|
|
|
+
|
|
|
+ if v.sessionID != "" {
|
|
|
+ data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
|
|
|
+ err = v.session.wsConn.WriteJSON(data)
|
|
|
+ v.sessionID = ""
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ v.Close()
|
|
|
+
|
|
|
+ delete(v.session.VoiceConnections, v.GuildID)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func (v *VoiceConnection) Close() {
|
|
|
+
|
|
|
+ v.Lock()
|
|
|
+ defer v.Unlock()
|
|
|
+
|
|
|
+ v.Ready = false
|
|
|
+
|
|
|
+ if v.close != nil {
|
|
|
+ close(v.close)
|
|
|
+ v.close = nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if v.UDPConn != nil {
|
|
|
+ err := v.UDPConn.Close()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("error closing udp connection: ", err)
|
|
|
+ }
|
|
|
+ v.UDPConn = nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if v.wsConn != nil {
|
|
|
+ err := v.wsConn.Close()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("error closing websocket connection: ", err)
|
|
|
+ }
|
|
|
+ v.wsConn = nil
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{}) {
|
|
@@ -298,36 +396,6 @@ func (v *VoiceConnection) wsHeartbeat(wsConn *websocket.Conn, close <-chan struc
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type voiceSpeakingData struct {
|
|
|
- Speaking bool `json:"speaking"`
|
|
|
- Delay int `json:"delay"`
|
|
|
-}
|
|
|
-
|
|
|
-type voiceSpeakingOp struct {
|
|
|
- Op int `json:"op"`
|
|
|
- Data voiceSpeakingData `json:"d"`
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (v *VoiceConnection) Speaking(b bool) (err error) {
|
|
|
-
|
|
|
- if v.wsConn == nil {
|
|
|
- return fmt.Errorf("No VoiceConnection websocket.")
|
|
|
- }
|
|
|
-
|
|
|
- data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}}
|
|
|
- err = v.wsConn.WriteJSON(data)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("Speaking() write json error:", err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
@@ -613,63 +681,3 @@ func (v *VoiceConnection) opusReceiver(UDPConn *net.UDPConn, close <-chan struct
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-func (v *VoiceConnection) Close() {
|
|
|
-
|
|
|
- v.Lock()
|
|
|
- defer v.Unlock()
|
|
|
-
|
|
|
- v.Ready = false
|
|
|
-
|
|
|
- if v.close != nil {
|
|
|
- close(v.close)
|
|
|
- v.close = nil
|
|
|
- }
|
|
|
-
|
|
|
- if v.UDPConn != nil {
|
|
|
- err := v.UDPConn.Close()
|
|
|
- if err != nil {
|
|
|
- fmt.Println("error closing udp connection: ", err)
|
|
|
- }
|
|
|
- v.UDPConn = nil
|
|
|
- }
|
|
|
-
|
|
|
- if v.wsConn != nil {
|
|
|
- err := v.wsConn.Close()
|
|
|
- if err != nil {
|
|
|
- fmt.Println("error closing websocket connection: ", err)
|
|
|
- }
|
|
|
- v.wsConn = nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err error) {
|
|
|
-
|
|
|
- data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}}
|
|
|
- err = v.session.wsConn.WriteJSON(data)
|
|
|
-
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (v *VoiceConnection) Disconnect() (err error) {
|
|
|
-
|
|
|
-
|
|
|
- if v.sessionID != "" {
|
|
|
- data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
|
|
|
- err = v.session.wsConn.WriteJSON(data)
|
|
|
- v.sessionID = ""
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- v.Close()
|
|
|
-
|
|
|
- delete(v.session.VoiceConnections, v.GuildID)
|
|
|
-
|
|
|
- return
|
|
|
-}
|