tools.play_music module
Play music in Discord voice channels using Lyria RealTime via ctx.adapter.
- async tools.play_music.run(guild_id, channel_id=None, prompt=None, bpm=135, temperature=1.0, brightness=0.5, scale=None, music_generation_mode=None, guidance=None, density=None, mute_bass=None, mute_drums=None, only_bass_and_drums=None, top_k=None, seed=None, ctx=None)[source]
Join a Discord voice channel and start AI-generated music via Lyria.
Implements the
play_musictool: it connects (or moves) the bot into a voice channel and kicks off a continuous Lyria RealTime generation session driven by the supplied prompt and musical parameters. This is the heavy entry point of the music subsystem;pause_music,resume_music, andsteer_musicoperate on the session it creates.The work fans out across several collaborators: the live
discord.Clientis obtained throughtools._discord_helpers.get_discord_client, prompts are parsed byservices.lyria_session.parse_prompts, and a Gemini API key is resolved per-user viatools.manage_api_keys.get_user_api_key(falling back to a shared default key that is rate-limited throughdefault_key_limit_applies/check_default_key_limit/increment_default_key_usage, all keyed in Redis viactx.redis). It then touches Discord voice state directly (connect / move_to / disconnect), builds agoogle.genai.types.LiveMusicGenerationConfig, and hands everything toctx.adapter.lyria_service(init_client,is_playing,start_session,stop) which streams audio into the channel. On any exception it stops the Lyria session and force-disconnects the voice client to avoid leaving a dangling connection. It is registered and dispatched dynamically bytool_loader.pyas the module-levelrunhandler; no in-repo callers invoke it directly outside of tests.- Parameters:
guild_id (
str) – Discord server (guild) ID as a string; must parse to an int.channel_id (
str) – Optional voice channel ID. When omitted, the first voice channel that currently has members is chosen.prompt (
str) – Music description inprompt:weightform; defaults to"Violin metal"whenNone.bpm (
int) – Beats per minute for the generation config.temperature (
float) – Sampling creativity passed to Lyria.brightness (
float) – Tonal brightness passed to Lyria.scale (
str) – Optional musical scale name resolved againsttypes.Scale.music_generation_mode (
str) – Optional mode name resolved againsttypes.MusicGenerationMode(QUALITY, DIVERSITY, VOCALIZATION).guidance (
float) – Optional prompt-adherence strength (validated 0.0-6.0).density (
float) – Optional note density (validated 0.0-1.0).mute_bass (
bool) – Optional flag to reduce bass output.mute_drums (
bool) – Optional flag to reduce drums output.only_bass_and_drums (
bool) – Optional flag to restrict output to bass and drums.top_k (
int) – Optional sampling parameter (validated 1-1000).seed (
int) – Optional random seed (validated 0-2147483647).ctx (
ToolContext|None) – The tool execution context. Must expose a non-Noneadapter;ctx.redisandctx.user_idare used for key resolution and rate limiting.
- Returns:
A human-readable confirmation describing the prompt, channel, bitrate, and guild tier on success, or an
Error:prefixed string for any validation, connection, or generation failure.- Return type: