Browse Source

Separated Close into two functions, added deaf/mute to ChangeChannel

This is requried because if a guild changes it's voice region
while we're connected we need to take down the udp/ws connections
and re-connect without disconnecting from the voice channel.
Bruce Marriner 8 năm trước cách đây
mục cha
commit
706fd1c03f
1 tập tin đã thay đổi với 28 bổ sung14 xóa
  1. 28 14
      voice.go

+ 28 - 14
voice.go

@@ -615,17 +615,11 @@ func (v *VoiceConnection) opusReceiver(UDPConn *net.UDPConn, close <-chan struct
 }
 
 // Close closes the voice ws and udp connections
-func (v *VoiceConnection) Close() {
+func (v *VoiceConnection) close() {
+
 	v.Lock()
 	defer v.Unlock()
 
-	// Send a OP4 with a nil channel to disconnect
-	if v.sessionID != "" {
-		data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
-		v.session.wsConn.WriteJSON(data)
-		v.sessionID = ""
-	}
-
 	v.Ready = false
 
 	if v.close != nil {
@@ -648,14 +642,34 @@ func (v *VoiceConnection) Close() {
 		}
 		v.wsConn = nil
 	}
-
-	delete(v.session.VoiceConnections, v.GuildID)
 }
 
-// Request to change channels
-func (v *VoiceConnection) ChangeChannel(channelID string) (err error) {
-	data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, true, true}}
+// ChangeChannel sends Discord a request to change channels within a Guild
+// !!! NOTE !!! This function may be removed in favour of just using ChannelVoiceJoin
+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 err
+	return
+}
+
+// Disconnect disconnects from this voice channel and closes the websocket
+// and udp connections to Discord.
+// !!! NOTE !!! this function may be removed in favour of ChannelVoiceLeave
+func (v *VoiceConnection) Disconnect() (err error) {
+
+	// Send a OP4 with a nil channel to disconnect
+	if v.sessionID != "" {
+		data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
+		err = v.session.wsConn.WriteJSON(data)
+		v.sessionID = ""
+	}
+
+	// Close websocket and udp connections
+	v.close()
+
+	delete(v.session.VoiceConnections, v.GuildID)
+
+	return
 }