Documentation

nextcord.ext.lava.enable_debug_logging()

Sets up a logger to stdout. This solely exists to make things easier for end-users who want to debug issues with Lava.

nextcord.ext.lava.add_event_hook(*hooks, event: Optional[nextcord.ext.lava.events.Event] = None)

Adds an event hook to be dispatched on an event.

Parameters
  • hooks (function) – The hooks to register for the given event type. If event parameter is left empty, then it will run when any event is dispatched.

  • event (Event) – The event the hook belongs to. This will dispatch when that specific event is dispatched. Defaults to None which means the hook is dispatched on all events.

Client

class nextcord.ext.lava.Client(user_id: int, player=<class 'nextcord.ext.lava.models.DefaultPlayer'>, regions: Optional[dict] = None, connect_back: bool = False)

Represents a Lavalink client used to manage nodes and connections.

Parameters
  • user_id (int) – The user id of the bot.

  • player (Optional[BasePlayer]) – The class that should be used for the player. Defaults to DefaultPlayer. Do not change this unless you know what you are doing!

  • regions (Optional[dict]) – A dictionary representing region -> nextcord endpoint. You should only change this if you know what you’re doing and want more control over which regions handle specific locations. Defaults to None.

  • connect_back (Optional[bool]) –

    A boolean that determines if a player will connect back to the node it was originally connected to. This is not recommended to do since the player will most likely be performing better in the new node. Defaults to False.

    Warning

    If this option is enabled and the player’s node is changed through Player.change_node after the player was moved via the failover mechanism, the player will still move back to the original node when it becomes available. This behaviour can be avoided in custom player implementations by setting self._original_node to None in the change_node function.

node_manager

Represents the node manager that contains all lavalink nodes.

Type

NodeManager

player_manager

Represents the player manager that contains all the players.

Type

PlayerManager

add_node(host: str, port: int, password: str, region: str, resume_key: Optional[str] = None, resume_timeout: int = 60, name: Optional[str] = None, reconnect_attempts: int = 3)

Adds a node to Lavalink’s node manager.

Parameters
  • host (str) – The address of the Lavalink node.

  • port (int) – The port to use for websocket and REST connections.

  • password (str) – The password used for authentication.

  • region (str) – The region to assign this node to.

  • resume_key (Optional[str]) – A resume key used for resuming a session upon re-establishing a WebSocket connection to Lavalink. Defaults to None.

  • resume_timeout (Optional[int]) – How long the node should wait for a connection while disconnected before clearing all players. Defaults to 60.

  • name (Optional[str]) – An identifier for the node that will show in logs. Defaults to None

  • reconnect_attempts (Optional[int]) – The amount of times connection with the node will be reattempted before giving up. Set to -1 for infinite. Defaults to 3.

async get_tracks(query: str, node: Optional[nextcord.ext.lava.node.Node] = None)

This function is a coroutine. Gets all tracks associated with the given query.

Parameters
  • query (str) – The query to perform a search for.

  • node (Optional[Node]) – The node to use for track lookup. Leave this blank to use a random node. Defaults to None which is a random node.

Returns

A dict representing tracks.

Return type

dict

async decode_track(track: str, node: Optional[nextcord.ext.lava.node.Node] = None)

This function is a coroutine. Decodes a base64-encoded track string into a dict.

Parameters
  • track (str) – The base64-encoded track string.

  • node (Optional[Node]) – The node to use for the query. Defaults to None which is a random node.

Returns

A dict representing the track’s information.

Return type

dict

async decode_tracks(tracks: list, node: Optional[nextcord.ext.lava.node.Node] = None)

This function is a coroutine. Decodes a list of base64-encoded track strings into a dict.

Parameters
  • tracks (list[str]) – A list of base64-encoded track strings.

  • node (Optional[Node]) – The node to use for the query. Defaults to None which is a random node.

Returns

A list of dicts representing track information.

Return type

List[dict]

async routeplanner_status(node: nextcord.ext.lava.node.Node)

This function is a coroutine. Gets the routeplanner status of the target node.

Parameters

node (Node) – The node to use for the query.

Returns

A dict representing the routeplanner information.

Return type

dict

async routeplanner_free_address(node: nextcord.ext.lava.node.Node, address: str)

This function is a coroutine. Gets the routeplanner status of the target node.

Parameters
  • node (Node) – The node to use for the query.

  • address (str) – The address to free.

Returns

True if the address was freed, False otherwise.

Return type

bool

async routeplanner_free_all_failing(node: nextcord.ext.lava.node.Node)

This function is a coroutine. Gets the routeplanner status of the target node.

Parameters

node (Node) – The node to use for the query.

Returns

True if all failing addresses were freed, False otherwise.

Return type

bool

async voice_update_handler(data)

This function is a coroutine. This function intercepts websocket data from your nextcord library and forwards the relevant information on to Lavalink, which is used to establish a websocket connection and send audio packets to nextcord.

Example

bot.add_listener(lavalink_client.voice_update_handler, 'on_socket_response')
Parameters

data (dict) – The payload received from nextcord.

Events

All Events are derived from Event

class nextcord.ext.lava.Event

The base for all Lavalink events.

class nextcord.ext.lava.TrackStartEvent(player, track)

This event is dispatched when the player starts to play a track.

player

The player that started to play a track.

Type

BasePlayer

track

The track that started playing.

Type

AudioTrack

class nextcord.ext.lava.TrackEndEvent(player, track, reason)

This event is dispatched when the player finished playing a track.

player

The player that finished playing a track.

Type

BasePlayer

track

The track that finished playing.

Type

AudioTrack

reason

The reason why the track stopped playing.

Type

str

class nextcord.ext.lava.TrackStuckEvent(player, track, threshold)

This event is dispatched when the currently playing track is stuck. This normally has something to do with the stream you are playing and not Lavalink itself.

player

The player that has the playing track being stuck.

Type

BasePlayer

track

The track is stuck from playing.

Type

AudioTrack

threshold

The amount of time the track had while being stuck.

Type

int

class nextcord.ext.lava.TrackExceptionEvent(player, track, exception)

This event is dispatched when an exception occurs while playing a track.

player

The player that had the exception occur while playing a track.

Type

BasePlayer

track

The track that had the exception while playing.

Type

AudioTrack

exception

The type of exception that the track had while playing.

Type

Exception

class nextcord.ext.lava.QueueEndEvent(player)

This event is dispatched when there are no more songs in the queue.

player

The player that has no more songs in queue.

Type

BasePlayer

class nextcord.ext.lava.NodeConnectedEvent(node)

This event is dispatched when Lavalink.py successfully connects to a node.

node

The node that was successfully connected to.

Type

Node

class nextcord.ext.lava.NodeChangedEvent(player, old_node, new_node)

This event is dispatched when a player changes to another node. Keep in mind this event can be dispatched multiple times if a node disconnects and the load balancer moves players to a new node.

player

The player whose node was changed.

Type

BasePlayer

old_node

The node the player was moved from.

Type

Node

new_node

The node the player was moved to.

Type

Node

class nextcord.ext.lava.NodeDisconnectedEvent(node, code, reason)

This event is dispatched when a node disconnects and becomes unavailable.

node

The node that was disconnected from.

Type

Node

code

The status code of the event.

Type

int

reason

The reason of why the node was disconnected.

Type

str

class nextcord.ext.lava.WebSocketClosedEvent(player, code, reason, by_remote)

This event is dispatched when a audio websocket to nextcord is closed. This can happen happen for various reasons like an expired voice server update.

player

The player whose audio websocket was closed.

Type

BasePlayer

code

The node the player was moved from.

Type

int

reason

The node the player was moved to.

Type

str

by_remote

If the websocket was closed remotely.

Type

bool

Models

All custom players must derive from BasePlayer

class nextcord.ext.lava.AudioTrack(data: dict, requester: int, **extra)

Represents the AudioTrack sent to Lavalink.

Parameters
  • data (dict) – The data to initialise an AudioTrack from.

  • requester (any) – The requester of the track.

  • extra (dict) – Any extra information to store in this AudioTrack.

track

The base64-encoded string representing a Lavalink-readable AudioTrack.

Type

str

identifier

The track’s id. For example, a youtube track’s identifier will look like dQw4w9WgXcQ.

Type

str

is_seekable

Whether the track supports seeking.

Type

bool

author

The track’s uploader.

Type

str

duration

The duration of the track, in milliseconds.

Type

int

stream

Whether the track is a live-stream.

Type

bool

title

The title of the track.

Type

str

uri

The full URL of track.

Type

str

extra

Any extra properties given to this AudioTrack will be stored here.

Type

dict

class nextcord.ext.lava.BasePlayer(guild_id, node)

Represents the BasePlayer all players must be inherited from.

guild_id

The guild id of the player.

Type

str

node

The node that the player is connected to.

Type

Node

class nextcord.ext.lava.DefaultPlayer(guild_id, node)

The player that Lavalink.py defaults to use.

guild_id

The guild id of the player.

Type

int

node

The node that the player is connected to.

Type

Node

paused

Whether or not a player is paused.

Type

bool

position_timestamp

The position of how far a track has gone.

Type

int

volume

The volume at which the player is playing at.

Type

int

shuffle

Whether or not to mix the queue up in a random playing order.

Type

bool

repeat

Whether or not to continuously to play a track.

Type

bool

equalizer

The changes to audio frequencies on tracks.

Type

list

queue

The order of which tracks are played.

Type

list

current

The track that is playing currently.

Type

AudioTrack

property is_playing

Returns the player’s track state.

property is_connected

Returns whether the player is connected to a voicechannel or not.

property position

Returns the position in the track, adjusted for Lavalink’s 5-second stats interval.

store(key: object, value: object)

Stores custom user data.

Parameters
  • key (object) – The key of the object to store.

  • value (object) – The object to associate with the key.

fetch(key: object, default=None)

Retrieves the related value from the stored user data.

Parameters
  • key (object) – The key to fetch.

  • default (Optional[any]) – The object that should be returned if the key doesn’t exist. Defaults to None.

Returns

Return type

any

delete(key: object)

Removes an item from the the stored user data.

Parameters

key (object) – The key to delete.

add(requester: int, track: Union[nextcord.ext.lava.models.AudioTrack, dict], index: Optional[int] = None)

Adds a track to the queue.

Parameters
  • requester (int) – The ID of the user who requested the track.

  • track (Union[AudioTrack, dict]) – The track to add. Accepts either an AudioTrack or a dict representing a track returned from Lavalink.

  • index (Optional[int]) – The index at which to add the track. If index is left unspecified, the default behaviour is to append the track. Defaults to None.

async play(track: Optional[Union[nextcord.ext.lava.models.AudioTrack, dict]] = None, start_time: int = 0, end_time: int = 0, no_replace: bool = False)

Plays the given track.

Parameters
  • track (Optional[Union[AudioTrack, dict]]) – The track to play. If left unspecified, this will default to the first track in the queue. Defaults to None so plays the next song in queue. Accepts either an AudioTrack or a dict representing a track returned from Lavalink.

  • start_time (Optional[int]) – Setting that determines the number of milliseconds to offset the track by. If left unspecified, it will start the track at its beginning. Defaults to 0, which is the normal start time.

  • end_time (Optional[int]) – Settings that determines the number of milliseconds the track will stop playing. By default track plays until it ends as per encoded data. Defaults to 0, which is the normal end time.

  • no_replace (Optional[bool]) – If set to true, operation will be ignored if a track is already playing or paused. Defaults to False

async stop()

Stops the player.

async skip()

Plays the next track in the queue, if any.

set_repeat(repeat: bool)

Sets the player’s repeat state. :param repeat: Whether to repeat the player or not. :type repeat: bool

set_shuffle(shuffle: bool)

Sets the player’s shuffle state. :param shuffle: Whether to shuffle the player or not. :type shuffle: bool

async set_pause(pause: bool)

Sets the player’s paused state.

Parameters

pause (bool) – Whether to pause the player or not.

async set_volume(vol: int)

Sets the player’s volume

Note

A limit of 1000 is imposed by Lavalink.

Parameters

vol (int) – The new volume level.

async seek(position: int)

Seeks to a given position in the track.

Parameters

position (int) – The new position to seek to in milliseconds.

async set_gain(band: int, gain: float = 0.0)

Sets the equalizer band gain to the given amount.

Parameters
  • band (int) – Band number (0-14).

  • gain (Optional[float]) – A float representing gain of a band (-0.25 to 1.00). Defaults to 0.0.

async set_gains(*gain_list)

Modifies the player’s equalizer settings.

Parameters

gain_list (any) – A list of tuples denoting (band, gain).

async reset_equalizer()

Resets equalizer to default values.

async change_node(node)

Changes the player’s node

Parameters

node (Node) – The node the player is changed to.

Node

class nextcord.ext.lava.Node(manager, host: str, port: int, password: str, region: str, resume_key: str, resume_timeout: int, name: Optional[str] = None, reconnect_attempts: int = 3)

Represents a Node connection with Lavalink.

Note

Nodes are NOT mean’t to be added manually, but rather with Client.add_node(). Doing this can cause invalid cache and much more problems.

host

The address of the Lavalink node.

Type

str

port

The port to use for websocket and REST connections.

Type

int

password

The password used for authentication.

Type

str

region

The region to assign this node to.

Type

str

name

The name the Node is identified by.

Type

str

stats

The statistics of how the Node is performing.

Type

Stats

property available

Returns whether the node is available for requests.

property players

Returns a list of all players on this node.

property penalty

Returns the load-balancing penalty for this node.

async get_tracks(query: str)

This function is a coroutine. Gets all tracks associated with the given query.

Parameters

query (str) – The query to perform a search for.

Returns

A dict representing an AudioTrack.

Return type

dict

async routeplanner_status()

This function is a coroutine. Gets the routeplanner status of the target node.

Returns

A dict representing the routeplanner information.

Return type

dict

async routeplanner_free_address(address: str)

This function is a coroutine. Gets the routeplanner status of the target node.

Parameters

address (str) – The address to free.

Returns

True if the address was freed, False otherwise.

Return type

bool

async routeplanner_free_all_failing()

This function is a coroutine. Gets the routeplanner status of the target node.

Returns

True if all failing addresses were freed, False otherwise.

Return type

bool

Node Manager

class nextcord.ext.lava.NodeManager(lavalink, regions: dict)

Represents the node manager that contains all lavalink nodes.

iter(x):

Returns an iterator of all the nodes cached.

nodes

Cache of all the nodes that Lavalink has created.

Type

list

regions

All the regions that nextcord supports.

Type

dict

property available_nodes

Returns a list of available nodes.

add_node(host: str, port: int, password: str, region: str, resume_key: Optional[str] = None, resume_timeout: int = 60, name: Optional[str] = None, reconnect_attempts: int = 3)

Adds a node to Lavalink’s node manager.

Parameters
  • host (str) – The address of the Lavalink node.

  • port (int) – The port to use for websocket and REST connections.

  • password (str) – The password used for authentication.

  • region (str) – The region to assign this node to.

  • resume_key (Optional[str]) – A resume key used for resuming a session upon re-establishing a WebSocket connection to Lavalink. Defaults to None.

  • resume_timeout (Optional[int]) – How long the node should wait for a connection while disconnected before clearing all players. Defaults to 60.

  • name (Optional[str]) – An identifier for the node that will show in logs. Defaults to None.

  • reconnect_attempts (Optional[int]) – The amount of times connection with the node will be reattempted before giving up. Set to -1 for infinite. Defaults to 3.

remove_node(node: nextcord.ext.lava.node.Node)

Removes a node.

Parameters

node (Node) – The node to remove from the list.

get_region(endpoint: str)

Returns a Lavalink.py-friendly region from a nextcord voice server address.

Parameters

endpoint (str) – The address of the nextcord voice server.

Returns

Return type

Optional[str]

find_ideal_node(region: Optional[str] = None)

Finds the best (least used) node in the given region, if applicable.

Parameters

region (Optional[str]) – The region to find a node in. Defaults to None.

Returns

Return type

Optional[Node]

Player Manager

class nextcord.ext.lava.PlayerManager(lavalink, player)

Represents the player manager that contains all the players.

len(x):

Returns the total amount of cached players.

iter(x):

Returns an iterator of all the players cached.

players

Cache of all the players that Lavalink has created.

Type

dict

default_player

The player that the player manager is initialized with.

Type

BasePlayer

async destroy(guild_id: int)

Removes a player from cache, and also Lavalink if applicable. Ensure you have disconnected the given guild_id from the voicechannel first, if connected.

Warning

This should only be used if you know what you’re doing. Players should never be destroyed unless they have been moved to another Node.

Parameters

guild_id (int) – The guild_id associated with the player to remove.

values()

Returns an iterator that yields only values.

find_all(predicate=None)

Returns a list of players that match the given predicate.

Parameters

predicate (Optional[function]) – A predicate to return specific players. Defaults to None.

Returns

Return type

List[DefaultPlayer]

remove(guild_id: int)

Removes a player from the internal cache.

Parameters

guild_id (int) – The player that will be removed.

get(guild_id: int)

Gets a player from cache.

Parameters

guild_id (int) – The guild_id associated with the player to get.

Returns

Return type

Optional[DefaultPlayer]

create(guild_id: int, region: str = 'eu', endpoint: Optional[str] = None, node: Optional[nextcord.ext.lava.node.Node] = None)

Creates a player if one doesn’t exist with the given information.

If node is provided, a player will be created on that node. If region is provided, a player will be created on a node in the given region. If endpoint is provided, Lavalink.py will attempt to parse the region from the endpoint and return a node in the parsed region.

If node, region and endpoint are left unspecified, or region/endpoint selection fails, Lavalink.py will fall back to the node with the lowest penalty.

Region can be omitted if node is specified and vice-versa.

Parameters
  • guild_id (int) – The guild_id to associate with the player.

  • region (str) – The region to use when selecting a Lavalink node. Defaults to eu.

  • endpoint (str) – The address of the nextcord voice server. Defaults to None.

  • node (Node) – The node to put the player on. Defaults to None and a node with the lowest penalty is chosen.

Returns

Return type

DefaultPlayer

Stats

class nextcord.ext.lava.Stats(node, data)

Represents the stats of Lavalink node.

uptime

How long the node has been running for in milliseconds.

Type

int

players

The amount of players connected to the node.

Type

int

playing_players

The amount of players that are playing in the node.

Type

int

memory_free

The amount of memory free to the node.

Type

int

memory_used

The amount of memory that is used by the node.

Type

int

memory_allocated

The amount of memory allocated to the node.

Type

int

memory_reservable

The amount of memory reservable to the node.

Type

int

cpu_cores

The amount of cpu cores the system of the node has.

Type

int

system_load

The overall CPU load of the system.

Type

int

The CPU load generated by Lavalink.

Type

int

frames_sent

The number of frames sent to nextcord.

Warning

Given that audio packets are sent via UDP, this number may not be 100% accurate due to dropped packets.

Type

int

frames_nulled

The number of frames that yielded null, rather than actual data.

Type

int

frames_deficit

The number of missing frames. Lavalink generates this figure by calculating how many packets to expect per minute, and deducting frames_sent. Deficit frames could mean the CPU is overloaded, and isn’t generating frames as quickly as it should be.

Type

int

penalty
Type

Penalty

class nextcord.ext.lava.Penalty(stats)

Represents the penalty of the stats of a Node.

player_penalty
Type

int

cpu_penalty
Type

int

null_frame_penalty
Type

int

deficit_frame_penalty
Type

int

total
Type

int

Utilities

nextcord.ext.lava.format_time(time)

Formats the given time into HH:MM:SS.

Parameters

time (int) – The time in milliseconds.

Returns

Return type

str

nextcord.ext.lava.parse_time(time)

Parses the given time into days, hours, minutes and seconds. Useful for formatting time yourself.

Parameters

time (int) – The time in milliseconds.

Returns

Return type

Tuple[int, int, int, int]

nextcord.ext.lava.decode_track(track, decode_errors='ignore')

Decodes a base64 track string into an AudioTrack object.

Parameters
  • track (str) – The base64 track string.

  • decode_errors (str) – The action to take upon encountering erroneous characters within track titles.

Returns

Return type

AudioTrack