Audio

This API acts as a wrapper for Red-LavaLink and allows developers to interact with the audio cog

Note

First attempt of creating an API for redbots audio module. This is not in any way production ready/stable yet. Use at your own risk

Many features of the core audio cog have been moved to the API’s code, thus the audio cog depends on it now This API however doesn’t depend on the audio cog being loaded

Basic Usage

from redbot.core import commands, audio

class MyAudioCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.bot.loop.create_task(self.initialize())
        #This function starts the lavalink server and established a lavalink and database connection

    async def initialize(self):
        await self.bot.wait_until_red_ready() #needed to ensure `bot` is fully initialized
        await audio.initialize(self.bot, "MyAudioCog", 365911945565569036)

    async def shutdown(self):
        await audio.shutdown("MyAudioCog", 365911945565569036)

    def cog_unload(self):
        self.bot.loop.create_task(self.shutdown())

    @commands.command()
    async def connect(self, ctx, channel: discord.VoiceChannel):
        player = audio.get_player(ctx.guild.id)
        if not player: #not currently connected to a voice channel
            player = await audio.connect(self.bot, ctx.author.voice.channel)
        await ctx.send(f"Successfully connected to {channel.mention}")

    @commands.command()
    async def play(self, ctx, query: str):
        player = audio.get_player(ctx.guild.id)
        if not player:
            return #use audio.connect first

        await player.play(ctx.author, query)
        now_playing = await player.current
        await ctx.send(f"Now playing: {now_playing.title}")

Event Reference

Dispatched when the lavalink server has been started

Parameters

java_path (str) – The java executable used to start the server

Dispatched when the lavalink server has been stopped

Dispatched when the lavalink connection has been established

Parameters
  • host (str) – The host of the lavalink server

  • password (str) – The password of the lavalink server

  • ws_port (in) – The server’s websocket port

on_red_audio_track_start(guild, track, requester)

Dispatched when a track has started

Parameters
  • guild (discord.Guild) – The guild in which the track has been started

  • track (lavalink.Track) – The track which have been started

  • requester (discord.Member) – The requester of the track

on_red_audio_track_enqueue(guild, track, requester)

Dispatched when a track is enqueued

Parameters
  • guild (discord.Guild) – The guild in which the track has been enqueued

  • track (lavalink.Track) – The tracks which have been enqueued

  • requester (discord.Member) – The requester of the tracks

on_red_audio_track_end(guild, track, requester, reason)

Dispatched when a track has ended

Parameters
  • guild (discord.Guild) – The guild in which the track has ended

  • track (lavalink.Track) – The track which has ended

  • requester (discord.Member) – The requester of the track

  • reason (lavalink.TrackEndReason) – The reason why the track has ended

on_red_audio_queue_end(guild, track, requester)

Dispatched when the queue has ended

Parameters
  • guild (discord.Guild) – The guild in which the queue has ended

  • track (lavalink.Track) – The last played track

  • requester (discord.Member) – The requester of the last track

on_red_audio_track_skip(guild, track, requester)

Dispatched when a track is skipped

Parameters
  • guild (discord.Guild) – The guild in which the track has been skipped

  • track (lavalink.Track) – The current track

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

on_red_audio_track_exception(guild, track, requester, exception)
Parameters
  • guild (discord.Guild) – The guild in which the exception occurred

  • track (lavalink.Track) – The track which got stuck

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

  • exception (str) – The exception

on_red_audio_track_stuck(guild, track, requester, threshold)
Parameters
  • guild (discord.Guild) – The guild in which the track has been stuck

  • track (lavalink.Track) – The track which got stuck

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

  • threshold (int) – Threshold milliseconds that the track has been stuck for

on_red_audio_audio_paused(guild, paused)

Dispatched when the player is paused/resumed

Parameters
  • guild (discord.Guild) – The guild in which the player has been stopped/resumed

  • paused (bool) – True if the player has been paused, False if resumed

on_red_audio_audio_stop(guild)

Dispatched when the player has been manually stopped

Parameters

guild (discord.Guild) – The guild in which the player disconnected

on_red_audio_audio_disconnect(guild)

Dispatched when the player disconnects from a voice channel

Parameters

guild (discord.Guild) – The guild in which the player disconnected

API Reference

audio

await redbot.core.audio.connect(bot, channel, deafen=False)[source]

Connects to a voice channel

Parameters
  • bot (Red) – The bot object

  • channel (discord.VoiceChannel) – The channel to connect to

  • deafen (bool) – Overwrites the configs auto_deafen value if given

Returns

Player – A player object

Return type

Player

await redbot.core.audio.dj_enabled(guild)[source]

Whether or not DJ controls are enabled

Parameters

guild (discord.Guild) – The guild for which to check

Returns

Whether or not DJ controls are enabled

Return type

bool

redbot.core.audio.get_player(guild_id)[source]

Get the Player object of the given guild

Parameters

guild (discord.Guild) – The guild to get the Player object from

Returns

Player – The Player object of this guild

Return type

Optional[Player]

await redbot.core.audio.initialize(bot, cog_name, identifier, force_restart_ll_server=False, force_reset_db_conn=False)[source]

Initializes the api and establishes all connections

Parameters
  • bot (Red) – The bot object

  • cog_name (str) – Cog name used to track api usage

  • identifier (int) – Unique identifier to counteract similar cog_names

  • force_restart_ll_server (bool) – Force restarts the lavalink sever if set to True

  • force_reset_db_conn (bool) – Force resets the database connection

await redbot.core.audio.is_dj(user)[source]

Whether ot not a user has the DJ role

Parameters

user (discord.Member) – The user to check for

Returns

Whether or not the user has the DJ role

Return type

bool

await redbot.core.audio.shutdown(cog_name, identifier, force_shutdown=False)[source]

Closes the api connection

Parameters
  • cog_name (str) – The same cog name used in :py:meth:initialize

  • identifier (int) – The same identifier used in :py:meth:initialize

  • force_shutdown (bool) – whether or not the connection should be force closed, even though other cogs are still connected

Player

Note

This class wraps various lavalink.Player methods and meanwhile interacts with RED’s inbuilt config and databases. This class shouldn’t be instantiated manually. Use audio.connect() to do so. The presence of this object guarantees that the bot is connected to a voice channel in the current guild

class redbot.core.audio.Player(bot, channel)[source]

Bases: object

property channel

The current voice channel

Type

discord.VoiceChannel

property current

The current track

Type

lavalink.Track

property guild

The player’s guild

Type

discord.Guild

property is_playing

Whether or not the player is playing :type: bool

property paused

Whether or not the player is paused

Type

bool

property position

Position of the current song

Type

float

property queue

The player’s queue

Type

list

property repeat

Whether or not the queue repeats

Type

bool

property shuffle

Whether or not the queue is shuffled

Type

bool

property volume

The player’s volume :type: int

await disconnect()[source]

Disconnects the player

await get_tracks(query, local_folder=None)[source]

Queries the local apis for a track and falls back to lavalink if none was found Note: falling back to lavalink is only possible if the bot is connected to a voice channel

Parameters
  • query (str) – The query to search for. Supports everything core audio supports

  • local_folder (pathlib.Path) – Local folder if track is a local track

Returns

Return type

List[lavalink.Track], playlist_data

await get_local_tracks(query)[source]

Converts a localtrack into a lavalink.Track object which can then be used in Player.play

Parameters

query (pathlib.Path) – path to the music file

Returns

Return type

List[lavalink.Track], playlist_data

fetch(key, default=None)[source]

Returns a stored metadata value.

Parameters
  • key – Key used to store metadata.

  • default – Optional, used if the key doesn’t exist.

await is_requester(member)[source]

Whether a user is the requester of the current song

Parameters

member (discord.Member) – Member to check

Returns

Return type

bool

maybe_shuffle(sticky_songs=1)[source]

Shuffles the queue if shuffle is True

await move_to(channel, deafen=True)[source]

Move the player to another voice channel

Parameters
  • channel (discord.VoiceChannel) – The channel to move the player to

  • deafen (bool) – Whether or not the player deafens

await pause()[source]

Pauses the player in a guild

await play(requester, query=None, track=None, local_folder=None, bump=False, bump_and_skip=False)[source]

Plays a track

Parameters
  • requester (discord.Member) – The requester of the song

  • query (str) – The query to search for. Supports everything core audio supports

  • track (lavalink.Track) – A track object to be enqueued

  • local_folder (pathlib.Path) – Local folder if track is a local track

  • bump (bool) – Puts the song at queue position 0 if True

  • bump_and_skip (bool) – acts like bump but also skips the current song

Returns

Return type

List[lavalink.Track], playlist_data

await resume()[source]

Resumes the player in a guild

await seek(seconds=None, timestamp=None)[source]

Skip to a given position or skips a given amount of seconds

Parameters
  • seconds (int) – Seconds to skip. Can be a negative number to go backwards

  • timestamp (str) – timestamp to skip to Expected format: hh:mm:ss -> 00:10:15 for example

await set_volume(vol)[source]

Set the player’s volume

Returns

the volume after change

Return type

int

await skip(requester, skip_to_track=None)[source]

Skips a track

Parameters
  • requester (discord.Member) – The requester of the skip

  • skip_to_track (int) – Amount of tracks you want to skip

Returns

The current track

Return type

lavalink.Track

await stop()[source]

Stop the playback

store(key, value)[source]

Stores a metadata value by key.

ServerManager

Note

This class handles the lavalink server subprocess. While the properties provided by this class may be useful, one shouldn’t interact with any other function since starting of the server and dealing with the jar is handled by audio.initialize(). Shutting down is handled by audio.shutdown()

class redbot.core.audio.ServerManager(bot, config)[source]

Bases: object

property build_time

lavalink build time

Type

str

property is_running

whether or not the ll server is running

Type

bool

property jvm

java version

Type

str

property lavaplayer

lavaplayer version

Type

str

property ll_branch

lavalink branch

Type

str

property ll_build

lavalink build

Type

str

await maybe_download_jar()[source]

Checks jar version and redownloads if outdated

property path

java path

Type

str

await shutdown_ll_server()[source]

Stops the internal lavalink server

await start_ll_server(java_path)[source]

Starts the internal lavalink server and handles jar downloading

Parameters
  • bot (Red) – The bot object to start the connection from

  • java_path (str) – the java path to use