API Reference
Main functions
- await lavalink.initialize(bot: discord.ext.commands.bot.Bot)[source]
Setup event and update listener
Important
This function must only be called AFTER the bot has received its “on_ready” event!
- Parameters
bot (discord.ext.commands.Bot) – An instance of a discord Bot object.
- await lavalink.add_node(bot: discord.ext.commands.bot.Bot, host: str, password: str, ws_port: int, timeout: int = 30, resume_key: Optional[str] = None, resume_timeout: int = 60)[source]
Create and initialize a new node
Important
This function must only be called AFTER the initialize function
- Parameters
bot (discord.ext.commands.Bot) – An instance of a discord Bot object.
host (str) – The hostname or IP address of the Lavalink node.
password (str) – The password of the Lavalink node.
ws_port (int) – The websocket port on the Lavalink Node.
timeout (int) – Amount of time to allow retries to occur,
None
is considered forever.resume_key (Optional[str]) – A resume key used for resuming a session upon re-establishing a WebSocket connection to Lavalink.
resume_timeout (int) – How long the node should wait for a connection while disconnected before clearing all players.
- await lavalink.connect(channel: discord.channel.VoiceChannel, deafen: bool = False)[source]
Connects to a discord voice channel.
This is the publicly exposed way to connect to a discord voice channel. The
initialize()
function must be called first!- Parameters
deafen (bool) – Prevent the bot from listening others
channel (discord.VoiceChannel) – The channel to move to
- Returns
The created Player object.
- Return type
- Raises
IndexError – If there are no available lavalink nodes ready to connect to discord.
- lavalink.get_player(guild_id: int) lavalink.player.Player [source]
Get the player of a guild
- await lavalink.close(bot: discord.ext.commands.bot.Bot)[source]
Closes the lavalink connection completely.
- Parameters
bot (discord.ext.commands.Bot) –
- lavalink.register_event_listener(coro: Callable[[...], Awaitable[None]])[source]
Registers a coroutine to receive lavalink event information.
This coroutine will accept three arguments:
Player
,LavalinkEvents
, and possibly an extra. The value of the extra depends on the value of the second argument.If the second argument is
LavalinkEvents.TRACK_END
, the extra will be aTrackEndReason
.If the second argument is
LavalinkEvents.TRACK_EXCEPTION
, the extra will be a dictionary withmessage
,cause
, andseverity
keys.If the second argument is
LavalinkEvents.TRACK_STUCK
, the extra will be the threshold milliseconds that the track has been stuck for.If the second argument is
LavalinkEvents.TRACK_START
, the extra will be a track identifier string.If the second argument is any other value, the third argument will not exist.
- lavalink.register_update_listener(coro: Callable[[...], Awaitable[None]])[source]
Registers a coroutine to receive lavalink player update information.
This coroutine will accept two arguments: an instance of
Player
and an instance ofPlayerState
.
- lavalink.register_stats_listener(coro: Callable[[...], Awaitable[None]])[source]
Registers a coroutine to receive lavalink server stats information.
This coroutine will accept a single argument which will be an instance of
Stats
.
- lavalink.unregister_event_listener(coro: Callable[[...], Awaitable[None]])[source]
Unregisters coroutines from being event listeners.
- Parameters
coro (coroutine) –
- lavalink.unregister_update_listener(coro: Callable[[...], Awaitable[None]])[source]
Unregisters coroutines from being player update listeners.
- Parameters
coro (coroutine) –
- lavalink.unregister_stats_listener(coro: Callable[[...], Awaitable[None]])[source]
Unregisters coroutines from being server stats listeners.
- Parameters
coro (coroutine) –
- lavalink.all_players() Tuple[lavalink.player.Player] [source]
Get all the players
- lavalink.all_connected_players() Tuple[lavalink.player.Player] [source]
Get all the connected players
- lavalink.active_players() Tuple[lavalink.player.Player] [source]
Get all the active players
Enums
- class lavalink.LavalinkEvents(value)[source]
An enumeration of the Lavalink Track Events.
- TRACK_END = 'TrackEndEvent'
The track playback has ended.
- TRACK_EXCEPTION = 'TrackExceptionEvent'
There was an exception during track playback.
- TRACK_STUCK = 'TrackStuckEvent'
Track playback got stuck during playback.
- TRACK_START = 'TrackStartEvent'
The track playback started.
- WEBSOCKET_CLOSED = 'WebSocketClosedEvent'
Websocket has been closed.
- FORCED_DISCONNECT = 'ForcefulDisconnectEvent'
Connection has been disconnect, do not attempt to reconnect.
- QUEUE_END = 'QueueEndEvent'
This is a custom event generated by this library to denote the end of all tracks in the queue.
- class lavalink.TrackEndReason(value)[source]
The reasons why track playback has ended.
- FINISHED = 'FINISHED'
The track reached the end, or the track itself ended with an exception.
- LOAD_FAILED = 'LOAD_FAILED'
The track failed to start, throwing an exception before providing any audio.
- STOPPED = 'STOPPED'
The track was stopped due to the player being stopped.
- REPLACED = 'REPLACED'
The track stopped playing because a new track started playing.
- CLEANUP = 'CLEANUP'
The track was stopped because the cleanup threshold for the audio player was reached.
- class lavalink.FiltersOp(value)[source]
An enumeration of all filters available to use
- VOLUME = 'volume'
Manage song volume
- EQUALIZER = 'equalizer'
Edit equalizer
- KARAOKE = 'karaoke'
Remove vocals
- TIMESCALE = 'timescale'
Changes the speed, pitch, and rate
- TREMOLO = 'tremolo'
Create a shuddering effect
- VIBRATO = 'vibrato'
Produce a pulsating change of the pitch
- ROTATION = 'rotation'
Audio Panning
- DISTORTION = 'distortion'
Distortion effect
- CHANNEL_MIX = 'channelMix'
Mixes both channels (left and right)
- LOW_PASS = 'lowPass'
Higher frequencies get suppressed, while lower frequencies pass through this filter
- class lavalink.NodeState(value)[source]
An enumeration.
- CONNECTING = 0
The client is connecting to the node
- READY = 1
The node is ready to send and receive requests
- RECONNECTING = 2
The connection with the node was lost. Now it’s reconnecting with it
- DISCONNECTING = 3
Closing connection with the node
- class lavalink.PlayerState(value)[source]
An enumeration.
- CREATED = -1
Player was created
- CONNECTING = 0
The client is connecting to the player
- READY = 1
The player is ready to reproduce
- NODE_BUSY = 2
The node cannot respond to the player requests
- RECONNECTING = 3
The connection with the player was lost. Now it’s reconnecting with it
- DISCONNECTING = 4
Closing connection with the player
Player
- class lavalink.Player(client: Bot = None, channel: discord.VoiceChannel = None, node: Node = None, ssl: bool = False)[source]
The Player class represents the current state of playback. It also is used to control playback and queue tracks.
The existence of this object guarantees that the bot is connected to a voice channel.
- channel
The channel the bot is connected to.
- Type
- await on_voice_server_update(data: dict[str, Any])[source]
Send voice server update data to the node
- await wait_until_ready(timeout: Optional[float] = None, no_raise: bool = False) bool [source]
Waits for the underlying node to become ready.
- await connect(timeout: float = 2.0, reconnect: bool = False, deafen: bool = False)[source]
Connects to the voice channel associated with this Player.
- await move_to(channel: discord.channel.VoiceChannel, deafen: bool = False)[source]
Moves this player to a voice channel.
- Parameters
deafen (bool) – Prevent the bot from listening others
channel (discord.VoiceChannel) – The channel to move to
- Raises
TypeError – If the channel is in another guild
- await disconnect(force: bool = False)[source]
Disconnects this player from its voice channel.
- Parameters
force (bool) – Force the player to disconnect
- fetch(key: Union[str, int], default: Optional[Any] = None) Any [source]
Returns a stored metadata value.
- Parameters
key – Used to store metadata.
default – Optional, used if the key doesn’t exist.
- Returns
If the key doesn’t exist returns the default value, otherwise the set value
- Return type
Any
- await update_state(state: lavalink.enums.PlayerState)[source]
Change the current player state :param state: The state to set :type state: PlayerState
- await handle_event(event: lavalink.enums.LavalinkEvents, extra: Any)[source]
Handles various Lavalink Events.
If the event is TRACK END, extra will be TrackEndReason.
If the event is TRACK EXCEPTION, extra will be the string reason.
If the event is TRACK STUCK, extra will be the threshold ms.
- Parameters
event (node.LavalinkEvents) –
extra (Any) –
- await handle_player_update(state: lavalink.tuples.PositionTime)[source]
Handles player updates from lavalink.
- Parameters
state (websocket.PlayerState) –
- add(requester: discord.user.User, track: lavalink.rest_api.Track)[source]
Adds a track to the queue.
- Parameters
requester (discord.User) – Who requested the track.
track (Track) – Result from any of the lavalink track search methods.
- await set_volume(volume: int)[source]
Sets the volume of Lavalink.
- Parameters
volume (int) – Between 0 and 150
- await seek(position: int)[source]
If the track allows it, seeks to a position.
- Parameters
position (int) – Between 0 and track length.
- await random_distortion()[source]
Apply a random distortion
Warning
It can be extremely painful to listen to
- cleanup()
This method must be called to ensure proper clean-up during a disconnect.
It is advisable to call this from within
disconnect()
when you are completely done with the voice protocol instance.This method removes it from the internal state cache that keeps track of currently alive voice clients. Failure to clean-up will cause subsequent connections to report that it’s still connected.
- await get_tracks(query: str) Tuple[lavalink.rest_api.Track, ...]
Gets tracks from lavalink.
- await karaoke(level: float = 1.0, mono_level: float = 1.0, filter_band: float = 220.0, filter_width: float = 100.0)[source]
Remove the vocals
- await load_tracks(query: str) lavalink.rest_api.LoadResult
Executes a loadtracks request. Only works on Lavalink V3.
- Parameters
query (str) –
- Return type
- await search_sc(query: str) lavalink.rest_api.LoadResult
Gets track results from SoundCloud from Lavalink.
- Parameters
query (str) –
- Return type
list of Track
- await search_yt(query: str) lavalink.rest_api.LoadResult
Gets track results from YouTube from Lavalink.
- Parameters
query (str) –
- Return type
list of Track
- await rotation(rotation: Optional[int] = None)[source]
Rotates the sound around the stereo channels/user headphones Audio Panning
- Parameters
rotation (Optional[int]) – The frequency of the audio rotating around the listener in Hz
- await timescale(speed: float = 1.0, pitch: float = 1.0, rate: float = 1.0)[source]
Changes the speed, pitch, and rate
- await tremolo(frequency: float = 2.0, depth: float = 0.5)[source]
Uses amplification to create a shuddering effect. Oscillates the volume
- await distortion(sin_offset: float = 0, sin_scale: float = 1, cos_offset: float = 0, cos_scale: float = 1, tan_offset: float = 0, tan_scale: float = 1, offset: float = 0, scale: float = 1)[source]
Distortion effect
Node
- class lavalink.Node(_loop: asyncio.base_events.BaseEventLoop, event_handler: Callable, host: str, password: str, port: int, user_id: int, num_shards: int, resume_key: Optional[str] = None, resume_timeout: int = 60, bot: Optional[discord.ext.commands.bot.Bot] = None)[source]
- await connect(timeout: Optional[int] = None, ssl: bool = False)[source]
Connects to the Lavalink player event websocket.
- Parameters
- Raises
asyncio.TimeoutError – If the websocket failed to connect after the given time.
- await create_player(channel: Union[discord.channel.VoiceChannel, discord.channel.StageChannel], deafen: bool = False) lavalink.player.Player [source]
Connects to a discord voice channel.
This function is safe to repeatedly call as it will return an existing player if there is one.
- Parameters
deafen –
channel –
- Returns
The created Player object.
- Return type
- get_player(guild_id: int) lavalink.player.Player [source]
Gets a Player object from a guild ID.
- add_player(guild_id: int, player: lavalink.player.Player)[source]
Register a player
- remove_player(player: lavalink.player.Player)[source]
Remove a player
- await destroy_guild(guild_id: int)[source]
Disconnect from a guild and delete the player
- Parameters
guild_id (int) –
- await stop(guild_id: int)[source]
Stop the player on the given guild id
- Parameters
guild_id (int) –
- await play(guild_id: int, track: lavalink.rest_api.Track, replace: bool = True, start: int = 0, pause: bool = False)[source]
Start playing on the given guild id
- await volume(guild_id: int, _volume: int)[source]
Set the volume of the track :param guild_id: :type guild_id: int :param _volume: Volume may range from 0 to 1000 :type _volume: int
- await equalizer(guild_id: int, bands: list[lavalink.tuples.EqualizerBands])[source]
Set the equalizer
- Parameters
guild_id (int) –
bands (list[EqualizerBands]) – A list of bands to change
- await karaoke(guild_id: int, level: float = 1.0, mono_level: float = 1.0, filter_band: float = 220.0, filter_width: float = 100.0)[source]
Uses equalization to eliminate part of a band, usually targeting vocals.
- await time_scale(guild_id: int, speed: float = 1.0, pitch: float = 1.0, rate: float = 1.0)[source]
Changes the speed, pitch, and rate. All default to 1.
- await tremolo(guild_id: int, frequency: float = 2.0, depth: float = 0.5)[source]
Uses amplification to create a shuddering effect, where the volume quickly oscillates.
- await vibrato(guild_id: int, frequency: float = 2.0, depth: float = 0.5)[source]
Similar to tremolo. While tremolo oscillates the volume, vibrato oscillates the pitch.
- await rotation(guild_id: int, rotation: int = 0)[source]
Rotates the sound around the stereo channels/user headphones aka Audio Panning. It can produce an effect similar to: https://youtu.be/QB9EB8mTKcc (without the reverb)
- await distortion(guild_id: int, sin_offset: int = 0, sin_scale: int = 1, cos_offset: int = 0, cos_scale: int = 1, tan_offset: int = 0, tan_scale: int = 1, offset: int = 0, scale: int = 1)[source]
Distortion effect
- await channel_mix(guild_id: int, left_to_left: float = 1.0, left_to_right: float = 0.0, right_to_left: float = 0.0, right_to_right: float = 1.0)[source]
Mixes both channels (left and right), with a configurable factor on how much each channel affects the other. With the defaults, both channels are kept independent from each other. Setting all factors to 0.5 means both channels get the same audio. :param guild_id: :type guild_id: int :param left_to_left: :type left_to_left: float :param left_to_right: :type left_to_right: float :param right_to_left: :type right_to_left: float :param right_to_right: :type right_to_right: float
- class lavalink.NodeStats(data: dict[str, Any])[source]
The NodeStats class contains the performance stats of a certain node
Rest API
- class lavalink.rest_api.Track(data: dict[str, Any])[source]
Information about a Lavalink track.
- requester
The user who requested the track.
- Type
- class lavalink.rest_api.RESTClient(player: Player, ssl: bool = False)[source]
Client class used to access the REST endpoints on a Lavalink node.
- state
The current player state
- Type
- await load_tracks(query: str) lavalink.rest_api.LoadResult [source]
Executes a loadtracks request. Only works on Lavalink V3.
- Parameters
query (str) –
- Return type
- await get_tracks(query: str) Tuple[lavalink.rest_api.Track, ...] [source]
Gets tracks from lavalink.
- await search_yt(query: str) lavalink.rest_api.LoadResult [source]
Gets track results from YouTube from Lavalink.
- Parameters
query (str) –
- Return type
list of Track
- await search_sc(query: str) lavalink.rest_api.LoadResult [source]
Gets track results from SoundCloud from Lavalink.
- Parameters
query (str) –
- Return type
list of Track