Skip to content

Commit 78db63e

Browse files
committed
implement lyrics command
1 parent c6571c5 commit 78db63e

File tree

6 files changed

+69
-9
lines changed

6 files changed

+69
-9
lines changed

commands/lyrics.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package commands
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"time"
8+
9+
"github.com/disgoorg/disgo/discord"
10+
"github.com/disgoorg/disgo/handler"
11+
"github.com/disgoorg/json"
12+
"github.com/disgoorg/lavalyrics-plugin"
13+
)
14+
15+
func (c *Commands) Lyrics(e *handler.CommandEvent) error {
16+
if err := e.DeferCreateMessage(false); err != nil {
17+
return err
18+
}
19+
20+
node := c.Lavalink.BestNode()
21+
22+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
23+
defer cancel()
24+
lyrics, err := lavalyrics.GetLyrics(ctx, node.Rest(), node.SessionID(), *e.GuildID())
25+
if err != nil {
26+
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
27+
Content: json.Ptr(fmt.Sprintf("failed to decode track: %s", err)),
28+
})
29+
return err
30+
}
31+
32+
var content string
33+
if len(lyrics.Lines) == 0 {
34+
content = lyrics.Text
35+
} else {
36+
for _, line := range lyrics.Lines {
37+
content += fmt.Sprintf("%s\n", line.Line)
38+
}
39+
}
40+
41+
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
42+
Files: []*discord.File{
43+
discord.NewFile("lyrics.txt", "", bytes.NewReader([]byte(content))),
44+
},
45+
})
46+
return err
47+
}

commands/play.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func (c *Commands) PlayAutocomplete(e *handler.AutocompleteEvent) error {
5858
types = append(types, lavasearch.SearchType(searchType))
5959
}
6060

61-
result, err := lavasearch.LoadSearch(c.Lavalink.BestNode().Rest(), query, types)
61+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
62+
defer cancel()
63+
result, err := lavasearch.LoadSearch(ctx, c.Lavalink.BestNode().Rest(), query, types)
6264
if err != nil {
6365
if errors.Is(err, lavasearch.ErrEmptySearchResult) {
6466
return e.AutocompleteResult(nil)

commands/sponsorblock.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package commands
22

33
import (
4+
"context"
5+
"time"
6+
47
"github.com/disgoorg/disgo/discord"
58
"github.com/disgoorg/disgo/handler"
69
"github.com/disgoorg/sponsorblock-plugin"
@@ -9,7 +12,9 @@ import (
912
func (c *Commands) ShowSponsorblock(e *handler.CommandEvent) error {
1013
node := c.Lavalink.Player(*e.GuildID()).Node()
1114

12-
categories, err := sponsorblock.GetCategories(node.Rest(), node.SessionID(), *e.GuildID())
15+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
16+
defer cancel()
17+
categories, err := sponsorblock.GetCategories(ctx, node.Rest(), node.SessionID(), *e.GuildID())
1318
if err != nil {
1419
return e.CreateMessage(discord.MessageCreate{
1520
Content: "Failed to get categories: " + err.Error(),
@@ -38,7 +43,9 @@ func (c *Commands) SetSponsorblock(e *handler.CommandEvent) error {
3843
}
3944
}
4045

41-
if err := sponsorblock.SetCategories(node.Rest(), node.SessionID(), *e.GuildID(), categories); err != nil {
46+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
47+
defer cancel()
48+
if err := sponsorblock.SetCategories(ctx, node.Rest(), node.SessionID(), *e.GuildID(), categories); err != nil {
4249
return e.CreateMessage(discord.MessageCreate{
4350
Content: "Failed to set categories: " + err.Error(),
4451
Flags: discord.MessageFlagEphemeral,

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ require (
66
github.com/disgoorg/disgo v0.17.0
77
github.com/disgoorg/disgolink/v3 v3.0.0
88
github.com/disgoorg/json v1.1.0
9-
github.com/disgoorg/lavasearch-plugin v1.0.0
9+
github.com/disgoorg/lavalyrics-plugin v0.0.0-20231229185924-a5de9eaf4fd2
10+
github.com/disgoorg/lavasearch-plugin v1.0.1-0.20231229185935-76caa6d35100
1011
github.com/disgoorg/lavasrc-plugin v1.0.0
1112
github.com/disgoorg/snowflake/v2 v2.0.1
12-
github.com/disgoorg/sponsorblock-plugin v1.0.0
13+
github.com/disgoorg/sponsorblock-plugin v1.0.1-0.20231229185915-2448f4189bbc
1314
github.com/google/go-github/v52 v52.0.0
1415
github.com/lithammer/fuzzysearch v1.1.8
1516
github.com/mattn/go-colorable v0.1.13

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ github.com/disgoorg/disgolink/v3 v3.0.0 h1:rresswQhYRu/L9Ly0pm/ALM8ApTcN/ejspABq
1313
github.com/disgoorg/disgolink/v3 v3.0.0/go.mod h1:YIwjIteZcjfI7HYZWH241iRI7RjTLoN51HLDOUHVSFI=
1414
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
1515
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
16-
github.com/disgoorg/lavasearch-plugin v1.0.0 h1:Fdv6LdtXfE/F8mLHFAdAR4ksOxgHpysgj4pCU8H4nEI=
17-
github.com/disgoorg/lavasearch-plugin v1.0.0/go.mod h1:SjlXMsCmXImKOKVYpElcr8Ogbwt7L1BGYemCwt+Ld4I=
16+
github.com/disgoorg/lavalyrics-plugin v0.0.0-20231229185924-a5de9eaf4fd2 h1:aZKHFGYkTLGYvMQoTHkSWIlQW+SWtDe0hEGSfwy2B/4=
17+
github.com/disgoorg/lavalyrics-plugin v0.0.0-20231229185924-a5de9eaf4fd2/go.mod h1:xoVKtS9skWr9OF2NgPPjv9ejuYXJ5RLcfMf7hTr6MWY=
18+
github.com/disgoorg/lavasearch-plugin v1.0.1-0.20231229185935-76caa6d35100 h1:lmibrw2MBBZ+vyKHzlJ01MvLz16jiCQAJ6Qm/j3Al70=
19+
github.com/disgoorg/lavasearch-plugin v1.0.1-0.20231229185935-76caa6d35100/go.mod h1:SjlXMsCmXImKOKVYpElcr8Ogbwt7L1BGYemCwt+Ld4I=
1820
github.com/disgoorg/lavasrc-plugin v1.0.0 h1:FMQ8igQgIeP9rWX+YxjMtJCrWz8nCY0kLb2c/6n0Dpo=
1921
github.com/disgoorg/lavasrc-plugin v1.0.0/go.mod h1:6iwaxvGe07SsooplOHO7cZKnJaL2aeH95UpHHR6TIiM=
2022
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
2123
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
22-
github.com/disgoorg/sponsorblock-plugin v1.0.0 h1:chHlMQK0XBHFZiyO5c6FNMiebqieU7MpwXXQUZE4jtA=
23-
github.com/disgoorg/sponsorblock-plugin v1.0.0/go.mod h1:RXm79FX342ijRDJy8TMZPGtkshIXjgPJELbet9uuAIE=
24+
github.com/disgoorg/sponsorblock-plugin v1.0.1-0.20231229185915-2448f4189bbc h1:3R5oHK4895QxqXvyMkhIKwF67x6Twq+ILLcvlWCFNuk=
25+
github.com/disgoorg/sponsorblock-plugin v1.0.1-0.20231229185915-2448f4189bbc/go.mod h1:RXm79FX342ijRDJy8TMZPGtkshIXjgPJELbet9uuAIE=
2426
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2527
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
2628
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func main() {
6060
r.Route("/music", func(r handler.Router) {
6161
r.Command("/play", cmds.Play)
6262
r.Command("/tts", cmds.TTS)
63+
r.Command("/lyrics", cmds.Lyrics)
6364
r.Autocomplete("/play", cmds.PlayAutocomplete)
6465
r.Group(func(r handler.Router) {
6566
r.Use(cmds.RequirePlayer)

0 commit comments

Comments
 (0)