Skip to content

Commit

Permalink
added play track command
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jun 16, 2024
1 parent 3fedb71 commit ac71a27
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions commands/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ var music = discord.SlashCommandCreate{
},
},
},
discord.ApplicationCommandOptionSubCommand{
Name: "play-track",
Description: "Plays a given encoded track",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "track",
Description: "The encoded track to play",
Required: true,
},
},
},
discord.ApplicationCommandOptionSubCommand{
Name: "tts",
Description: "Crafts a text-to-speech link to play",
Expand Down
59 changes: 59 additions & 0 deletions commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,62 @@ func (c *Commands) Play(data discord.SlashCommandInteractionData, e *handler.Com
}
return nil
}

func (c *Commands) PlayTrack(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{
Content: "You need to be in a voice channel to use this command.",
Flags: discord.MessageFlagEphemeral,
})
}

encodedTrack := data.String("track")

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

ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second)
defer cancel()
track, err := c.Lavalink.BestNode().DecodeTrack(ctx, encodedTrack)
if err != nil {
_, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr(fmt.Sprintf("Failed to decode track: %s", err)),
})
return err
}

userDataRaw, _ := json.Marshal(UserData{
Requester: e.User().ID,
})
track.UserData = userDataRaw

if _, err = e.UpdateInteractionResponse(discord.MessageUpdate{
Content: json.Ptr(fmt.Sprintf("Loaded track **%s**", res.FormatTrack(*track, 0))),
}); err != nil {
return err
}

if err = c.Client.UpdateVoiceState(context.Background(), *e.GuildID(), voiceState.ChannelID, false, false); err != nil {
_, err = e.CreateFollowupMessage(discord.MessageCreate{
Content: fmt.Sprintf("Failed to join voice channel: %s", err),
})
return err
}

player := c.Lavalink.Player(*e.GuildID())
if player.Track() == nil {
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{
Content: fmt.Sprintf("Failed to play track: %s", err),
})
return err
}
} else {
c.MusicQueue.Add(*e.GuildID(), e.Channel().ID(), *track)
}
return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {

r.Route("/music", func(r handler.Router) {
r.SlashCommand("/play", cmds.Play)
r.SlashCommand("/play-track", cmds.PlayTrack)
r.SlashCommand("/tts", cmds.TTS)
r.Autocomplete("/play", cmds.PlayAutocomplete)
r.SlashCommand("/lyrics", cmds.Lyrics)
Expand Down

0 comments on commit ac71a27

Please sign in to comment.