File size: 7,362 Bytes
60d2967
 
96f3285
60d2967
 
 
636e642
96f3285
adfd0c7
f6dc26b
60d2967
 
96f3285
 
 
 
 
1bc04be
f6dc26b
60d2967
 
 
 
 
 
 
 
96f3285
 
 
 
 
 
 
 
60d2967
 
 
 
 
 
 
 
96f3285
 
60d2967
 
96f3285
60d2967
96f3285
 
1bc04be
932c444
 
 
 
 
 
 
 
 
 
 
 
 
96f3285
932c444
 
 
 
 
 
96f3285
932c444
96f3285
 
932c444
96f3285
932c444
96f3285
 
 
1bc04be
96f3285
 
636e642
96f3285
932c444
96f3285
 
932c444
 
 
 
 
1bc04be
932c444
1bc04be
932c444
96f3285
60d2967
932c444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d2967
 
 
 
 
036628e
77815e1
036628e
 
 
 
77815e1
036628e
60d2967
96f3285
 
77815e1
 
60d2967
96f3285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d2967
036628e
 
60d2967
 
036628e
 
 
60d2967
036628e
 
60d2967
 
 
 
 
036628e
 
 
60d2967
932c444
 
 
 
 
 
 
 
 
60d2967
 
 
f6dc26b
60d2967
 
 
 
 
f6dc26b
60d2967
 
 
f6dc26b
60d2967
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import discord
from discord.ext import commands
import gradio as gr
from dotenv import load_dotenv
import os
import threading
import asyncio
import yt_dlp

# Load environment variables
load_dotenv()

# Check if cookies.txt exists
if not os.path.exists('cookies.txt'):
    txt = os.getenv('COOKIES')
    with open('cookies.txt', 'w') as f:
        f.write(txt)

# Bot configuration
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

class MusicBot:
    def __init__(self):
        self.is_playing = False
        self.voice_client = None
        self.queue = []
        self.ydl_opts = {
            'format': 'bestaudio/best',
            'cookiefile': 'cookies.txt',
            'quiet': True,
            'no_warnings': True,
            'extract_flat': 'in_playlist'
        }

    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)
            return True
        return False

    async def play_next(self, ctx):
        if self.queue and not self.is_playing:
            self.is_playing = True
            url = self.queue.pop(0)
            
            try:
                ydl_opts = {
                    'format': 'bestaudio/best',
                    'cookiefile': 'cookies.txt',
                    'quiet': True,
                    'no_warnings': True,
                    'extract_flat': False,  # Changed this to False for proper extraction
                    'postprocessors': [{
                        'key': 'FFmpegExtractAudio',
                        'preferredcodec': 'opus',
                    }]
                }
                
                with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                    info = ydl.extract_info(url, download=False)
                    if 'entries' in info:  # It's a playlist
                        audio_url = info['entries'][0]['url']
                        title = info['entries'][0].get('title', 'Unknown title')
                    else:  # It's a single video
                        audio_url = info['url']
                        title = info.get('title', 'Unknown title')
                
                # Updated FFmpeg options for better streaming
                ffmpeg_options = {
                    'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
                    'options': '-vn -filter:a "volume=0.5"'
                }
                
                audio_source = discord.FFmpegPCMAudio(audio_url, **ffmpeg_options)
                
                def after_playing(error):
                    self.is_playing = False
                    if error:
                        print(f"Playback error: {error}")
                    asyncio.run_coroutine_threadsafe(self.play_next(ctx), bot.loop)

                self.voice_client.play(audio_source, after=after_playing)
                await ctx.send(f"Now playing: {title}")
                
            except yt_dlp.utils.DownloadError as e:
                print(f"YT-DLP Error for URL {url}: {e}")
                self.is_playing = False
                await ctx.send("Error playing the song: Could not extract audio stream")
                await self.play_next(ctx)
            except Exception as e:
                print(f"General Error playing URL {url}: {e}")
                self.is_playing = False
                await ctx.send("An unexpected error occurred while playing the song")
                await self.play_next(ctx)

    async def get_queue_info(self):
        if not self.queue:
            return "Queue is empty"
            
        try:
            queue_info = []
            with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
                for i, url in enumerate(self.queue, 1):
                    info = ydl.extract_info(url, download=False)
                    title = info.get('title', 'Unknown title')
                    queue_info.append(f"{i}. {title}")
            
            return "\n".join(queue_info)
        except Exception as e:
            return f"Error fetching queue info: {str(e)}"

music_bot = MusicBot()

@bot.event
async def on_ready():
    print(f'Bot is ready! Logged in as {bot.user}')
    print("Syncing commands...")
    try:
        await bot.tree.sync(guild=None)  # Set to None for global sync
        print("Successfully synced commands globally!")
    except discord.app_commands.errors.CommandSyncFailure as e:
        print(f"Failed to sync commands: {e}")
    except Exception as e:
        print(f"An error occurred while syncing commands: {e}")

@bot.tree.command(name="play", description="Play a YouTube URL")
async def play(interaction: discord.Interaction, url: str):
    await interaction.response.defer()
    ctx = await commands.Context.from_interaction(interaction)
    
    if not await music_bot.join_voice(ctx):
        await interaction.followup.send("You need to be in a voice channel!")
        return

    try:
        with yt_dlp.YoutubeDL(music_bot.ydl_opts) as ydl:
            info = ydl.extract_info(url, download=False)
            title = info.get('title', 'Unknown title')
        
        music_bot.queue.append(url)
        await interaction.followup.send(f'Added to queue: {title}')
        
        if not music_bot.is_playing:
            await music_bot.play_next(ctx)
    except Exception as e:
        await interaction.followup.send(f'Error: {str(e)}')

@bot.tree.command(name="skip", description="Skip the current song")
async def skip(interaction: discord.Interaction):
    if music_bot.voice_client:
        music_bot.voice_client.stop()
        await interaction.response.send_message('Skipped current song!')
    else:
        await interaction.response.send_message('No song is currently playing!')

@bot.tree.command(name="leave", description="Disconnect bot from voice channel")
async def leave(interaction: discord.Interaction):
    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 interaction.response.send_message('Bot disconnected!')
    else:
        await interaction.response.send_message('Bot is not in a voice channel!')

@bot.tree.command(name="queue", description="Show the current music queue")
async def queue(interaction: discord.Interaction):
    if music_bot.is_playing:
        await interaction.response.defer()
        queue_info = await music_bot.get_queue_info()
        await interaction.followup.send(f"**Current Queue:**\n{queue_info}")
    else:
        await interaction.response.send_message("No music is currently playing!")

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)