Star ✨ on GitHub

PlayerManager

Manages the lifecycle of audio players, allowing creation, retrieval, filtering, and destruction of voice connections.

The PlayerManager is your gateway to managing voice sessions. It allows you to create new players for specific guilds, retrieve existing ones, and perform bulk operations like finding specific players or clearing all sessions.

Properties

PlayerManager Properties

Core components and state accessible via the PlayerManager instance.

References

managerManager

The main Moonlink Manager instance.

playersMap<string, Player>

A standard JavaScript Map containing all active players, keyed by the Guild ID.

State & Filters

sizenumber

The total number of active players.

allPlayer[]

Returns an array of all active players.

playingPlayersPlayer[]

Returns an array of players that are currently playing a track.

idlePlayersPlayer[]

Returns an array of players that are currently idle (not playing).

Methods

create(options)

Creates a new player for a guild. If a player already exists for that guild, it returns the existing one. It automatically selects the best available node for the new player.

optionsIPlayerConfig required

Configuration for the new player.

IPlayerConfig

guildIdstring required

The ID of the guild.

voiceChannelIdstring required

The ID of the voice channel to connect to.

textChannelIdstring

Optional. ID of the text channel for sending updates.

volumenumber

Initial volume (0-1000). Default is 100.

Example
const player = manager.players.create({
    guildId: '123456789012345678',
    voiceChannelId: '987654321098765432',
    textChannelId: '456789012345678901',
    volume: 80
});

// Connect to the voice channel
player.connect({ setDeaf: true, setMute: false });

get(guildId)

get(guildId)#

Retrieves an existing player by Guild ID.

guildIdstring required

The ID of the guild.

Example
const player = manager.players.get("123456789012345678");
if (player) {
    player.pause(true);
}

has(guildId)

has(guildId)#

Checks if a player exists for the specified Guild ID.

guildIdstring required

The ID of the guild.

Example
if (manager.players.has("123456789012345678")) {
    console.log("Music is already active in this server.");
}

destroy(guildId, reason)

destroy(guildId, reason)#

Destroys a specific player, disconnecting it from the voice channel and cleaning up resources. Returns true if a player was found and destroyed.

guildIdstring required

The ID of the guild.

reasonstring

Optional reason for destruction.

Example
await manager.players.destroy("123456789012345678", "Command invoked");

destroyAll()

destroyAll()#

Destroys all active players. Useful during bot shutdown. (Alias for clear()).

Example
process.on("SIGINT", async () => {
    await manager.players.destroyAll();
    process.exit(0);
});

filter(predicate)

Filters active players based on a condition.

predicate(player: Player) => boolean required

The function to test each player.

Example
// Find players with high volume
const loudPlayers = manager.players.filter(p => p.volume > 200);

find(predicate)

find(predicate)#

Finds the first player that matches a condition.

predicate(player: Player) => boolean required

The function to test each player.

Example
// Find a player in a specific voice channel
const player = manager.players.find(p => p.voiceChannelId === "987654321...");

map(callback)

Maps all players to a new array of values.

callback(player: Player) => T required

The function to transform each player.

Example
// Get a list of all Guild IDs with active players
const guildIds = manager.players.map(p => p.guildId);