pylav.core package#

Submodules#

pylav.core.bot_overrides module#

async pylav.core.bot_overrides.get_context(self, message, /, *, cls=<class 'pylav.core.context.PyLavContext'>)[source]#

Get the context for a command invocation.

async pylav.core.bot_overrides.process_commands(self, message, /)[source]#

Same as base method, but dispatches an additional event for cogs which want to handle normal messages differently to command messages, without the overhead of additional get_context calls per cog.

pylav.core.client module#

class pylav.core.client.Client(*args, **kwargs)[source]#

Bases: object

Represents a Lavalink client used to manage nodes and connections.

Parameters:
  • bot (DISCORD_BOT_TYPE) – The bot instance.

  • cog (DISCORD_COG_TYPE) – The cog to register the client to.

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

  • connect_back (Optional[:class:`bool]`) –

    A boolean that determines if a player will connect back to the node it was originally connected to. This is not recommended doing 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 fail-over 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.

  • config_folder (Optional[:class:`pathlib.Path]`) – The path to the settings folder. Defaults to CONFIG_DIR.

async add_node(*, unique_identifier, host, port, password, resume_timeout=60, name=None, reconnect_attempts=-1, ssl=False, search_only=False, managed=False, yaml=None, disabled_sources=None, extras=None, temporary=False)[source]#

Adds a node to PyLav’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.

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

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

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

  • ssl (Optional[:class:`bool]`) – Whether to use SSL for the connection. Defaults to False.

  • search_only (bool) – Whether the node should only be used for searching. Defaults to False.

  • unique_identifier (in) – A unique identifier for the node. Defaults to None.

  • yaml (Optional[:class:`dict]`) – A dictionary of extra information to be stored in the node. Defaults to None.

  • extras (Optional[:class:`dict]`) – A dictionary of extra information to be stored in the node. Defaults to None.

  • managed (bool) – Whether the node is managed by the client. Defaults to False.

  • disabled_sources (Optional[:class:`list`[:class:`str]]`) – A list of sources that should be disabled for the node. Defaults to None.

  • temporary (bool) – Whether the node is temporary. Defaults to False.

property bot#

Returns the bot client

property bot_id#

Returns the bot id

property cached_session#

Returns the cached aiohttp session used by the PyLav client

property config_folder#

Returns the config folder

async connect_player(requester, channel, node=None, self_deaf=None)[source]#

Connects the player for the target guild.

Parameters:
  • channel (discord.channel.VocalGuildChannel) – The channel to connect to.

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

  • self_deaf (bool) – Whether the bot should be deafened.

  • requester (discord.Member) – The member requesting the connection.

Returns:

The player for the target guild.

Return type:

Player

async construct_embed(*, embed=None, colour=None, color=None, title=None, embed_type='rich', url=None, description=None, timestamp=None, author_name=None, author_url=None, thumbnail=None, footer=None, footer_url=None, messageable=None)[source]#

Constructs an embed.

async decode_track(track, feature=None, raise_on_failure=False, lazy=False)[source]#

Decodes a base64-encoded track string into a dict.

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

  • feature (Optional[:class:`str]`) – The feature to decode the track for. Defaults to None.

  • raise_on_failure (Optional[:class:`bool]`) – Whether to raise an exception if the track fails to decode. Defaults to False.

  • lazy (bool) – Weather to decode within the Bot or send to Lavalink. Defaults to False.

Returns:

An object representing the track’s information.

Return type:

LavalinkTrackObject

async decode_tracks(tracks, feature=None, raise_on_failure=False)[source]#

Decodes a list of base64-encoded track strings into a dict.

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

  • feature (Optional[:class:`str]`) – The feature to decode the tracks for. Defaults to None.

  • raise_on_failure (Optional[:class:`bool]`) – Whether to raise an exception if the tracks fail to decode. Defaults to False.

Returns:

A list of LavalinkTrackObject representing track information.

Return type:

List[LavalinkTrackObject]

dispatch_event(event)[source]#

Dispatches the given event to all registered hooks.

property dispatch_manager#

Returns the dispatch manager

property equalizer_db_manager#

Returns the sql equalizer config manager

async static generate_mix_playlist(*, video_id=None, user_id=None, playlist_id=None, channel_id=None)[source]#

Generates an YouTube mixed playlist url from a single video, user, channel or playlist.

get_all_players()[source]#

Gets all players.

async get_all_tracks_for_queries(*queries, requester, player=None, bypass_cache=False, enqueue=True)[source]#

High level interface to get and return all tracks for a list of queries.

This will automatically handle playlists, albums, searches and local files.

Parameters:
  • queries (Query) – The list of queries to search for.

  • bypass_cache (bool, optional) – Whether to bypass the cache and force a new search. Local files will always be bypassed.

  • requester (discord.Member) – The user who requested the op.

  • player (Player) – The player requesting the op.

  • enqueue (bool, optional) – Whether to enqueue the tracks as needed while try are processed so users dont sit waiting for the bot to finish.

Returns:

  • tracks (List[Track]) – The list of tracks found.

  • total_tracks (int) – The total number of tracks found.

  • queries (List[Query]) – The list of queries that were not found.

async get_context(what)[source]#

Gets the context for the target message or interaction.

get_managed_node()[source]#

Gets a managed node.

get_my_node()[source]#

Gets the local node that is managed by PyLav.

get_player(guild)[source]#

Gets the player for the target guild.

Parameters:

guild (discord.Guild) – The guild to get the player for.

Returns:

The player for the target guild.

Return type:

Player

async get_tracks(*queries, bypass_cache=False, fullsearch=False, region=None, player=None, sleep=False)[source]#

This method can be rather slow as it recursively queries all queries and their associated entries.

Thus, if you are processing user input you may be interested in using the get_all_tracks_for_queries() where it can enqueue tracks as needed to the player.

Parameters:
  • queries (Query) – The list of queries to search for.

  • bypass_cache (bool, optional) – Whether to bypass the cache and force a new search. Local files will always be bypassed.

  • fullsearch (bool, optional) – if a Search query is passed wether to returrn a list of tracks instead of the first.

  • region (str, optional) – The region to search in.

  • player (Player, optional) – The player to use for enqueuing tracks.

  • sleep (bool, optional) – Whether to sleep between each query to avoid ratelimits.

initialize()[source]#

Initialize the client

property initialized#

Returns whether the client has been initialized

async is_dj(user, guild, *, additional_role_ids=None, additional_user_ids=None)[source]#

Checks if a user is a DJ in a guild.

property is_shutting_down#

Returns whether the client is shutting down

property lib_db_manager#

Returns the sql lib config manager

property lib_version#

Returns the version of the PyLav library

property local_tracks_cache#

Returns the local tracks cache

property managed_node_controller#

Returns the local node manager

async managed_node_is_enabled()[source]#

Returns whether the managed node is enabled or not

property node_db_manager#

Returns the sql node config manager

property node_manager#

Returns the node manager

async on_pylav_ready()[source]#

Handle ready events.

async on_pylav_red_api_tokens_update(service_name, api_tokens)[source]#

Update API tokens for services when they are updated in Red.

async on_pylav_resumed()[source]#

Handle resume events.

async on_pylav_shard_ready(shard_id)[source]#

Handle shard ready events.

async on_pylav_shard_resumed(shard_id)[source]#

Handle shard resume events.

property player_config_manager#

Returns the player config manager

property player_manager#

Returns the player manager

property player_state_db_manager#

Returns the sql player state config manager

property playlist_db_manager#

Returns the sql playlist config manager

property query_cache_manager#

Returns the query cache manager

property radio_browser#

Returns the radio browser instance

async register(cog)[source]#

Register a cog to the PyLav Client.

async remove_node(node_id)[source]#

Removes a node from the node manager

async static routeplanner_free_address(node, address)[source]#

Gets the route-planner status of the target node.

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

  • address (str) – The address to free.

async static routeplanner_free_all_failing(node)[source]#

Gets the route-planner status of the target node.

Parameters:

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

async static routeplanner_status(node)[source]#

Gets the route-planner status of the target node.

Parameters:

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

Returns:

An object representing the route-planner information.

Return type:

RoutePlannerStatusResponseObject

property scheduler#

Returns the scheduler

async search_query(query, bypass_cache=False, fullsearch=False, region=None, sleep=False, player=None)[source]#

Search for the specified query returns a LoadTrackResponse object

Parameters:
  • query (Query) – The list of queries to search for.

  • bypass_cache (bool, optional) – Whether to bypass the cache and force a new search. Local files will always be bypassed.

  • fullsearch (bool, optional) – if a Search query is passed wether to returrn a list of tracks instead of the first.

  • region (str, optional) – The region to search in.

  • sleep (bool, optional) – Whether to sleep for a short duration if a lavalink call is made.

  • player (Player, optional) – The player to use for enqueuing tracks.

property session#

Returns the aiohttp session used by the PyLav client

async set_context_locale(guild)[source]#

Set the locale for the current context.

property spotify_client#

Returns the spotify client

async unregister(cog)[source]#

Unregister the specified Cog and if no cogs are left closes the client.

Parameters:

cog (discord.ext.commands.Cog) – The cog to unregister.

async update_applemusic_tokens(token, country_code, **kwargs)[source]#

Update Apple Music tokens for the managed node.

async update_deezer_tokens(master_token, **kwargs)[source]#

Update Deezer tokens for the managed node.

async update_google_account(email, password, **kwargs)[source]#

Update Google Account for the managed node.

async update_localtracks_folder(folder)[source]#

Updates the localtracks folder.

async update_spotify_tokens(client_id, client_secret, **kwargs)[source]#

Update Spotify tokens for the managed node and the client instance.

async update_yandex_tokens(token, **kwargs)[source]#

Update Yandex tokens for the managed node.

async wait_until_ready(timeout=None)[source]#

Wait until the client is ready to use.

pylav.core.context module#

class pylav.core.context.PyLavContext(*, message, bot, view, args=..., kwargs=..., prefix=None, command=None, invoked_with=None, invoked_parents=..., invoked_subcommand=None, subcommand_passed=None, command_failed=False, current_parameter=None, current_argument=None, interaction=None)[source]#

Bases: Context

author#

Union[User, Member]: Returns the author associated with this context’s command. Shorthand for Message.author

bot#
channel#

Returns the channel associated with this context’s command. Shorthand for Message.channel.

Type:

Union[abc.Messageable]

client#
property cog#

Returns the cog associated with this context’s command. None if it does not exist

Type:

Optional[Cog]

async connect_player(channel=None, self_deaf=None)[source]#

Connect player

async construct_embed(*, embed=None, colour=None, color=None, title=None, embed_type='rich', url=None, description=None, timestamp=None, author_name=None, author_url=None, thumbnail=None, footer=None, footer_url=None, messageable=None)[source]#

Construct embed

dispatch_command(message, command, author, args, prefix=None)[source]#

Dispatch command

async classmethod from_interaction(interaction, /)[source]#

Creates a context from a discord.Interaction. This only works on application command based interactions, such as slash commands or context menus.

On slash command based interactions this creates a synthetic Message that points to an ephemeral message that the command invoker has executed. This means that Context.author returns the member that invoked the command.

In a message context menu based interaction, the Context.message attribute is the message that the command is being executed on. This means that Context.author returns the author of the message being targetted. To get the member that invoked the command then discord.Interaction.user should be used instead.

New in version 2.0.

Parameters:

interaction (discord.Interaction) – The interaction to create a context with.

Raises:
  • ValueError – The interaction does not have a valid command.

  • TypeError – The interaction client is not derived from Bot or AutoShardedBot.

guild#

Returns the guild associated with this context’s command. None if not available

Type:

Optional[Guild]

interaction#
property original_ctx_or_interaction#

Get original ctx or interaction

property player#

Get player

async send_interactive(messages, box_lang=None, timeout=15, embed=False)[source]#

Send multiple messages interactively.

The user will be prompted for whether or not they would like to view the next message, one at a time. They will also be notified of how many messages are remaining on each prompt.

Parameters:
  • messages (iterable of str) – The messages to send.

  • box_lang (str) – If specified, each message will be contained within a codeblock of this language.

  • timeout (int) – How long the user has to respond to the prompt before it times out. After timing out, the bot deletes its prompt message.

  • embed (bool) – Whether or not to send the messages as embeds.

Module contents#