Skip to main content

A full-featured Python Lavalink client for Discord music bots

Project description

Sonora

PyPI version Python 3.11+ License: MIT Build Status Coverage

A full-featured, production-ready Python Lavalink client for building music-capable Discord bots. Equivalent to Riffy but in Python.

🚀 v1.2.0-beta now available with Smart Autoplay & Queue Intelligence!

Features

  • Async-first Python library compatible with Python 3.11+
  • Full Lavalink protocol support (v3 & v4)
  • Integrations for discord.py, py-cord, and nextcord
  • Plugin system for platform adapters (YouTube, Spotify, SoundCloud)
  • Voice connection management, track loading, queue management, player controls
  • Node pooling & reconnection, smart autoplay, per-guild voice state resumption
  • Intelligent queue system with session memory and adaptive reordering
  • Clean high-level API + low-level control
  • Well-documented with MkDocs
  • Thoroughly tested (coverage >= 36%)
  • Packaged for PyPI, CI/CD with GitHub Actions, Docker support

v1.2.0-beta Features

⚠️ BETA FEATURES - Experimental and subject to change

  • Smart Autoplay Engine: Context-aware track recommendations based on artist, genre, and popularity
  • Intelligent Queue: Session memory, similarity scoring, smart shuffle, skip fatigue detection
  • Adaptive Queue Reordering: Automatic queue optimization based on metrics
  • Plugin Hooks: Extend autoplay strategies and similarity scorers
  • Enhanced CLI: New commands for autoplay and queue management

Installation

pip install py-sonora

Quickstart

First, set up your environment variables:

cp .env.example .env
# Edit .env with your Lavalink server details and Discord token

Minimal Discord Bot Example

import discord
from discord.ext import commands
from sonora import SonoraClient

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='!', intents=intents)
sonora = SonoraClient(
    lavalink_nodes=[{"host": "127.0.0.1", "port": 2333, "password": "youshallnotpass"}],
    node_pooling=True,
    reconnect_policy={"max_retries": 5, "backoff": "exponential"}
)

@bot.event
async def on_ready():
    await sonora.start()
    print(f'Logged in as {bot.user}')

@bot.command()
async def join(ctx):
    if ctx.author.voice:
        await ctx.author.voice.channel.connect()
        player = await sonora.get_player(ctx.guild.id)
        await ctx.send("Joined voice channel!")
    else:
        await ctx.send("You need to be in a voice channel!")

@bot.command()
async def play(ctx, *, query):
    player = await sonora.get_player(ctx.guild.id)
    track = await player.play(query)
    await ctx.send(f"Now playing: {track.title}")

bot.run(os.getenv('DISCORD_TOKEN'))

For more examples, see the examples/ directory.

Configuration

Sonora supports configuration via environment variables:

  • LAVALINK_HOST: Lavalink server host (default: 127.0.0.1)
  • LAVALINK_PORT: Lavalink server port (default: 2333)
  • LAVALINK_PASSWORD: Lavalink server password
  • DISCORD_TOKEN: Your Discord bot token

See .env.example for a full list.

Documentation

Full documentation is available at https://code-xon.github.io/sonora/.

Development

Prerequisites

Setup

git clone https://github.com/code-xon/sonora.git
cd sonora
pip install -e .[dev]
pre-commit install

Testing

pytest

Building Docs

mkdocs serve

Contributing

See CONTRIBUTING.md for details.

License

MIT License - see LICENSE for details.

Contact

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_sonora-1.2.0b0.tar.gz (41.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_sonora-1.2.0b0-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

Details for the file py_sonora-1.2.0b0.tar.gz.

File metadata

  • Download URL: py_sonora-1.2.0b0.tar.gz
  • Upload date:
  • Size: 41.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for py_sonora-1.2.0b0.tar.gz
Algorithm Hash digest
SHA256 232a739495a24acc66bcb1c36db5b3f6c06d60fe7981f458afaff7a829224b90
MD5 0962e937f39a68b786194226e98d5738
BLAKE2b-256 1303072260fd7e2aa6594f718ac3c1b758b2578534c5e66eb34b8c713482aa49

See more details on using hashes here.

File details

Details for the file py_sonora-1.2.0b0-py3-none-any.whl.

File metadata

  • Download URL: py_sonora-1.2.0b0-py3-none-any.whl
  • Upload date:
  • Size: 30.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for py_sonora-1.2.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 44f406a836a20eb6e68e3e1207e1a2f6550873a99331f61beb47d7da409d3979
MD5 a064fb01d82c756e4abb1ffc2904ec8e
BLAKE2b-256 4f675aa81c1ced7a72a9e20ee987a28e18f7370802b3d5e23192a8c3e9bed2d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page