Py.Cafe

thiiz./

zyt

Streamlit on Py.cafe: Interactive Application Showcase

DocsPricing
  • app.py
  • requirements.txt
app.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import os
os.system('apt-get update')
os.system('apt-get install -y ffmpeg')
import discord
from discord.ext import commands
import yt_dlp
import asyncio
from typing import Optional, Dict, List
import re

DISCORD_TOKEN = os.environ.get('DISCORD_BOT_TOKEN')
COOKIES = os.environ.get('cookies')

DEFAULT_VOLUME = 0.3

YTDL_OPTIONS = {
    'format': 'bestaudio/best',
    'extractaudio': True,
    'audioformat': 'mp3',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': False,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'ytsearch',
    'source_address': '0.0.0.0',
    'prefer_ffmpeg': True,
    'keepvideo': False,
    'extract_flat': 'in_playlist',
    'playliststart': 1,
    'playlistend': 50,
    'cookiefile': COOKIES,
}

FFMPEG_OPTIONS = {
    'options': '-vn',
    'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
}

class Track:
    def __init__(self, video_id: str, title: str, duration: int, thumbnail: str):
        self.video_id = video_id
        self.title = title
        self.duration = duration
        self.thumbnail = thumbnail
        self.url = f"https://youtube.com/watch?v={video_id}"

    @staticmethod
    def format_duration(seconds: float) -> str:
        seconds = int(seconds)
        minutes, seconds = divmod(seconds, 60)
        hours, minutes = divmod(minutes, 60)
        if hours:
            return f"{hours}:{minutes:02d}:{seconds:02d}"
        return f"{minutes}:{seconds:02d}"

    def __str__(self) -> str:
        return f"[{self.title}]({self.url}) ({self.format_duration(self.duration)})"

class GuildQueue:
    def __init__(self):
        self.tracks: List[Track] = []
        self.current_track: Optional[Track] = None
        self.loop: bool = False
        self.text_channel = None
        self.volume: float = DEFAULT_VOLUME  # Volume padrão (30%)

class MusicBot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.message_content = True
        intents.voice_states = True
        super().__init__(command_prefix='!', intents=intents)
        self.ytdl = yt_dlp.YoutubeDL(YTDL_OPTIONS)
        
    async def setup_hook(self):
        await self.add_cog(Music(self))

class Music(commands.Cog):
    def __init__(self, bot: MusicBot):
        self.bot = bot
        self.guild_queues: Dict[int, GuildQueue] = {}
        
    def get_queue(self, guild_id: int) -> GuildQueue:
        if guild_id not in self.guild_queues:
            self.guild_queues[guild_id] = GuildQueue()
        return self.guild_queues[guild_id]

    async def ensure_voice_state(self, ctx: commands.Context) -> Optional[discord.VoiceClient]:
        if ctx.author.voice is None:
            embed = discord.Embed(
                title="❌ Erro de Conexão",
                description="Você precisa estar em um canal de voz para usar este comando.",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed, silent=True)
            return None
            
        voice_channel = ctx.author.voice.channel
        voice_client = ctx.voice_client
        
        if voice_client:
            if voice_client.channel != voice_channel:
                await voice_client.move_to(voice_channel)
        else:
            voice_client = await voice_channel.connect()
            
        return voice_client

    def extract_track_info(self, data: dict) -> Optional[Track]:
        try:
            video_id = data.get('id')
            if not video_id:
                return None

            title = data.get('title', 'Unknown Title')
            duration = data.get('duration', 0)
            thumbnail = data.get('thumbnail', '')

            return Track(
                video_id=video_id,
                title=title,
                duration=duration,
                thumbnail=thumbnail
            )
        except Exception as e:
            print(f"Erro ao extrair informações da faixa: {e}")
            return None

    async def play_next(self, guild: discord.Guild):
        queue = self.get_queue(guild.id)
        voice_client = guild.voice_client

        if not voice_client or not voice_client.is_connected():
            return

        if queue.loop and queue.current_track:
            queue.tracks.append(queue.current_track)

        if not queue.tracks:
            queue.current_track = None
            return

        track = queue.tracks.pop(0)
        queue.current_track = track

        if queue.text_channel:
            embed = discord.Embed(
                title="🎵 Começando a Tocar",
                description=f"[{track.title}]({track.url})\n⏱️ Duração: {Track.format_duration(track.duration)}",
                color=discord.Color.blue()
            )
            if track.thumbnail:
                embed.set_thumbnail(url=track.thumbnail)
            await queue.text_channel.send(embed=embed, silent=True)

        video_url = f"https://youtube.com/watch?v={track.video_id}"
        try:
            data = await self.bot.loop.run_in_executor(
                None, 
                lambda: self.bot.ytdl.extract_info(video_url, download=False)
            )
            if not data:
                embed = discord.Embed(
                    title="❌ Erro de Reprodução",
                    description=f"Não foi possível obter informações para [{track.title}]({track.url})",
                    color=discord.Color.red()
                )
                await queue.text_channel.send(embed=embed, silent=True)
                queue.current_track = None
                return

            formats = data.get('formats', [])
            audio_formats = [f for f in formats if f.get('acodec') != 'none' and f.get('vcodec') == 'none']
            if audio_formats:
                best_audio = max(audio_formats, key=lambda f: f.get('abr', 0) if f.get('abr') else 0)
                url = best_audio.get('url')
            else:
                url = data.get('url')

            if not url:
                embed = discord.Embed(
                    title="❌ Erro de Reprodução",
                    description=f"Nenhuma URL reproduzível encontrada para [{track.title}]({track.url})",
                    color=discord.Color.red()
                )
                await queue.text_channel.send(embed=embed, silent=True)
                queue.current_track = None
                return

            audio_source = discord.FFmpegPCMAudio(
                url,
                executable="ffmpeg",
                **FFMPEG_OPTIONS
            )
            source = discord.PCMVolumeTransformer(audio_source, volume=queue.volume)

            def after_playing(error):
                if error:
                    print(f'Erro no player (ignorado): {error}')
                asyncio.run_coroutine_threadsafe(self.play_next(guild), self.bot.loop)

            voice_client.play(source, after=after_playing)

        except Exception as e:
            embed = discord.Embed(
                title="❌ Erro de Reprodução",
                description=f"Erro ao reproduzir [{track.title}]({track.url})\n```{str(e)}```",
                color=discord.Color.red()
            )
            await queue.text_channel.send(embed=embed, silent=True)
            queue.current_track = None

    @commands.command(name="p", aliases=["play"])
    async def play(self, ctx: commands.Context, *, query: str):
        voice_client = await self.ensure_voice_state(ctx)
        if not voice_client:
            return

        queue = self.get_queue(ctx.guild.id)
        queue.text_channel = ctx.channel

        async with ctx.typing():
            is_url = bool(re.match(r'https?://(?:www\.)?.+', query))
            processed_query = query if is_url else f'ytsearch:{query}'

            try:
                data = await self.bot.loop.run_in_executor(
                    None, 
                    lambda: self.bot.ytdl.extract_info(processed_query, download=False)
                )

                if not data:
                    embed = discord.Embed(
                        title="❌ Erro na Busca",
                        description="Não foi possível encontrar a música solicitada.",
                        color=discord.Color.red()
                    )
                    await ctx.send(embed=embed, silent=True)
                    return

                # Se for playlist
                if data.get('_type') == 'playlist':
                    tracks = []
                    entries = data.get('entries', [])
                    for entry in entries:
                        if entry:
                            track = self.extract_track_info(entry)
                            if track:
                                tracks.append(track)
                    
                    if tracks:
                        queue.tracks.extend(tracks)
                        embed = discord.Embed(
                            title="📑 Playlist Adicionada",
                            description=f"✅ **{len(tracks)} músicas** adicionadas à fila\n\n🎵 **Primeira música:**\n[{tracks[0].title}]({tracks[0].url})\n⏱️ Duração: {Track.format_duration(tracks[0].duration)}",
                            color=discord.Color.green()
                        )
                        if tracks[0].thumbnail:
                            embed.set_thumbnail(url=tracks[0].thumbnail)
                        embed.set_footer(text=f"Use !q para ver a fila completa | Total na fila: {len(queue.tracks)}")
                        await ctx.send(embed=embed, silent=True)

                else:
                    track = self.extract_track_info(data)
                    if track:
                        queue.tracks.append(track)
                        position = len(queue.tracks)
                        
                        embed = discord.Embed(
                            title="🎵 Música Adicionada",
                            description=f"✅ Adicionada à fila:\n[{track.title}]({track.url})\n\n⏱️ Duração: {Track.format_duration(track.duration)}\n📍 Posição na fila: {position}",
                            color=discord.Color.green()
                        )
                        if track.thumbnail:
                            embed.set_thumbnail(url=track.thumbnail)
                        embed.set_footer(text="Use !q para ver a fila completa")
                        await ctx.send(embed=embed, silent=True)

            except Exception as e:
                embed = discord.Embed(
                    title="❌ Erro",
                    description=f"Ocorreu um erro ao processar sua solicitação:\n```{str(e)}```",
                    color=discord.Color.red()
                )
                await ctx.send(embed=embed, silent=True)
                return

        if not voice_client.is_playing() and not voice_client.is_paused():
            await self.play_next(ctx.guild)

    @commands.command(name="q", aliases=["queue"])
    async def show_queue(self, ctx: commands.Context):
        queue = self.get_queue(ctx.guild.id)
        
        if not queue.current_track and not queue.tracks:
            embed = discord.Embed(
                title="📭 Fila Vazia",
                description="Nenhuma música na fila. Use !play para adicionar músicas!",
                color=discord.Color.light_grey()
            )
            await ctx.send(embed=embed, silent=True)
            return

        embed = discord.Embed(
            title="🎶 Fila de Reprodução",
            color=discord.Color.blue()
        )

        if queue.current_track:
            embed.add_field(
                name="🔊 Tocando Agora",
                value=f"[{queue.current_track.title}]({queue.current_track.url})\n⏱️ {Track.format_duration(queue.current_track.duration)}",
                inline=False
            )

        if queue.tracks:
            track_list = []
            for i, track in enumerate(queue.tracks[:10], 1):
                track_list.append(
                    f"`{i}.` [{track.title}]({track.url})\n⏱️ {Track.format_duration(track.duration)}"
                )
            
            remaining = len(queue.tracks) - 10 if len(queue.tracks) > 10 else 0
            
            tracks_text = "\n\n".join(track_list)
            if remaining > 0:
                tracks_text += f"\n\n*...e mais {remaining} músicas na fila*"
            
            embed.add_field(
                name="📋 Próximas Músicas",
                value=tracks_text,
                inline=False
            )

        total_duration = sum(track.duration for track in queue.tracks)
        if queue.current_track:
            total_duration += queue.current_track.duration

        status = []
        status.append(f"🔄 Loop: {'Ativado' if queue.loop else 'Desativado'}")
        status.append(f"🔊 Volume: {int(queue.volume * 100)}%")
        status.append(f"⏱️ Tempo total: {Track.format_duration(total_duration)}")
        
        embed.set_footer(text=" | ".join(status))
        await ctx.send(embed=embed, silent=True)

    @commands.command(name="loop")
    async def toggle_loop(self, ctx: commands.Context):
        queue = self.get_queue(ctx.guild.id)
        queue.loop = not queue.loop
        
        embed = discord.Embed(
            title="🔄 Modo Loop",
            description=f"Loop {'ativado' if queue.loop else 'desativado'} com sucesso!",
            color=discord.Color.blue()
        )
        await ctx.send(embed=embed, silent=True)

    @commands.command(name="clear")
    async def clear_queue(self, ctx: commands.Context):
        queue = self.get_queue(ctx.guild.id)
        old_length = len(queue.tracks)
        queue.tracks.clear()
        
        embed = discord.Embed(
            title="🗑️ Fila Limpa",
            description=f"Foram removidas {old_length} músicas da fila",
            color=discord.Color.blue()
        )
        await ctx.send(embed=embed, silent=True)

    @commands.command(name="s", aliases=["skip"])
    async def skip(self, ctx: commands.Context):
        voice_client = ctx.voice_client
        if voice_client and voice_client.is_playing():
            current_track = self.get_queue(ctx.guild.id).current_track
            voice_client.stop()
            
            embed = discord.Embed(
                title="⏭️ Música Pulada",
                description=f"Pulando [{current_track.title}]({current_track.url})",
                color=discord.Color.blue()
            )
            await ctx.send(embed=embed, silent=True)
        else:
            embed = discord.Embed(
                title="❌ Erro ao Pular",
                description="Não há nenhuma música tocando no momento",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed, silent=True)

    @commands.command()
    async def pause(self, ctx: commands.Context):
        voice_client = ctx.voice_client
        if voice_client and voice_client.is_playing():
            voice_client.pause()
            current_track = self.get_queue(ctx.guild.id).current_track
            
            embed = discord.Embed(
                title="⏸️ Música Pausada",
                description=f"[{current_track.title}]({current_track.url}) foi pausada",
                color=discord.Color.blue()
            )
            await ctx.send(embed=embed, silent=True)
        else:
            embed = discord.Embed(
                title="❌ Erro ao Pausar",
                description="Não há nenhuma música tocando no momento",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed, silent=True)

    @commands.command()
    async def resume(self, ctx: commands.Context):
        voice_client = ctx.voice_client
        if voice_client and voice_client.is_paused():
            voice_client.resume()
            current_track = self.get_queue(ctx.guild.id).current_track
            
            embed = discord.Embed(
                title="▶️ Música Retomada",
                description=f"[{current_track.title}]({current_track.url}) foi retomada",
                color=discord.Color.blue()
            )
            await ctx.send(embed=embed, silent=True)
        else:
            embed = discord.Embed(
                title="❌ Erro ao Retomar",
                description="Não há nenhuma música pausada no momento",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed, silent=True)

    @commands.command()
    async def volume(self, ctx: commands.Context, volume: int):
        """Define o volume (0-100)"""
        if not 0 <= volume <= 100:
            embed = discord.Embed(
                title="❌ Erro no Volume",
                description="O volume deve estar entre 0 e 100",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed, silent=True)
            return

        queue = self.get_queue(ctx.guild.id)
        old_volume = int(queue.volume * 100)
        queue.volume = volume / 100

        voice_client = ctx.voice_client
        if voice_client and voice_client.source:
            voice_client.source.volume = queue.volume
            embed = discord.Embed(
                title="🔊 Volume Ajustado",
                description=f"Volume alterado: **{old_volume}%** → **{volume}%**",
                color=discord.Color.blue()
            )
        else:
            embed = discord.Embed(
                title="🔊 Volume Configurado",
                description=f"Volume será **{volume}%** na próxima música",
                color=discord.Color.blue()
            )
        
        await ctx.send(embed=embed, silent=True)

    @commands.command()
    async def leave(self, ctx: commands.Context):
        voice_client = ctx.voice_client
        if voice_client:
            queue = self.get_queue(ctx.guild.id)
            tracks_count = len(queue.tracks)
            
            if ctx.guild.id in self.guild_queues:
                del self.guild_queues[ctx.guild.id]
            
            await voice_client.disconnect()
            
            embed = discord.Embed(
                title="👋 Desconectado",
                description=f"Saí do canal de voz e limpei a fila com {tracks_count} músicas",
                color=discord.Color.blue()
            )
            await ctx.send(embed=embed, silent=True)
        else:
            embed = discord.Embed(
                title="❌ Erro ao Sair",
                description="Não estou em nenhum canal de voz",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed)

    @commands.command(name="np", aliases=["now", "playing"])
    async def now_playing(self, ctx: commands.Context):
        """Mostra informações sobre a música atual"""
        queue = self.get_queue(ctx.guild.id)
        
        if not queue.current_track:
            embed = discord.Embed(
                title="❌ Nada Tocando",
                description="Não há nenhuma música tocando no momento",
                color=discord.Color.red()
            )
            await ctx.send(embed=embed, silent=True)
            return
            
        embed = discord.Embed(
            title="🎵 Tocando Agora",
            description=f"[{queue.current_track.title}]({queue.current_track.url})\n⏱️ Duração: {Track.format_duration(queue.current_track.duration)}",
            color=discord.Color.blue()
        )
        
        if queue.current_track.thumbnail:
            embed.set_thumbnail(url=queue.current_track.thumbnail)
            
        status = []
        status.append(f"🔄 Loop: {'Ativado' if queue.loop else 'Desativado'}")
        status.append(f"🔊 Volume: {int(queue.volume * 100)}%")
        
        embed.set_footer(text=" | ".join(status))
        await ctx.send(embed=embed, silent=True)

bot = MusicBot()
bot.remove_command("help")

@bot.event
async def on_ready():
    print(f'Bot está pronto como {bot.user.name}')
    await bot.change_presence(activity=discord.Activity(
        type=discord.ActivityType.listening,
        name="!play | !help"
    ))
    print('------')

@bot.command(name="help")
async def help_command(ctx):
    embed = discord.Embed(
        title="📜 Lista de Comandos",
        description="Aqui estão todos os comandos disponíveis:",
        color=discord.Color.blue()
    )
    
    commands_list = {
        "🎵 Música": {
            "!p ou !play": "Toca uma música ou playlist (!p <nome ou URL>)",
            "!s ou !skip": "Pula a música atual",
            "!q ou !queue": "Mostra a fila de reprodução",
            "!np": "Mostra a música atual",
            "!pause": "Pausa a música",
            "!resume": "Retoma a música pausada",
            "!loop": "Ativa/desativa o modo loop",
            "!clear": "Limpa a fila de reprodução"
        },
        "⚙️ Configurações": {
            "!volume": "Ajusta o volume (0-100)",
            "!leave": "Desconecta o bot do canal"
        }
    }
    
    for category, cmds in commands_list.items():
        command_text = "\n".join(f"`{cmd}`: {desc}" for cmd, desc in cmds.items())
        embed.add_field(name=category, value=command_text, inline=False)
        
    embed.set_footer(text="🎵 Divirta-se com a música!")
    await ctx.send(embed=embed, silent=True)

bot.run(DISCORD_TOKEN)