Skip to content

Skip with count and implement remove #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions commands/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ var music = discord.SlashCommandCreate{
discord.ApplicationCommandOptionSubCommand{
Name: "skip",
Description: "Skips the current track",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionInt{
Name: "count",
Description: "The number of tracks to skip",
Required: false,
},
},
},
discord.ApplicationCommandOptionSubCommand{
Name: "pause",
Expand Down Expand Up @@ -345,6 +352,7 @@ var music = discord.SlashCommandCreate{
Description: "The index of the track to remove",
Required: true,
Autocomplete: true,
MinValue: json.Ptr(0),
},
},
},
Expand Down
21 changes: 21 additions & 0 deletions commands/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package commands

import (
"fmt"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/handler"
)

func (c *Commands) Remove(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
index := data.Int("index")
ok := c.MusicQueue.Remove(*e.GuildID(), index-1, index)
if !ok {
return e.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("Failed to remove track %d from queue", index),
})
}

return e.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("Removed track %d from queue", index),
})
}
17 changes: 12 additions & 5 deletions commands/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,38 @@ package commands

import (
"context"
"fmt"
"time"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/disgolink/v3/lavalink"
)

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

var trackCount = data.Int("count")
if trackCount == 0 {
trackCount = 1
}

player := c.Lavalink.ExistingPlayer(*e.GuildID())
track, ok := c.MusicQueue.Next(*e.GuildID())
track, ok := c.MusicQueue.NextCount(*e.GuildID(), trackCount)
if !ok {
return e.CreateMessage(discord.MessageCreate{
Content: "No more tracks in queue",
Content: "Not enough tracks to skip",
Flags: discord.MessageFlagEphemeral,
})
}
if err := player.Update(ctx, lavalink.WithTrack(track)); err != nil {
return e.CreateMessage(discord.MessageCreate{
Content: "Failed to play skip track",
Content: "Failed to skip track(s)",
})
}

return e.CreateMessage(discord.MessageCreate{
Content: "⏭ Skipped track",
Content: fmt.Sprintf("⏭ Skipped %d track(s)", trackCount),
})
}
21 changes: 16 additions & 5 deletions lavalinkbot/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,26 @@ func (q *PlayerManager) Add(guildID snowflake.ID, channelID snowflake.ID, tracks
qq.tracks = append(qq.tracks, tracks...)
}

func (q *PlayerManager) Remove(guildID snowflake.ID, from int, to int) {
func (q *PlayerManager) Remove(guildID snowflake.ID, from int, to int) bool {
q.mu.Lock()
defer q.mu.Unlock()

qq, ok := q.queues[guildID]
if !ok {
return
return false
}

queueLen := len(qq.tracks)
if from >= queueLen || to >= queueLen {
return false
}

if to == 0 {
to = from + 1
}

qq.tracks = append(qq.tracks[:from], qq.tracks[to:]...)
return true
}

func (q *PlayerManager) Clear(guildID snowflake.ID) {
Expand Down Expand Up @@ -132,22 +138,27 @@ func (q *PlayerManager) SetRepeatMode(guildID snowflake.ID, mode RepeatMode) {
}

func (q *PlayerManager) Next(guildID snowflake.ID) (lavalink.Track, bool) {
return q.NextCount(guildID, 1)
}

func (q *PlayerManager) NextCount(guildID snowflake.ID, count int) (lavalink.Track, bool) {
q.mu.Lock()
defer q.mu.Unlock()

qq, ok := q.queues[guildID]
if !ok {
return lavalink.Track{}, false
}
if len(qq.tracks) == 0 {
if len(qq.tracks) < count {
return lavalink.Track{}, false
}
track := qq.tracks[0]

track := qq.tracks[count-1]
if qq.mode != RepeatModeTrack {
if qq.mode == RepeatModeQueue {
qq.tracks = append(qq.tracks, track)
}
qq.tracks = qq.tracks[1:]
qq.tracks = qq.tracks[count:]
}
return track, true
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func main() {
r.SlashCommand("/queue", cmds.Queue)
r.SlashCommand("/now-playing", cmds.NowPlaying)
// r.SlashCommand("/lyrics", cmds.Lyrics)
// r.SlashCommand("/remove", cmds.Remove)
r.SlashCommand("/remove", cmds.Remove)
// r.SlashCommand("/move", cmds.Move)
// r.SlashCommand("/swap", cmds.Swap)
// r.SlashCommand("/clear", cmds.Clear)
Expand Down