queue

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package queue is a player's track list: what plays now, what played before and what comes next, with optional persistence through a Store.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change string

Change says what happened to a queue.

const (
	Added    Change = "added"
	Removed  Change = "removed"
	Shuffled Change = "shuffled"
	// Current is the playing track changing, queue untouched.
	Current Change = "current"
)

type Config

type Config struct {
	// Store persists the queue. Without one nothing is written.
	Store Store
	// Logger reports store failures. Defaults to [slog.Default].
	Logger *slog.Logger
	// OnChange runs after every change, outside the lock.
	OnChange func(ctx context.Context, guildID string, change Change, tracks []lavalink.Track)
	// HistoryLimit is how many played tracks to remember. Defaults to 25.
	HistoryLimit int
}

Config configures a Queue. Every field is optional.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue is a player's track list. Safe for concurrent use.

A change writes to Config.Store and calls Config.OnChange before returning, both outside the lock, which is what the context bounds.

func New

func New(guildID string, cfg Config) *Queue

New builds an empty queue. Queue.Load fills it from the store.

func (*Queue) Add

func (q *Queue) Add(ctx context.Context, tracks ...lavalink.Track)

Add appends tracks to the end of the queue.

func (*Queue) AddNext

func (q *Queue) AddNext(ctx context.Context, tracks ...lavalink.Track)

AddNext puts tracks at the front, so they play before everything queued.

func (*Queue) Advance

func (q *Queue) Advance(ctx context.Context) (lavalink.Track, bool)

Advance retires the playing track and makes the next one current. Nothing queued reports false and clears Current.

func (*Queue) Back

func (q *Queue) Back(ctx context.Context) (lavalink.Track, bool)

Back replays the last played track, pushing the playing one to the front.

func (*Queue) Clear

func (q *Queue) Clear(ctx context.Context)

Clear drops every waiting track, leaving the playing one alone.

func (*Queue) Current

func (q *Queue) Current() *lavalink.Track

Current returns the playing track, or nil when nothing plays.

func (*Queue) Delete

func (q *Queue) Delete(ctx context.Context) error

Delete drops the stored copy, leaving the one in memory alone.

func (*Queue) Duration

func (q *Queue) Duration() lavalink.Duration

Duration is the total play time of the waiting tracks.

func (*Queue) Filter

func (q *Queue) Filter(ctx context.Context, keep func(lavalink.Track) bool)

Filter keeps only the waiting tracks keep returns true for.

func (*Queue) Find

func (q *Queue) Find(match func(lavalink.Track) bool) int

Find is the index of the first waiting track match returns true for, or -1.

func (*Queue) Insert

func (q *Queue) Insert(ctx context.Context, i int, tracks ...lavalink.Track)

Insert puts tracks at index i, clamped to the queue bounds.

func (*Queue) Len

func (q *Queue) Len() int

Len is how many tracks are waiting.

func (*Queue) Load

func (q *Queue) Load(ctx context.Context) error

Load replaces the queue from the store. An unseen guild is not an error.

func (*Queue) Move

func (q *Queue) Move(ctx context.Context, from, to int) error

Move moves the track at from to index to.

func (*Queue) OnChange added in v0.1.2

func (q *Queue) OnChange() func(ctx context.Context, guildID string, change Change, tracks []lavalink.Track)

OnChange returns the queue-change callback, if any.

func (*Queue) Peek

func (q *Queue) Peek() (lavalink.Track, bool)

Peek returns the next track without removing it.

func (*Queue) Previous

func (q *Queue) Previous() []lavalink.Track

Previous returns a copy of the played tracks, newest first.

func (*Queue) Remove

func (q *Queue) Remove(ctx context.Context, i int) (lavalink.Track, bool)

Remove drops the track at index i and returns it.

func (*Queue) RemoveRange

func (q *Queue) RemoveRange(ctx context.Context, start, end int) ([]lavalink.Track, bool)

RemoveRange drops tracks in [start, end) and returns them.

func (*Queue) Replayed added in v0.1.3

func (q *Queue) Replayed() bool

Replayed reports whether the playing track came back out of the history, in which case it is already there and must not be retired into it twice.

func (*Queue) Save

func (q *Queue) Save(ctx context.Context) error

Save writes the queue to Config.Store; without one it does nothing.

func (*Queue) SetCurrent

func (q *Queue) SetCurrent(ctx context.Context, t *lavalink.Track)

SetCurrent replaces the playing track without touching the history. It copies: callers pass tracks out of events listeners still hold.

func (*Queue) SetOnChange added in v0.1.2

func (q *Queue) SetOnChange(f func(ctx context.Context, guildID string, change Change, tracks []lavalink.Track))

SetOnChange replaces the queue-change callback. It runs after every change, outside the lock.

func (*Queue) Shuffle

func (q *Queue) Shuffle(ctx context.Context)

Shuffle reorders the waiting tracks.

func (*Queue) Swap

func (q *Queue) Swap(ctx context.Context, i, j int) error

Swap exchanges two waiting tracks.

func (*Queue) Tracks

func (q *Queue) Tracks() []lavalink.Track

Tracks returns a copy of the tracks waiting to play.

type Store

type Store interface {
	Get(ctx context.Context, guildID string) ([]byte, error)
	Set(ctx context.Context, guildID string, data []byte) error
	Delete(ctx context.Context, guildID string) error
}

Store persists queues. Get returns nil for a guild it never saw; implementations must be safe for concurrent use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL