The Node
class represents a connection to a Lavalink server. It manages the communication between your Discord bot and the Lavalink server, enabling audio playback.
Nodes are the connection points to Lavalink servers. Each node handles the actual audio processing and streaming, while your bot manages the player interface.
Constructor
The Node constructor is not meant to be used directly
Manager
when you initialize it with node options.// Don't use directly
// new Node(options, manager);
// Instead, configure nodes through the Manager
const manager = new Manager({
nodes: [
{
identifier: 'Node 1',
host: 'localhost',
port: 2333,
password: 'youshallnotpass',
secure: false
}
],
// ... other options
});
Properties
Property | Type | Description | Default |
---|---|---|---|
uuid | String | Unique identifier for the node instance | Generated |
host | String | The hostname of the Lavalink server | Required |
port | Number | The port of the Lavalink server | Required |
identifier | String | A unique identifier for the node. If not provided, a UUID will be generated. | Generated |
password | String | The password for the Lavalink server. | "youshallnotpass" |
pathVersion | String | API version path for Lavalink | "v4" |
connected | Boolean | Whether the node is connected | false |
destroyed | Boolean | Whether the node has been destroyed | false |
reconnectTimeout | NodeJS.Timeout | Current reconnection timeout | undefined |
reconnectAttempts | Number | Number of reconnection attempts made | 0 |
retryAmount | Number | Maximum number of reconnection attempts | 5 |
retryDelay | Number | Delay between reconnection attempts (ms) | 60000 |
resumed | Boolean | Whether the node session was resumed | false |
resumeTimeout | Number | Session resume timeout (ms) | 60000 |
regions | Array<String> | Voice regions associated with this node | [] |
secure | Boolean | Whether to use SSL/TLS connection | false |
sessionId | String | The current session ID | Generated |
priority | Number | The priority of the node for selection | undefined |
socket | WebSocket | The WebSocket connection to Lavalink | Created |
stats | INodeStats | Node statistics (CPU, memory, players) | undefined |
info | Object | Additional node information | undefined |
version | String | Lavalink server version | undefined |
url | String | Base URL for REST requests | Generated |
rest | REST | The REST instance for making HTTP requests | Created |
state | NodeState | The current state of the node | DISCONNECTED |
capabilities | Array<String> | Capabilities supported by the node (e.g., lavadspx , lavasearch ) | [] |
plugins | Map<String, AbstractPlugin> | Map of loaded plugins for this node | Map |
Methods
connect
Connect to Node
Establishes connection to the Lavalink server with optional session resuming. Sets the node state to CONNECTING
.
Returns & Example
Returns
• void
node.connect();
reconnect
Reconnect Node
Attempts to reconnect to the Lavalink server after a delay with exponential backoff. Sets the node state to CONNECTING
. If reconnect attempts are exhausted, it attempts to migrate players to another node.
Returns & Example
Returns
• void
node.reconnect();
destroy
Destroy Node
Closes the WebSocket connection and marks the node as destroyed. Unloads any loaded plugins for this node.
Returns & Example
Returns
• void
node.destroy();
getPlayers
Get Players
Gets all players connected to this node.
Returns & Example
Returns
• Player[]
— Array of players connected to this node
const players = node.getPlayers();
console.log(`Players on node: ${players.length}`);
getPlayersCount
Get Players Count
Gets the number of players connected to this node.
Returns & Example
Returns
• number
— Number of connected players
const count = node.getPlayersCount;
console.log(`Connected players: ${count}`);
getSystemStats
Get System Stats
Retrieves CPU and memory usage statistics for the node.
Returns & Example
Returns
• { cpuLoad: number; memoryUsage: number }
— Object containing CPU load and memory usage
const stats = node.getSystemStats();
console.log(`CPU Load: ${stats.cpuLoad}%, Memory Usage: ${stats.memoryUsage} bytes`);
isOverloaded
Is Overloaded
Checks if the node is currently overloaded based on CPU and memory thresholds.
Parameters
80
80
Returns & Example
Returns
• boolean
— True if the node is overloaded, false otherwise
if (node.isOverloaded()) {
console.log('Node is overloaded!');
}
migrateAllPlayers
Migrate All Players
Migrates all players from this node to another specified node or the best available node.
Parameters
Returns & Example
Returns
• Promise<void>
await node.migrateAllPlayers();
getNodeStatus
Get Node Status
Retrieves a comprehensive status report for the node, including connection, performance, and player information.
Parameters
2000
Returns & Example
Returns
• object
— An object containing the node's status, including connected
, version
, stats
(cpu, memory, uptime), players
(total, active, paused, idle), and health
(status, needsRestart, responding, performance).
const status = await node.getNodeStatus();
console.log(status);
/*
Example Output:
{
identifier: 'Node-1',
connected: true,
version: '1.4.0',
stats: {
cpu: 0.5,
memory: 1024000000,
uptime: 123456789
},
players: {
total: 5,
active: 3,
paused: 2,
idle: 0
},
health: {
status: 'stable',
needsRestart: false,
responding: true,
performance: 'excellent'
}
}
*/
Performance Considerations
Consider these tips for optimal node performance:
- Load Balancing: Distribute players across multiple nodes for better performance.
- Monitoring: Regularly monitor node statistics to identify performance issues.
- Reconnection: Properly configure reconnection parameters to handle temporary disconnections.
- Regions: Associate nodes with specific regions to optimize voice latency.