123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- package discordgo
- import (
- "encoding/binary"
- "encoding/json"
- "fmt"
- "net"
- "strings"
- "time"
- "github.com/gorilla/websocket"
- )
- type VEvent struct {
- Type string `json:"t"`
- State int `json:"s"`
- Operation int `json:"op"`
- RawData json.RawMessage `json:"d"`
- }
- type VoiceOP2 struct {
- SSRC uint32 `json:"ssrc"`
- Port int `json:"port"`
- Modes []string `json:"modes"`
- HeartbeatInterval time.Duration `json:"heartbeat_interval"`
- }
- func (s *Session) VoiceOpenWS() {
- var self User
- var err error
- self, err = s.User("@me")
-
- vg := fmt.Sprintf("wss://%s", strings.TrimSuffix(s.VEndpoint, ":80"))
- s.VwsConn, _, err = websocket.DefaultDialer.Dial(vg, nil)
- if err != nil {
- fmt.Println("VOICE cannot open websocket:", err)
- }
-
- json := map[string]interface{}{
- "op": 0,
- "d": map[string]interface{}{
- "server_id": s.VGuildID,
- "user_id": self.ID,
- "session_id": s.VSessionID,
- "token": s.VToken,
- },
- }
- err = s.VwsConn.WriteJSON(json)
- if err != nil {
- fmt.Println("VOICE ERROR sending init packet:", err)
- }
-
- go s.VoiceListen()
- }
- func (s *Session) VoiceCloseWS() {
- s.VwsConn.Close()
- }
- func (s *Session) VoiceListen() (err error) {
- for {
- messageType, message, err := s.VwsConn.ReadMessage()
- if err != nil {
- fmt.Println("Voice Listen Error:", err)
- break
- }
-
- go s.VoiceEvent(messageType, message)
- }
- return
- }
- func (s *Session) VoiceEvent(messageType int, message []byte) (err error) {
- if s.Debug {
- fmt.Println("VOICE EVENT:", messageType)
- printJSON(message)
- }
- var e VEvent
- if err := json.Unmarshal(message, &e); err != nil {
- return err
- }
- switch e.Operation {
- case 2:
- var st VoiceOP2
- if err := json.Unmarshal(e.RawData, &st); err != nil {
- fmt.Println(e.Type, err)
- printJSON(e.RawData)
- return err
- }
-
- go s.VoiceHeartbeat(st.HeartbeatInterval)
-
- s.Vop2 = st
-
- s.VoiceOpenUDP()
- return
- case 3:
-
- return
- case 4:
-
- default:
- fmt.Println("UNKNOWN VOICE OP: ", e.Operation)
- printJSON(e.RawData)
- }
- return
- }
- func (s *Session) VoiceOpenUDP() {
-
-
- udpHost := fmt.Sprintf("%s:%d", strings.TrimSuffix(s.VEndpoint, ":80"), s.Vop2.Port)
- serverAddr, err := net.ResolveUDPAddr("udp", udpHost)
- if err != nil {
- fmt.Println(err)
- }
- s.UDPConn, err = net.DialUDP("udp", nil, serverAddr)
- if err != nil {
- fmt.Println(err)
- }
-
-
- sb := make([]byte, 70)
- binary.BigEndian.PutUint32(sb, s.Vop2.SSRC)
- s.UDPConn.Write(sb)
-
-
-
-
- rb := make([]byte, 70)
- rlen, _, err := s.UDPConn.ReadFromUDP(rb)
- if rlen < 70 {
- fmt.Println("Voice RLEN should be 70 but isn't")
- }
- ip := string(rb[4:16])
- port := make([]byte, 2)
- port[0] = rb[68]
- port[1] = rb[69]
- p := binary.LittleEndian.Uint16(port)
-
-
- json := fmt.Sprintf(`{"op":1,"d":{"protocol":"udp","data":{"address":"%s","port":%d,"mode":"plain"}}}`, ip, p)
- jsonb := []byte(json)
- err = s.VwsConn.WriteMessage(websocket.TextMessage, jsonb)
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- s.UDPReady = true
-
-
- }
- func (s *Session) VoiceCloseUDP() {
- s.UDPConn.Close()
- }
- func (s *Session) VoiceSpeaking() {
- if s.VwsConn == nil {
-
- fmt.Println("No Voice websocket.")
- return
- }
- jsonb := []byte(`{"op":5,"d":{"speaking":true,"delay":0}}`)
- err := s.VwsConn.WriteMessage(websocket.TextMessage, jsonb)
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- }
- func (s *Session) VoiceListenUDP() {
-
-
-
- for {
- b := make([]byte, 1024)
- rlen, _, err := s.UDPConn.ReadFromUDP(b)
- if err != nil {
- fmt.Println("Error reading from UDP:", err)
-
- }
- if rlen < 1 {
- fmt.Println("Empty UDP packet received")
- continue
-
- }
- fmt.Println("READ FROM UDP: ", b)
- }
- }
- func (s *Session) VoiceUDPKeepalive(i time.Duration) {
-
-
-
-
-
-
- ticker := time.NewTicker(i * time.Millisecond)
- for range ticker.C {
- sb := make([]byte, 8)
- sb[0] = 0x80
- sb[1] = 0xc9
- sb[2] = 0x00
- sb[3] = 0x01
- ssrcBE := make([]byte, 4)
- binary.BigEndian.PutUint32(ssrcBE, s.Vop2.SSRC)
- sb[4] = ssrcBE[0]
- sb[5] = ssrcBE[1]
- sb[6] = ssrcBE[2]
- sb[7] = ssrcBE[3]
- s.UDPConn.Write(ssrcBE)
- }
- }
- func (s *Session) VoiceHeartbeat(i time.Duration) {
- ticker := time.NewTicker(i * time.Millisecond)
- for {
- timestamp := int(time.Now().Unix())
- err := s.VwsConn.WriteJSON(map[string]int{
- "op": 3,
- "d": timestamp,
- })
- if err != nil {
- s.VoiceReady = false
- fmt.Println(err)
- return
- }
- s.VoiceReady = true
- <-ticker.C
- }
- }
|