Skip to content

Commit

Permalink
implement lyrics command
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Dec 29, 2023
1 parent c6571c5 commit 78db63e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
47 changes: 47 additions & 0 deletions commands/lyrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package commands

import (
"bytes"
"context"
"fmt"
"time"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/json"
"github.com/disgoorg/lavalyrics-plugin"
)

func (c *Commands) Lyrics(e *handler.CommandEvent) error {
if err := e.DeferCreateMessage(false); err != nil {
return err
}

node := c.Lavalink.BestNode()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
lyrics, err := lavalyrics.GetLyrics(ctx, node.Rest(), node.SessionID(), *e.GuildID())
if err != nil {
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr(fmt.Sprintf("failed to decode track: %s", err)),
})
return err
}

var content string
if len(lyrics.Lines) == 0 {
content = lyrics.Text
} else {
for _, line := range lyrics.Lines {
content += fmt.Sprintf("%s\n", line.Line)
}
}

_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Files: []*discord.File{
discord.NewFile("lyrics.txt", "", bytes.NewReader([]byte(content))),
},
})
return err
}
4 changes: 3 additions & 1 deletion commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func (c *Commands) PlayAutocomplete(e *handler.AutocompleteEvent) error {
types = append(types, lavasearch.SearchType(searchType))
}

result, err := lavasearch.LoadSearch(c.Lavalink.BestNode().Rest(), query, types)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := lavasearch.LoadSearch(ctx, c.Lavalink.BestNode().Rest(), query, types)
if err != nil {
if errors.Is(err, lavasearch.ErrEmptySearchResult) {
return e.AutocompleteResult(nil)
Expand Down
11 changes: 9 additions & 2 deletions commands/sponsorblock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package commands

import (
"context"
"time"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/sponsorblock-plugin"
Expand All @@ -9,7 +12,9 @@ import (
func (c *Commands) ShowSponsorblock(e *handler.CommandEvent) error {
node := c.Lavalink.Player(*e.GuildID()).Node()

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

if err := sponsorblock.SetCategories(node.Rest(), node.SessionID(), *e.GuildID(), categories); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := sponsorblock.SetCategories(ctx, node.Rest(), node.SessionID(), *e.GuildID(), categories); err != nil {
return e.CreateMessage(discord.MessageCreate{
Content: "Failed to set categories: " + err.Error(),
Flags: discord.MessageFlagEphemeral,
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ require (
github.com/disgoorg/disgo v0.17.0
github.com/disgoorg/disgolink/v3 v3.0.0
github.com/disgoorg/json v1.1.0
github.com/disgoorg/lavasearch-plugin v1.0.0
github.com/disgoorg/lavalyrics-plugin v0.0.0-20231229185924-a5de9eaf4fd2
github.com/disgoorg/lavasearch-plugin v1.0.1-0.20231229185935-76caa6d35100
github.com/disgoorg/lavasrc-plugin v1.0.0
github.com/disgoorg/snowflake/v2 v2.0.1
github.com/disgoorg/sponsorblock-plugin v1.0.0
github.com/disgoorg/sponsorblock-plugin v1.0.1-0.20231229185915-2448f4189bbc
github.com/google/go-github/v52 v52.0.0
github.com/lithammer/fuzzysearch v1.1.8
github.com/mattn/go-colorable v0.1.13
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ github.com/disgoorg/disgolink/v3 v3.0.0 h1:rresswQhYRu/L9Ly0pm/ALM8ApTcN/ejspABq
github.com/disgoorg/disgolink/v3 v3.0.0/go.mod h1:YIwjIteZcjfI7HYZWH241iRI7RjTLoN51HLDOUHVSFI=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/lavasearch-plugin v1.0.0 h1:Fdv6LdtXfE/F8mLHFAdAR4ksOxgHpysgj4pCU8H4nEI=
github.com/disgoorg/lavasearch-plugin v1.0.0/go.mod h1:SjlXMsCmXImKOKVYpElcr8Ogbwt7L1BGYemCwt+Ld4I=
github.com/disgoorg/lavalyrics-plugin v0.0.0-20231229185924-a5de9eaf4fd2 h1:aZKHFGYkTLGYvMQoTHkSWIlQW+SWtDe0hEGSfwy2B/4=
github.com/disgoorg/lavalyrics-plugin v0.0.0-20231229185924-a5de9eaf4fd2/go.mod h1:xoVKtS9skWr9OF2NgPPjv9ejuYXJ5RLcfMf7hTr6MWY=
github.com/disgoorg/lavasearch-plugin v1.0.1-0.20231229185935-76caa6d35100 h1:lmibrw2MBBZ+vyKHzlJ01MvLz16jiCQAJ6Qm/j3Al70=
github.com/disgoorg/lavasearch-plugin v1.0.1-0.20231229185935-76caa6d35100/go.mod h1:SjlXMsCmXImKOKVYpElcr8Ogbwt7L1BGYemCwt+Ld4I=
github.com/disgoorg/lavasrc-plugin v1.0.0 h1:FMQ8igQgIeP9rWX+YxjMtJCrWz8nCY0kLb2c/6n0Dpo=
github.com/disgoorg/lavasrc-plugin v1.0.0/go.mod h1:6iwaxvGe07SsooplOHO7cZKnJaL2aeH95UpHHR6TIiM=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/disgoorg/sponsorblock-plugin v1.0.0 h1:chHlMQK0XBHFZiyO5c6FNMiebqieU7MpwXXQUZE4jtA=
github.com/disgoorg/sponsorblock-plugin v1.0.0/go.mod h1:RXm79FX342ijRDJy8TMZPGtkshIXjgPJELbet9uuAIE=
github.com/disgoorg/sponsorblock-plugin v1.0.1-0.20231229185915-2448f4189bbc h1:3R5oHK4895QxqXvyMkhIKwF67x6Twq+ILLcvlWCFNuk=
github.com/disgoorg/sponsorblock-plugin v1.0.1-0.20231229185915-2448f4189bbc/go.mod h1:RXm79FX342ijRDJy8TMZPGtkshIXjgPJELbet9uuAIE=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
r.Route("/music", func(r handler.Router) {
r.Command("/play", cmds.Play)
r.Command("/tts", cmds.TTS)
r.Command("/lyrics", cmds.Lyrics)
r.Autocomplete("/play", cmds.PlayAutocomplete)
r.Group(func(r handler.Router) {
r.Use(cmds.RequirePlayer)
Expand Down

0 comments on commit 78db63e

Please sign in to comment.