-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlavalyrics.go
102 lines (86 loc) · 2.64 KB
/
lavalyrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package lavalyrics
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/disgoorg/disgolink/v3/disgolink"
"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/snowflake/v2"
)
type Lyrics struct {
SourceName string `json:"sourceName"`
Provider string `json:"provider"`
Text string `json:"text"`
Lines []Line `json:"lines"`
Plugin lavalink.RawData `json:"plugin"`
}
type Line struct {
Timestamp lavalink.Duration `json:"timestamp"`
Duration lavalink.Duration `json:"duration"`
Line string `json:"line"`
Plugin lavalink.RawData `json:"plugin"`
}
// GetCurrentTrackLyrics returns the lyrics of the current track being played in the guild.
// If the current track has no lyrics, it will return nil.
func GetCurrentTrackLyrics(ctx context.Context, client disgolink.RestClient, sessionID string, guildID snowflake.ID, skipTrackSource bool) (*Lyrics, error) {
rq, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("/v4/sessions/%s/players/%s/track/lyrics", sessionID, guildID), nil)
if err != nil {
return nil, err
}
q := rq.URL.Query()
q.Add("skipTrackSource", fmt.Sprint(skipTrackSource))
rq.URL.RawQuery = q.Encode()
rs, err := client.Do(rq)
if err != nil {
return nil, err
}
defer rs.Body.Close()
if rs.StatusCode < 200 || rs.StatusCode >= 300 {
var lavalinkError lavalink.Error
if err = json.NewDecoder(rs.Body).Decode(&lavalinkError); err != nil {
return nil, err
}
return nil, lavalinkError
}
if rs.StatusCode == http.StatusNoContent {
return nil, nil
}
var l Lyrics
if err = json.NewDecoder(rs.Body).Decode(&l); err != nil {
return nil, err
}
return &l, nil
}
// GetLyrics returns the lyrics of the provided track.
// If the track has no lyrics, it will return nil.
func GetLyrics(ctx context.Context, client disgolink.RestClient, track string, skipTrackSource bool) (*Lyrics, error) {
rq, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("/v4/lyrics"), nil)
if err != nil {
return nil, err
}
q := rq.URL.Query()
q.Add("track", track)
q.Add("skipTrackSource", fmt.Sprint(skipTrackSource))
rq.URL.RawQuery = q.Encode()
rs, err := client.Do(rq)
if err != nil {
return nil, err
}
defer rs.Body.Close()
if rs.StatusCode < 200 || rs.StatusCode >= 300 {
var lavalinkError lavalink.Error
if err = json.NewDecoder(rs.Body).Decode(&lavalinkError); err != nil {
return nil, err
}
return nil, lavalinkError
}
if rs.StatusCode == http.StatusNoContent {
return nil, nil
}
var l Lyrics
if err = json.NewDecoder(rs.Body).Decode(&l); err != nil {
return nil, err
}
return &l, nil
}