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
The main Moonlink Manager instance.
A standard JavaScript Map containing all active players, keyed by the Guild ID.
State & Filters
The total number of active players.
Returns an array of all active players.
Returns an array of players that are currently playing a track.
Returns an array of players that are currently idle (not playing).
Methods
create(options)
create(options)#
→PlayerCreates 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.
Configuration for the new player.
IPlayerConfig
The ID of the guild.
The ID of the voice channel to connect to.
Optional. ID of the text channel for sending updates.
Initial volume (0-1000). Default is 100.
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)#
→Player | undefinedRetrieves an existing player by Guild ID.
The ID of the guild.
const player = manager.players.get("123456789012345678");
if (player) {
player.pause(true);
}
has(guildId)
has(guildId)#
→booleanChecks if a player exists for the specified Guild ID.
The ID of the guild.
if (manager.players.has("123456789012345678")) {
console.log("Music is already active in this server.");
}
destroy(guildId, reason)
destroy(guildId, reason)#
→Promise<boolean>Destroys a specific player, disconnecting it from the voice channel and cleaning up resources. Returns true if a player was found and destroyed.
The ID of the guild.
Optional reason for destruction.
await manager.players.destroy("123456789012345678", "Command invoked");
destroyAll()
destroyAll()#
→Promise<void>Destroys all active players. Useful during bot shutdown.
(Alias for clear()).
process.on("SIGINT", async () => {
await manager.players.destroyAll();
process.exit(0);
});
filter(predicate)
filter(predicate)#
→Player[]Filters active players based on a condition.
The function to test each player.
// Find players with high volume
const loudPlayers = manager.players.filter(p => p.volume > 200);
find(predicate)
find(predicate)#
→Player | undefinedFinds the first player that matches a condition.
The function to test each player.
// Find a player in a specific voice channel
const player = manager.players.find(p => p.voiceChannelId === "987654321...");
map(callback)
map(callback)#
→T[]Maps all players to a new array of values.
The function to transform each player.
// Get a list of all Guild IDs with active players
const guildIds = manager.players.map(p => p.guildId);