Guides
Queue & Persistence
Manage queues and implement custom storage adapters.
This guide covers queue operations and patterns for persisting queue state across restarts using storage adapters.
Queue Operations
Add, remove, shuffle, and manage tracks in the player's queue.
import type { , } from 'hoshimi';
declare const : ;
declare const : ;
const = .;
await .();
const = await .();
.(`Queue size: ${.}`);
.(`Total size (incl. current): ${.}`);
.(`Current: ${.?..}`);
.(`History: ${..} tracks`);
await .();
await .(0, 1);
await .();In-Memory Storage (Default)
By default, queues and player data are stored in memory.
import { , } from 'hoshimi';
// The player storage is scoped to a guild.
const = new ('123456789012345678');
const = new ();
. = 'hoshimiplayer';
. = 'hoshimiqueue';Custom Persistence (Redis Example)
Implement a custom storage adapter to persist queue data to Redis.
import type { QueueJSON, } from 'hoshimi';
import { } from 'hoshimi';
interface RedisClient {
(: string): <string | null>;
(: string, : string): <void>;
(: string): <number>;
(: string): <number>;
}
class extends <QueueJSON> {
constructor(private readonly : RedisClient) {
super();
}
override async (: string): <QueueJSON | undefined> {
const = await this..(this.(this., ));
return ? this.() : ;
}
override async (: string, : QueueJSON): <void> {
await this..(this.(this., ), this.());
}
override async (: string): <boolean> {
return (await this..(this.(this., ))) > 0;
}
override async (: string): <boolean> {
return (await this..(this.(this., ))) > 0;
}
override async (): <void> {
// Implement according to your redis strategy
}
override (: unknown): QueueJSON {
return typeof === "string" ? .() : ( as QueueJSON);
}
override < = string>(: unknown): {
return (typeof === "object" ? .() : ) as ;
}
override (...: <string>): string {
return .().(":");
}
}Persistence Strategy
- What to persist: Guild ID, voice channel ID, text channel ID, queue data, current track position, node session info.
- What to skip: Timestamps, computed state, message IDs (these can go stale).
- Feature flags: Store in player storage (
enabledAutoplay,enabledLyrics). - Cleanup: On
PlayerDestroyandQueueEnd, clear transient storage (lyrics subscriptions, message IDs).