Skip to content

Commit 23b41b5

Browse files
committed
feat: implement remove command
1 parent 2cf68e1 commit 23b41b5

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

commands/music.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ var music = discord.SlashCommandCreate{
352352
Description: "The index of the track to remove",
353353
Required: true,
354354
Autocomplete: true,
355+
MinValue: json.Ptr(0),
355356
},
356357
},
357358
},

commands/remove.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/disgoorg/disgo/discord"
6+
"github.com/disgoorg/disgo/handler"
7+
)
8+
9+
func (c *Commands) Remove(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
10+
index := data.Int("index")
11+
ok := c.MusicQueue.Remove(*e.GuildID(), index-1, index)
12+
if !ok {
13+
return e.CreateMessage(discord.MessageCreate{
14+
Content: fmt.Sprintf("Failed to remove track %d from queue", index),
15+
})
16+
}
17+
18+
return e.CreateMessage(discord.MessageCreate{
19+
Content: fmt.Sprintf("Removed track %d from queue", index),
20+
})
21+
}

lavalinkbot/queue.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,26 @@ func (q *PlayerManager) Add(guildID snowflake.ID, channelID snowflake.ID, tracks
7676
qq.tracks = append(qq.tracks, tracks...)
7777
}
7878

79-
func (q *PlayerManager) Remove(guildID snowflake.ID, from int, to int) {
79+
func (q *PlayerManager) Remove(guildID snowflake.ID, from int, to int) bool {
8080
q.mu.Lock()
8181
defer q.mu.Unlock()
8282

8383
qq, ok := q.queues[guildID]
8484
if !ok {
85-
return
85+
return false
86+
}
87+
88+
queueLen := len(qq.tracks)
89+
if from >= queueLen || to >= queueLen {
90+
return false
8691
}
8792

8893
if to == 0 {
8994
to = from + 1
9095
}
9196

9297
qq.tracks = append(qq.tracks[:from], qq.tracks[to:]...)
98+
return true
9399
}
94100

95101
func (q *PlayerManager) Clear(guildID snowflake.ID) {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func main() {
9797
r.SlashCommand("/queue", cmds.Queue)
9898
r.SlashCommand("/now-playing", cmds.NowPlaying)
9999
// r.SlashCommand("/lyrics", cmds.Lyrics)
100-
// r.SlashCommand("/remove", cmds.Remove)
100+
r.SlashCommand("/remove", cmds.Remove)
101101
// r.SlashCommand("/move", cmds.Move)
102102
// r.SlashCommand("/swap", cmds.Swap)
103103
// r.SlashCommand("/clear", cmds.Clear)

0 commit comments

Comments
 (0)