Spaces:
Runtime error
Runtime error
import discord | |
from discord.ext import commands | |
import yt_dlp # Changed from youtube_dl | |
import asyncio | |
import gradio as gr | |
from dotenv import load_dotenv | |
import os | |
import threading | |
# Load environment variables | |
load_dotenv() | |
# Bot configuration | |
intents = discord.Intents.default() | |
intents.message_content = True | |
bot = commands.Bot(command_prefix='!', intents=intents) | |
# YouTube DL options | |
ydl_opts = { | |
'format': 'bestaudio/best', | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'mp3', | |
'preferredquality': '192', | |
}], | |
'noplaylist': True, | |
'nocheckcertificate': True, | |
'ignoreerrors': False, | |
'logtostderr': False, | |
'quiet': True, | |
'no_warnings': True, | |
'default_search': 'auto', | |
'source_address': '0.0.0.0' | |
} | |
# Music bot class | |
class MusicBot: | |
def __init__(self): | |
self.queue = [] | |
self.is_playing = False | |
self.voice_client = None | |
async def join_voice(self, ctx): | |
if ctx.author.voice: | |
channel = ctx.author.voice.channel | |
if self.voice_client is None: | |
self.voice_client = await channel.connect() | |
else: | |
await self.voice_client.move_to(channel) | |
else: | |
await ctx.send("You need to be in a voice channel!") | |
async def play_next(self, ctx): | |
if len(self.queue) > 0: | |
self.is_playing = True | |
url = self.queue.pop(0) | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: # Changed from youtube_dl | |
info = ydl.extract_info(url, download=False) | |
if 'entries' in info: | |
url2 = info['entries'][0]['url'] | |
else: | |
url2 = info['url'] | |
self.voice_client.play(discord.FFmpegPCMAudio(url2, **{'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'}), | |
after=lambda e: asyncio.run_coroutine_threadsafe(self.play_next(ctx), bot.loop)) | |
else: | |
self.is_playing = False | |
music_bot = MusicBot() | |
async def on_ready(): | |
print(f'Bot is ready! Logged in as {bot.user}') | |
async def play(ctx, url): | |
await music_bot.join_voice(ctx) | |
music_bot.queue.append(url) | |
if not music_bot.is_playing: | |
await music_bot.play_next(ctx) | |
await ctx.send('Playing music!') | |
else: | |
await ctx.send('Added to queue!') | |
async def skip(ctx): | |
if music_bot.voice_client: | |
music_bot.voice_client.stop() | |
await ctx.send('Skipped current song!') | |
async def leave(ctx): | |
if music_bot.voice_client: | |
await music_bot.voice_client.disconnect() | |
music_bot.voice_client = None | |
music_bot.queue = [] | |
music_bot.is_playing = False | |
await ctx.send('Bot disconnected!') | |
def run_discord_bot(): | |
bot.run(os.getenv('DISCORD_TOKEN')) | |
# Create the Gradio interface | |
with gr.Blocks() as iface: | |
gr.Markdown("# Discord Music Bot Control Panel") | |
gr.Markdown("Bot is running in background") | |
if __name__ == "__main__": | |
# Start the Discord bot in a separate thread | |
bot_thread = threading.Thread(target=run_discord_bot, daemon=True) | |
bot_thread.start() | |
# Run Gradio interface in the main thread | |
iface.launch(debug=True) |