A Python library for streaming music in Discord voice channels with YouTube support
Project description
Discord Music Streamer
A Python library for streaming music in Discord voice channels with YouTube support and queue management.
Features
- 🎵 Stream music from YouTube
- 📋 Queue management (add, remove, shuffle)
- ⏯️ Basic controls (play, pause, resume, stop, skip)
- 🔍 YouTube search functionality
- 🤖 Pre-built Discord.py commands
- 📦 Easy to integrate into existing bots
Installation
pip install discord-music-streamer
Quick Start
Basic Usage
import discord
from discord.ext import commands
from discord_music_streamer import MusicPlayer
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
music_player = MusicPlayer(bot)
@bot.command()
async def play(ctx, *, query):
result = await music_player.play(ctx, query)
await ctx.send(result)
@bot.command()
async def pause(ctx):
result = music_player.pause(ctx)
await ctx.send(result)
@bot.command()
async def resume(ctx):
result = music_player.resume(ctx)
await ctx.send(result)
bot.run('YOUR_BOT_TOKEN')
Using Pre-built Commands
import discord
from discord.ext import commands
from discord_music_streamer.commands import MusicCommands
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
# Add all music commands at once
bot.add_cog(MusicCommands(bot))
bot.run('YOUR_BOT_TOKEN')
API Reference
MusicPlayer
The main class for handling music playback.
Methods
play(ctx, query)- Play a song from YouTubepause(ctx)- Pause the current songresume(ctx)- Resume the current songstop(ctx)- Stop playback and clear queueskip(ctx)- Skip the current songget_queue(ctx)- Get the current queuedisconnect(ctx)- Disconnect from voice channelnow_playing(ctx)- Get currently playing song info
QueueManager
Manages music queues for different guilds.
Methods
add_to_queue(guild_id, source)- Add a song to the queueget_next(guild_id)- Get the next song from the queueclear_queue(guild_id)- Clear the queueshuffle_queue(guild_id)- Shuffle the queuequeue_size(guild_id)- Get queue size
YouTubeSource
Handles YouTube audio sources.
Methods
from_query(query)- Create source from search query or URLsearch_youtube(query, max_results=5)- Search YouTube videos
Requirements
- Python 3.8+
- discord.py[voice] >= 2.0.0
- yt-dlp >= 2023.7.6
- PyNaCl >= 1.5.0
- FFmpeg (must be installed on system)
Installation Notes
Make sure you have FFmpeg installed on your system:
Windows: Download from https://ffmpeg.org/download.html
macOS:
brew install ffmpeg
Linux:
sudo apt update
sudo apt install ffmpeg
Example Bot
import discord
from discord.ext import commands
from discord_music_streamer import MusicPlayer
# Bot setup
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Initialize music player
music_player = MusicPlayer(bot)
@bot.event
async def on_ready():
print(f'{bot.user} has logged in!')
@bot.command()
async def play(ctx, *, query):
"""Play a song from YouTube"""
try:
result = await music_player.play(ctx, query)
await ctx.send(result)
except Exception as e:
await ctx.send(f"Error: {str(e)}")
@bot.command()
async def queue(ctx):
"""Show current queue"""
queue = music_player.get_queue(ctx)
if queue:
queue_text = "\n".join([f"{i+1}. {title}" for i, title in enumerate(queue)])
await ctx.send(f"Current queue:\n```{queue_text}```")
else:
await ctx.send("Queue is empty")
# Add more commands as needed...
bot.run('YOUR_BOT_TOKEN')
License
MIT License - see LICENSE file for details.
Contributing
Pull requests are welcome! Please feel free to submit issues and enhancement requests.
Support
If you encounter any issues, please check the GitHub issues page or create a new issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file discord_music_streamer-0.1.1.tar.gz.
File metadata
- Download URL: discord_music_streamer-0.1.1.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b5add434f501aae7d7f75ece554d2d6e4dd694eab64c9e6a5c33acd76238a6d
|
|
| MD5 |
7bf027f47a40e9bcc37b65ae0569fd1c
|
|
| BLAKE2b-256 |
fc9dec9648bef7a1fc06e2c1bbb219376f09fc706a41fd84b256ca04fdc86022
|
File details
Details for the file discord_music_streamer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: discord_music_streamer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d14c68e7d90d3482958714fcfe095613a25638b0d4cac85f18cbc9f4a70a0e1f
|
|
| MD5 |
d54d815ec2dd5fb3ef54292064906a8e
|
|
| BLAKE2b-256 |
0f800e8b7664958430ba0caeb3cc5f211eda4c8ec8001e6b2e273d2b337f4a63
|