Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
ba67bde994 | |||
fbab76d269 | |||
ffe0ef235d |
4
.gitignore
vendored
4
.gitignore
vendored
@ -23,3 +23,7 @@
|
|||||||
# End of https://www.gitignore.io/api/go
|
# End of https://www.gitignore.io/api/go
|
||||||
|
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"github.com/valyala/fastjson"
|
"github.com/valyala/fastjson"
|
||||||
"io"
|
"io"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const trackInfoVersioned int32 = 1
|
const trackInfoVersioned int32 = 1
|
||||||
@ -109,7 +110,7 @@ func Decode(r io.Reader) (*TrackInfo, error) {
|
|||||||
Author: author,
|
Author: author,
|
||||||
URI: url,
|
URI: url,
|
||||||
Stream: stream == 1,
|
Stream: stream == 1,
|
||||||
Length: int(length),
|
Length: time.Duration(length) * time.Millisecond,
|
||||||
}
|
}
|
||||||
|
|
||||||
return track, nil
|
return track, nil
|
||||||
|
43
model.go
43
model.go
@ -1,9 +1,11 @@
|
|||||||
package gavalink
|
package gavalink
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -27,7 +29,7 @@ type Tracks struct {
|
|||||||
// NoMatches, or LoadFailed
|
// NoMatches, or LoadFailed
|
||||||
Type string `json:"loadType"`
|
Type string `json:"loadType"`
|
||||||
PlaylistInfo *PlaylistInfo `json:"playlistInfo"`
|
PlaylistInfo *PlaylistInfo `json:"playlistInfo"`
|
||||||
Tracks []Track `json:"tracks"`
|
Tracks []*Track `json:"tracks"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlaylistInfo contains information about a loaded playlist
|
// PlaylistInfo contains information about a loaded playlist
|
||||||
@ -43,7 +45,7 @@ type PlaylistInfo struct {
|
|||||||
type Track struct {
|
type Track struct {
|
||||||
// Data contains the base64 encoded Lavaplayer track
|
// Data contains the base64 encoded Lavaplayer track
|
||||||
Data string `json:"track"`
|
Data string `json:"track"`
|
||||||
Info TrackInfo `json:"info"`
|
Info *TrackInfo `json:"info"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrackInfo contains more data about a loaded track
|
// TrackInfo contains more data about a loaded track
|
||||||
@ -54,10 +56,36 @@ type TrackInfo struct {
|
|||||||
URI string `json:"uri"`
|
URI string `json:"uri"`
|
||||||
Seekable bool `json:"isSeekable"`
|
Seekable bool `json:"isSeekable"`
|
||||||
Stream bool `json:"isStream"`
|
Stream bool `json:"isStream"`
|
||||||
Length int `json:"length"`
|
Length time.Duration `json:"length"`
|
||||||
Position int `json:"position"`
|
Position int `json:"position"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) MarshalJSON() ([]byte, error) {
|
||||||
|
type Alias TrackInfo
|
||||||
|
return json.Marshal(&struct {
|
||||||
|
Length int64 `json:"length"`
|
||||||
|
*Alias
|
||||||
|
}{
|
||||||
|
Length: int64(t.Length / time.Millisecond),
|
||||||
|
Alias: (*Alias)(t),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) UnmarshalJSON(data []byte) error {
|
||||||
|
type Alias TrackInfo
|
||||||
|
aux := &struct {
|
||||||
|
Length int64 `json:"length"`
|
||||||
|
*Alias
|
||||||
|
}{
|
||||||
|
Alias: (*Alias)(t),
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(data, &aux); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
t.Length = time.Duration(aux.Length) * time.Millisecond
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
opVoiceUpdate = "voiceUpdate"
|
opVoiceUpdate = "voiceUpdate"
|
||||||
opVoiceProcessed = "voiceProcessed"
|
opVoiceProcessed = "voiceProcessed"
|
||||||
@ -90,7 +118,8 @@ type VoiceServerUpdate struct {
|
|||||||
type VoiceProcessingData struct {
|
type VoiceProcessingData struct {
|
||||||
io.ReadCloser
|
io.ReadCloser
|
||||||
|
|
||||||
Client *http.Client
|
node *Node
|
||||||
|
|
||||||
UserID string
|
UserID string
|
||||||
URL string
|
URL string
|
||||||
File string
|
File string
|
||||||
@ -99,7 +128,11 @@ type VoiceProcessingData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *VoiceProcessingData) open() error {
|
func (v *VoiceProcessingData) open() error {
|
||||||
res, err := v.Client.Get(v.URL)
|
req, err := http.NewRequest(http.MethodGet, v.URL, nil)
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", v.node.config.Password)
|
||||||
|
|
||||||
|
res, err := v.node.client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
33
model_test.go
Normal file
33
model_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package gavalink
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTrackInfo_JSON(t *testing.T) {
|
||||||
|
i := &TrackInfo{
|
||||||
|
Length: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(i)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log(string(b))
|
||||||
|
|
||||||
|
deserialize := &TrackInfo{}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(b, &deserialize); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("Deserialized length:", deserialize.Length)
|
||||||
|
|
||||||
|
if deserialize.Length != time.Second*10 {
|
||||||
|
t.Fatal("Expected deserialized time to be 10 seconds!")
|
||||||
|
}
|
||||||
|
}
|
8
node.go
8
node.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/valyala/fastjson"
|
"github.com/valyala/fastjson"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -208,7 +209,7 @@ func (node *Node) onEvent(v *fastjson.Value, msg []byte) error {
|
|||||||
track := jsonStringValue(v, "track")
|
track := jsonStringValue(v, "track")
|
||||||
|
|
||||||
data := &VoiceProcessingData{
|
data := &VoiceProcessingData{
|
||||||
Client: node.client,
|
node: node,
|
||||||
UserID: jsonStringValue(v, "userId"),
|
UserID: jsonStringValue(v, "userId"),
|
||||||
URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track),
|
URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track),
|
||||||
File: track,
|
File: track,
|
||||||
@ -261,9 +262,10 @@ func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServ
|
|||||||
//
|
//
|
||||||
// See the Lavaplayer Source Code for all valid options.
|
// See the Lavaplayer Source Code for all valid options.
|
||||||
func (node *Node) LoadTracks(query string) (*Tracks, error) {
|
func (node *Node) LoadTracks(query string) (*Tracks, error) {
|
||||||
url := fmt.Sprintf("%s/loadtracks?identifier=%s", node.config.REST, query)
|
v := url.Values{}
|
||||||
|
v.Set("identifier", query)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/loadtracks?%s", node.config.REST, v.Encode()), nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Reference in New Issue
Block a user