-
Notifications
You must be signed in to change notification settings - Fork 0
/
Music.py
152 lines (121 loc) · 6.38 KB
/
Music.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
import yt_dlp as youtube_dl
from Buttons import *
import re
# Contenedor de mensajes generados por el comando +play
music_messages = []
# Sistema para reproducir cancion desde youtube usando el comando +play
@bot.command(name='play', help='Reproducir canciones mediante link o nombre de la canción y artista: +play')
async def play(ctx, *, url):
global queue, current_song, vc_global, ctx_global, search_result_global, song_position, music_messages
vc_global = ctx.voice_client
ctx_global = ctx
if not hasattr(ctx, 'music') or not ctx.music:
ctx.music = MusicContext(ctx)
voice_channel = ctx.author.voice
if not voice_channel:
await ctx.send("Necesitas unirte a un canal de voz para usar este comando.")
return
vc = vc_global
if vc:
if vc.is_connected():
if vc.channel.id != voice_channel.channel.id:
await ctx.send(f"Necesitas estar en el canal {vc.channel} para reproducir música.")
return
else:
vc = await voice_channel.channel.connect()
else:
vc = await voice_channel.channel.connect()
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': 'best',
}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
if 'artist:' in url:
search_result_global = ydl.extract_info(f"ytsearch:{url}", download=False)
else:
search_result_global = ydl.extract_info(f"ytsearch1:{url}", download=False)
except youtube_dl.utils.DownloadError as e:
await ctx.send("Lo siento, no pude obtener la información de la canción.")
return
video_url = search_result_global['entries'][0]['url']
if vc.is_paused() or vc.is_playing():
await queue.put({"url": video_url, "info": search_result_global["entries"][0]})
queue_size = queue.qsize()
song_position = ctx.music.total_played + queue_size
embed = discord.Embed(title=f'Canción en cola - Posición {song_position}', description=search_result_global["entries"][0]["title"], color=discord.Color.red())
ctx.music.total_played += 1
await ctx.send(embed=embed)
return
else:
current_song = search_result_global["entries"][0]
ffmpeg_options = {
'options': '-vn',
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
}
vc.play(discord.FFmpegPCMAudio(video_url, executable="C:/ffmpeg/bin/ffmpeg.exe", **ffmpeg_options), after=lambda e: bot.loop.create_task(play_next_song(ctx, view)))
vc.source = discord.PCMVolumeTransformer(vc.source)
vc.source.volume = 1.0
embed = discord.Embed(title='🎶 Reproduciendo', description=current_song["title"], color=random_color())
embed.set_thumbnail(url=current_song["thumbnail"])
title = current_song["title"]
webpage_url = current_song["webpage_url"]
song_name = re.split(r'-|\(|\[', title, 1)[1].strip() if "-" in title else title
video_link = f"[{song_name}]({webpage_url})"
queue_size = queue.qsize() # Esto mostrará la cantidad de canciones que hay en la cola.
embed.description = f"**Canción:** {video_link}\n**Cola:** {queue_size}"
user = ctx.author
user_name = str(user)
user_avatar = user.avatar.url
embed.set_footer(text=f"Solicitado por: {user_name}", icon_url=user_avatar)
view = MusicView()
view.pause_button.set_callback(view.pause_resume)
view.stop_button.set_callback(lambda interaction: view.stop(interaction, ctx_global))
view.queue_button.set_callback(lambda interaction: view.show_queue(interaction, current_song))
view.skip_button.set_callback(view.skip)
view.pause_button.custom_id = "pause"
message = await ctx.send(embed=embed, view=view)
music_messages.append(message)
#Lógica para reproducir la siguiente canción.
async def play_next_song(ctx, view):
global queue, current_song, vc_global, ctx_global, search_result_global, music_messages, song_position, current_song_info
if not vc_global or not vc_global.is_connected():
return
try:
if queue.qsize() > 0:
next_song = await queue.get()
next_song_url = next_song["url"]
current_song = next_song["info"]
vc_global.stop()
ffmpeg_options = {
'options': '-vn',
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
}
for song in search_result_global["entries"]:
if song['url'] == next_song_url:
current_song = song
break
vc_global.play(discord.FFmpegPCMAudio(next_song_url, executable="C:/ffmpeg/bin/ffmpeg.exe", **ffmpeg_options), after=lambda e: bot.loop.create_task(play_next_song(ctx, view)))
vc_global.source = discord.PCMVolumeTransformer(vc_global.source)
vc_global.source.volume = 1.0
embed = discord.Embed(title='🎶 Reproduciendo', description=current_song["title"], color=random_color())
embed.set_thumbnail(url=current_song["thumbnail"])
title = current_song["title"]
webpage_url = current_song["webpage_url"]
song_name = re.split(r'-|\(|\[', title, 1)[1].strip() if "-" in title else title
video_link = f"[{song_name}]({webpage_url})"
queue_size = queue.qsize()
embed.description = f"**Canción:** {video_link}\n**Cola:** {queue_size}"
user = ctx_global.author
user_name = str(user)
user_avatar = user.avatar.url
embed.set_footer(text=f"Solicitado por: {user_name}", icon_url=user_avatar)
message = await ctx_global.send(embed=embed, view=view)
music_messages.append(message)
except discord.errors.InteractionFailed as e:
pass