Skip to content

Commit

Permalink
implement lyrics command (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 authored May 6, 2024
1 parent 6b554e6 commit 3c07fa5
Show file tree
Hide file tree
Showing 23 changed files with 226 additions and 132 deletions.
16 changes: 8 additions & 8 deletions commands/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ var decode = discord.SlashCommandCreate{
},
}

func (c *Commands) Decode(e *handler.CommandEvent) error {
track := e.SlashCommandInteractionData().String("track")
lavalink := e.SlashCommandInteractionData().Bool("lavalink")
func (c *Commands) Decode(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
track := data.String("track")
lavalink := data.Bool("lavalink")

if !lavalink {
var content string
Expand All @@ -43,8 +43,8 @@ func (c *Commands) Decode(e *handler.CommandEvent) error {
content += fmt.Sprintf("track was encoded with version: `%d`\n", version)
}
if decoded != nil {
data, _ := json.MarshalIndent(decoded, "", " ")
content += fmt.Sprintf("```json\n%s\n```", data)
decodedData, _ := json.MarshalIndent(decoded, "", " ")
content += fmt.Sprintf("```json\n%s\n```", decodedData)
}

return e.CreateMessage(discord.MessageCreate{
Expand All @@ -56,7 +56,7 @@ func (c *Commands) Decode(e *handler.CommandEvent) error {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
decoded, err := c.Lavalink.BestNode().Rest().DecodeTrack(ctx, track)
if err != nil {
Expand All @@ -66,7 +66,7 @@ func (c *Commands) Decode(e *handler.CommandEvent) error {
return err
}

data, err := json.MarshalIndent(decoded, "", " ")
decodedData, err := json.MarshalIndent(decoded, "", " ")
if err != nil {
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr(fmt.Sprintf("failed to decode track: %s", err)),
Expand All @@ -75,7 +75,7 @@ func (c *Commands) Decode(e *handler.CommandEvent) error {
}

_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr(fmt.Sprintf("```json\n%s\n```", data)),
Content: json.Ptr(fmt.Sprintf("```json\n%s\n```", decodedData)),
})
return err
}
4 changes: 2 additions & 2 deletions commands/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/disgoorg/disgo/handler"
)

func (c *Commands) Disconnect(e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
func (c *Commands) Disconnect(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()

if err := c.Client.UpdateVoiceState(ctx, *e.GuildID(), nil, false, false); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions commands/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ var effects = map[EffectType]lavalink.Filters{
},
}

func (c *Commands) Effects(e *handler.CommandEvent) error {
effectType := EffectType(e.SlashCommandInteractionData().String("effect"))
func (c *Commands) Effects(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
effectType := EffectType(data.String("effect"))
if err := c.Lavalink.ExistingPlayer(*e.GuildID()).Update(context.Background(), lavalink.WithFilters(effects[effectType])); err != nil {
return e.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("failed to appply effect: `%s`", err),
Expand Down
4 changes: 2 additions & 2 deletions commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var info = discord.SlashCommandCreate{
},
}

func (c *Commands) InfoBot(e *handler.CommandEvent) error {
func (c *Commands) InfoBot(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
var fields []discord.EmbedField
c.Lavalink.ForNodes(func(node disgolink.Node) {
version, err := node.Version(context.TODO())
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *Commands) InfoBot(e *handler.CommandEvent) error {
})
}

func (c *Commands) InfoLavalink(e *handler.CommandEvent) error {
func (c *Commands) InfoLavalink(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
nodeInfos := map[string]lavalink.Info{}
c.Lavalink.ForNodes(func(node disgolink.Node) {
nodeInfo, err := node.Info(context.TODO())
Expand Down
2 changes: 1 addition & 1 deletion commands/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Commands) LatestAutocomplete(e *handler.AutocompleteEvent) error {
return e.AutocompleteResult(choices)
}

func (c *Commands) Latest(e *handler.CommandEvent) error {
func (c *Commands) Latest(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
if err := e.DeferCreateMessage(false); err != nil {
return err
}
Expand Down
90 changes: 90 additions & 0 deletions commands/lyrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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"
"github.com/lavalink-devs/lavalink-bot/internal/res"
)

func (c *Commands) Lyrics(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
skipTrackSource := data.Bool("skip-track-source")

var (
track string
lyrics *lavalyrics.Lyrics
err error
)

ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
if encodedTrack, ok := data.OptString("track"); ok {
track = fmt.Sprintf("`%s`", encodedTrack)

if err = e.DeferCreateMessage(false); err != nil {
return err
}

lyrics, err = lavalyrics.GetLyrics(ctx, c.Lavalink.BestNode().Rest(), encodedTrack, skipTrackSource)
} else {
player := c.Lavalink.ExistingPlayer(*e.GuildID())
if player == nil {
return e.CreateMessage(discord.MessageCreate{
Content: "No player found",
Flags: discord.MessageFlagEphemeral,
})
}

playingTrack := player.Track()
if playingTrack == nil {
return e.CreateMessage(discord.MessageCreate{
Content: "no track playing",
Flags: discord.MessageFlagEphemeral,
})
}

track = res.FormatTrack(*playingTrack, 0)

if err = e.DeferCreateMessage(false); err != nil {
return err
}

lyrics, err = lavalyrics.GetCurrentTrackLyrics(ctx, player.Node().Rest(), player.Node().SessionID(), *e.GuildID(), skipTrackSource)
}
if err != nil {
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr(fmt.Sprintf("failed to decode track: %s", err)),
})
return err
}

if lyrics == nil {
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr("no lyrics found"),
})
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{
Content: json.Ptr(fmt.Sprintf("Loaded lyrics for %s from `%s`(`%s`)", track, lyrics.SourceName, lyrics.Provider)),
Files: []*discord.File{
discord.NewFile("lyrics.txt", "", bytes.NewReader([]byte(content))),
},
})
return err
}
18 changes: 13 additions & 5 deletions commands/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/json"
Expand Down Expand Up @@ -305,13 +304,22 @@ var music = discord.SlashCommandCreate{
discord.ApplicationCommandOptionBool{
Name: "raw",
Description: "Whether to include the raw track & info",
Required: false,
},
},
},
discord.ApplicationCommandOptionSubCommand{
Name: "lyrics",
Description: "Shows the lyrics of the current track",
Description: "Shows the lyrics of the current or a given track",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionBool{
Name: "track",
Description: "Whether to include the raw lyrics",
},
discord.ApplicationCommandOptionBool{
Name: "skip-track-source",
Description: "Whether to skip the track source to resolve the lyrics",
},
},
},
discord.ApplicationCommandOptionSubCommand{
Name: "remove",
Expand Down Expand Up @@ -459,10 +467,10 @@ var music = discord.SlashCommandCreate{
}

func (c *Commands) RequirePlayer(next handler.Handler) handler.Handler {
return func(e *events.InteractionCreate) error {
return func(e *handler.InteractionEvent) error {
if e.Type() == discord.InteractionTypeApplicationCommand {
if player := c.Lavalink.ExistingPlayer(*e.GuildID()); player == nil {
return e.Respond(discord.InteractionResponseTypeCreateMessage, discord.MessageCreate{
return e.CreateMessage(discord.MessageCreate{
Content: "No player found",
Flags: discord.MessageFlagEphemeral,
})
Expand Down
8 changes: 4 additions & 4 deletions commands/now_playing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/lavalink-devs/lavalink-bot/internal/res"
)

func (c *Commands) NowPlaying(e *handler.CommandEvent) error {
func (c *Commands) NowPlaying(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
player := c.Lavalink.Player(*e.GuildID())
track := player.Track()
if track == nil {
Expand All @@ -30,8 +30,8 @@ func (c *Commands) NowPlaying(e *handler.CommandEvent) error {
}

var files []*discord.File
if e.SlashCommandInteractionData().Bool("raw") {
data, err := json.MarshalIndent(track, "", " ")
if data.Bool("raw") {
decodedData, err := json.MarshalIndent(track, "", " ")
if err != nil {
return e.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("Failed to marshal track: %s", err),
Expand All @@ -40,7 +40,7 @@ func (c *Commands) NowPlaying(e *handler.CommandEvent) error {
}
files = append(files, &discord.File{
Name: "track.json",
Reader: bytes.NewReader(data),
Reader: bytes.NewReader(decodedData),
})
}

Expand Down
4 changes: 2 additions & 2 deletions commands/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/disgoorg/disgolink/v3/lavalink"
)

func (c *Commands) Pause(e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
func (c *Commands) Pause(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
player := c.Lavalink.ExistingPlayer(*e.GuildID())

Expand Down
12 changes: 6 additions & 6 deletions 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(e.Ctx, 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 Expand Up @@ -162,7 +164,7 @@ func (c Choices) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}

func (c *Commands) Play(e *handler.CommandEvent) error {
func (c *Commands) Play(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
voiceState, ok := c.Client.Caches().VoiceState(*e.GuildID(), e.User().ID)
if !ok {
return e.CreateMessage(discord.MessageCreate{
Expand All @@ -171,8 +173,6 @@ func (c *Commands) Play(e *handler.CommandEvent) error {
})
}

data := e.SlashCommandInteractionData()

query := data.String("query")

if !data.Bool("raw") {
Expand All @@ -189,7 +189,7 @@ func (c *Commands) Play(e *handler.CommandEvent) error {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
result, err := c.Lavalink.BestNode().LoadTracks(ctx, query)
if err != nil {
Expand Down Expand Up @@ -259,7 +259,7 @@ func (c *Commands) Play(e *handler.CommandEvent) error {
track, tracks = tracks[0], tracks[1:]
}

playCtx, playCancel := context.WithTimeout(context.Background(), 10*time.Second)
playCtx, playCancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer playCancel()
if err = player.Update(playCtx, lavalink.WithTrack(track)); err != nil {
_, err = e.CreateFollowupMessage(discord.MessageCreate{
Expand Down
2 changes: 1 addition & 1 deletion commands/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/lavalink-devs/lavalink-bot/internal/res"
)

func (c *Commands) Queue(e *handler.CommandEvent) error {
func (c *Commands) Queue(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
_, tracks := c.MusicQueue.Get(*e.GuildID())
if len(tracks) == 0 {
return e.CreateMessage(discord.MessageCreate{
Expand Down
4 changes: 2 additions & 2 deletions commands/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/disgoorg/disgolink/v3/lavalink"
)

func (c *Commands) Resume(e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
func (c *Commands) Resume(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
player := c.Lavalink.ExistingPlayer(*e.GuildID())

Expand Down
5 changes: 2 additions & 3 deletions commands/seek.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (
"github.com/lavalink-devs/lavalink-bot/internal/res"
)

func (c *Commands) Seek(e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
func (c *Commands) Seek(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
player := c.Lavalink.ExistingPlayer(*e.GuildID())

data := e.SlashCommandInteractionData()
position := data.Int("position")
duration, ok := data.OptInt("unit")
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion commands/shuffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/disgoorg/disgo/handler"
)

func (c *Commands) Shuffle(e *handler.CommandEvent) error {
func (c *Commands) Shuffle(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
ok := c.MusicQueue.Shuffle(*e.GuildID())
if !ok {
return e.CreateMessage(discord.MessageCreate{
Expand Down
4 changes: 2 additions & 2 deletions commands/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/disgoorg/disgolink/v3/lavalink"
)

func (c *Commands) Skip(e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
func (c *Commands) Skip(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
player := c.Lavalink.ExistingPlayer(*e.GuildID())
track, ok := c.MusicQueue.Next(*e.GuildID())
Expand Down
Loading

0 comments on commit 3c07fa5

Please sign in to comment.