-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot_music.py
556 lines (452 loc) · 19.5 KB
/
bot_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
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
import discord
import asyncio
import youtube_dl
import random
import re
import os
import logging
from apiclient import discovery
from discord.ext import commands
from discord.utils import get
from googleapiclient.errors import HttpError
import settings
logger = logging.getLogger('discord')
logger.setLevel(logging.ERROR)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# (https|http)://www\.youtube\.com/(.+)\?(v|list)=(.+)&?(list=)?(.+)?
youtube_api_key = settings.youtube_api_key()
discord_token = settings.bot_token()
prefix = settings.prefix()
client = commands.Bot(command_prefix=prefix)
queue = []
queue_shuffled = []
song = {}
repeat_mode = 'off'
shuffle_mode = False
song_downloaded = False
playing_now_message = None
song_download_error_message = None
# queue_shuffled = random.shuffle(queue)
@client.event
async def on_ready():
print('Bot online')
async def PlaySong(ctx):
global playing_now_message, song_download_error_message
global song_downloaded, song
global queue, queue_shuffled
async def VerifyQueue(ctx):
# Coloca a música no final da fila caso esteja repetindo todas as músicas.
if repeat_mode == 'all' and song:
queue.append(song)
if shuffle_mode:
queue_shuffled.append(song)
await PlaySong(ctx)
# Passa o 'ctx' para 'context' para quando for tocar novamente.
context = ctx
def PlayAgain(error):
nonlocal context
coro = VerifyQueue(context)
fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
try:
fut.result()
except:
pass
try:
channel = ctx.message.author.voice.channel
except AttributeError as error:
print(error)
await ctx.send("Você não está conectado em nenhum canal de voz.")
return
voice = get(client.voice_clients, guild=ctx.guild)
if voice is not None:
if voice.channel != channel:
await voice.move_to(channel)
try:
await channel.connect()
except:
pass
voice = get(client.voice_clients, guild=ctx.guild)
if voice:
if not len(queue) == 0 or repeat_mode == 'single' or repeat_mode == 'all':
if not (voice.is_playing() or voice.is_paused()):
if not repeat_mode == 'single' or not song_downloaded:
if not shuffle_mode:
song = queue.pop(0)
else:
song = queue_shuffled.pop(0)
try:
queue.remove(song)
except ValueError:
pass
# Define as opções de músicas
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
# Faz o download da música
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading song now\n")
try:
ydl.download([song['url']])
except Exception as error:
print(error)
song_download_error_message = await ctx.send(
f"Ocorreu um problema na tentativa de tocar **{song['title']}**\n"
"*Tocando próxima música.*")
await PlaySong(ctx)
# Remove a música antiga
old_song = os.path.isfile("song.mp3")
if old_song:
os.remove("song.mp3")
# Renomeia o nome do arquivo .mp3
for file in os.listdir("./"):
if file.endswith('.mp3'):
name = file
print(f'Renamed File: {file}\n')
os.rename(file, "song.mp3")
song_downloaded = True
# Toca a música utilizando o bot no discord
voice.play(discord.FFmpegPCMAudio('song.mp3'), after=PlayAgain)
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.25
embed = discord.Embed(color=ctx.guild.me.top_role.color, title="**Playing Now**",
description=f"[{song['title']}]({song['url']}) [{song['user'].mention}]")
try:
await playing_now_message.delete()
except:
pass
try:
await song_download_error_message.delete()
except:
pass
await asyncio.sleep(1)
playing_now_message = await ctx.channel.send(embed=embed)
print("playing\n")
else:
try:
await playing_now_message.delete()
except:
pass
await asyncio.sleep(1)
await ctx.send("Acabou as músicas da fila. Utilize `.play <url ou nome da musica>` para tocar mais.")
def GetIdAndTitle(url):
get_title_id = os.popen(f'youtube-dl --default-search "ytsearch" --skip-download --get-title --get-id "{url}"') \
.read().split(sep='\n')
return get_title_id
@client.command()
async def play(ctx, *, search: str):
video = {'items': []}
playlist = False
try:
channel = ctx.message.author.voice.channel
except AttributeError as error:
print(error)
await ctx.send("Você não está conectado em nenhum canal de voz.")
return
if search.startswith('https://'):
link = re.match(r"^https://www\.youtube\.com/(watch|playlist)\?(v|list)=([0-9A-Za-z_-]+).*$", search)
if link is not None:
link = list(link.groups())
if link[0] == 'watch':
video = GetIdAndTitle(link[2])
youtube_video_title = video[0]
youtube_video_url = f'https://www.youtube.com/watch?v={video[1]}'
elif link[0] == 'playlist':
playlist = True
loading = await ctx.send("Baixando dados da playlist.")
try:
youtube = discovery.build('youtube', 'v3', developerKey=youtube_api_key)
playlistitems_list_request = youtube.playlistItems().list(
playlistId=link[2],
part='snippet',
maxResults=50)
while playlistitems_list_request:
await asyncio.sleep(1.5)
playlistitems_list_response = playlistitems_list_request.execute()
# Print information about each video.
for song in playlistitems_list_response['items']:
video['items'].append(song)
# Faz uma próxima request da próxima página da playlist. Retorna None caso não tenha.
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request,
previous_response=playlistitems_list_response)
await loading.delete()
except:
await loading.delete()
await ctx.send("Algum problema ocorreu enquanto tentei pegar as músicas.\n"
"Verifique se a URL está correta ou se existe algum vídeo indisponível "
"dentro da playlist.")
return
else:
await ctx.send("Esse link não é válido. Tente novamente.")
return
else:
# outra opção como youtube.be\id-música
return
else:
try:
youtube = discovery.build('youtube', 'v3', developerKey=youtube_api_key)
request = youtube.search().list(q=search, part='snippet', type='video', maxResults=5)
response = request.execute()
msg = '**Selecione uma faixa clicando no emoji correspondente.**\n'
show_information = await ctx.send(f'Mostrando músicas com o termo `{search}`\n\n')
for x in range(0, len(response['items'])):
# Pega a 'duração' do vídeo
video = youtube.videos().list(id=response['items'][x]['id']['videoId'], part='contentDetails').execute()
duration = list(
re.match(r'^PT(\d+H)?(\d+M)?(\d+S)?$', video['items'][0]['contentDetails']['duration']).groups())
# Transforma: PT#H#M#S em H:M:S, por exemplo: PT4M2S -> 00:04:02
if duration[0] is None:
duration[0] = ''
else:
duration[0] = duration[0].replace('H', ':')
if len(duration[0]) < 3:
duration[0] = '0' + duration[0]
if duration[1] is None:
duration[1] = '00:'
else:
duration[1] = duration[1].replace('M', ':')
if len(duration[1]) < 3:
duration[1] = '0' + duration[1]
if duration[2] is None:
duration[2] = '00'
else:
duration[2] = duration[2].replace('S', '')
if len(duration[2]) < 2:
duration[2] = '0' + duration[2]
video_duration = duration[0] + duration[1] + duration[2]
video_title = response['items'][x]['snippet']['title']
msg = msg + f"**{(x + 1)})** {video_title} - ({video_duration})\n"
message = await ctx.send(msg)
emoji = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣']
await message.add_reaction(emoji[0])
await message.add_reaction(emoji[1])
await message.add_reaction(emoji[2])
await message.add_reaction(emoji[3])
await message.add_reaction(emoji[4])
def check(reaction, user):
return user == ctx.author and (str(reaction.emoji) == emoji[0]
or str(reaction.emoji) == emoji[1]
or str(reaction.emoji) == emoji[2]
or str(reaction.emoji) == emoji[3]
or str(reaction.emoji) == emoji[4])
try:
reaction, user = await client.wait_for('reaction_add', timeout=60, check=check)
await show_information.delete()
await message.delete()
for x in range(0, len(emoji)):
if str(reaction.emoji) == emoji[x]:
youtube_video_title = response['items'][x]['snippet']['title']
youtube_video_url = f'https://www.youtube.com/watch?v={response["items"][x]["id"]["videoId"]}'
break
except asyncio.TimeoutError:
error = await ctx.send("Você demorou muito.")
await error.delete(delay=5)
await show_information.delete()
return
except HttpError:
await ctx.send("O limite de requisições foi atingido. Não é possível procurar por uma música usando Youtube"
"Data API.")
return
# try:
# get_title_id = os.popen(f'youtube-dl --default-search "ytsearch" --skip-download --get-title --get-id "{search}"') \
# .read().split(sep='\n')
#
# youtube_video_title = get_title_id[0]
# youtube_video_url = f'https://www.youtube.com/watch?v={get_title_id[1]}'
# except:
# await ctx.send(f"Não foi possível achar a música com o nome: `{search}`")
# return
# get all the information from the video using the url
# video = youtube_dl.YoutubeDL({}).extract_info(youtube_video_url, download=False)
if not playlist:
queue.append({'title': youtube_video_title, 'url': youtube_video_url, 'user': ctx.author})
embed = discord.Embed(color=ctx.guild.me.top_role.color,
title='**Adicionado a Fila**',
description=f"[{youtube_video_title}]({youtube_video_url}) [{ctx.author.mention}]")
else:
for song in video['items']:
youtube_video_url = f"https://www.youtube.com/watch?v={song['snippet']['resourceId']['videoId']}"
youtube_video_title = song['snippet']['title']
queue.append({'title': youtube_video_title, 'url': youtube_video_url, 'user': ctx.author})
embed = discord.Embed(color=ctx.guild.me.top_role.color,
title='**Adicionado a Fila**',
description=f"`{len(video['items'])}` músicas da playlist. [{ctx.author.mention}]")
await ctx.send(embed=embed)
print("Added to queue\n")
await PlaySong(ctx)
@client.command(name='queue', aliases=['q'])
async def _queue(ctx, page=1):
global queue_shuffled, queue, song
msg = ''
if len(queue) > 0 or song:
if page == 0:
page = 1
elif page < 0:
await ctx.send("As páginas só podem ser mostradas com números positivos!")
return
tracks_number = 10 # Quantas músicas aparecerão por página
max_pages = len(queue) // tracks_number
if len(queue) % tracks_number > 0:
max_pages = max_pages + 1
if page > max_pages:
page = max_pages
if max_pages == 0:
max_pages = 1
if len(queue) == 0:
page = 1
if not shuffle_mode:
queue_list = queue[tracks_number * (page - 1):tracks_number * page]
else:
queue_list = queue_shuffled[tracks_number * (page - 1):tracks_number * page]
msg = 'Mostrando playlist embaralhada\n'
if repeat_mode == 'off':
msg = msg + '\n\n'
elif repeat_mode == 'single':
msg = msg + 'Repetindo a faixa atual\n\n\n'
elif repeat_mode == 'all':
msg = msg + 'Repetindo a fila atual\n\n\n'
msg = msg + f'Página **{page}** de **{max_pages}**\n\n'
voice = get(client.voice_clients, guild=ctx.guild)
i = tracks_number * (page - 1)
if page == 1:
if voice and voice.is_paused():
msg = msg + f'**⏸ {song["title"]}** - *[@{song["user"].name}]*\n'
else:
msg = msg + f'**▶ {song["title"]}** - *[@{song["user"].name}]*\n'
for track in queue_list:
i = i + 1
msg = msg + f'`[{i}]` **{track["title"]}** - *[@{track["user"].name}]*\n'
await ctx.send(msg)
@client.command()
async def pause(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
print("Music paused")
voice.pause()
await ctx.send("A música foi pausada.")
else:
if voice and voice.is_paused():
await ctx.send("O player já está pausado")
else:
print("Nenhuma música está sendo tocada")
await ctx.send("Nenhuma música está sendo tocada, falha em pausar player.")
@client.command(aliases=['unpause'])
async def resume(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_paused():
print("Resumed Music")
voice.resume()
await ctx.send("O player foi despausado, voltando a tocar.")
else:
if voice and voice.is_playing():
print("Music is not paused")
await ctx.send("A música não está pausada, falha em despausar.")
else:
await ctx.send("Nenhuma música está sendo tocada, falha em despausar player.")
@client.command()
async def repeat(ctx, arg):
global repeat_mode
if arg == 'single':
await ctx.send('Repetindo a faixa atual.')
repeat_mode = 'single'
elif arg == 'all':
await ctx.send('Repetindo todas as faixas.')
repeat_mode = 'all'
elif arg == 'off':
await ctx.send('Desligando repetição.')
repeat_mode = False
else:
await ctx.send('Use `single | off` para ativar/desativar a repetição.')
@client.command()
async def shuffle(ctx):
global shuffle_mode
global queue_shuffled
if len(queue) > 0 or song:
if not shuffle_mode:
shuffle_mode = True
queue_shuffled = random.sample(queue, len(queue))
await ctx.send("O reprodutor agora está em modo aleatório.")
else:
shuffle_mode = False
queue_shuffled.clear()
await ctx.send("O reprodutor não está mais em modo aleatório.")
else:
await ctx.send("Não há músicas na fila para ser embaralhada.")
@client.command(aliases=['next'])
async def _next(ctx):
global song_downloaded
voice = get(client.voice_clients, guild=ctx.guild)
if voice:
song_downloaded = False
voice.stop()
await ctx.send("Tocando próxima música.")
@client.command()
async def stop(ctx):
global song_downloaded, shuffle_mode, repeat_mode
voice = get(client.voice_clients, guild=ctx.guild)
queue.clear()
queue_shuffled.clear()
song.clear()
song_downloaded = False
shuffle_mode = False
repeat_mode = 'off'
# Feature Incomplete: Parar a música mesmo quando pausar
if voice:
print("Music stopped")
voice.stop()
await ctx.send("O player parou de tocar.")
else:
print("Nenhuma música está sendo tocada.")
await ctx.send("Nenhuma música está sendo tocada, falha em parar player.")
@client.command()
async def join(ctx):
top_role = ctx.guild.me.top_role
channel = ctx.message.author.voice.channel
# await discord.VoiceChannel.connect(channel)
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
if voice.channel != channel:
await voice.move_to(channel)
else:
await channel.connect()
if not voice is None:
await voice.disconnect()
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
if voice.channel != channel:
await voice.move_to(channel)
else:
await channel.connect()
await ctx.send(f"Conectado ao canal `{channel}`.")
@client.command()
async def leave(ctx):
# print(client.voice_clients[0].guild)
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
# server = ctx.message.guild.voice_client
# server = client.voice_clients[0]
if voice and voice.is_connected():
await voice.disconnect()
await ctx.send(f"Saiu do canal `{channel}`")
else:
print("O bot não está em nenhum canal.")
@client.command()
async def shutdown(ctx):
if ctx.message.author.id == ctx.guild.owner.id: # replace OWNERID with your user id
print("shutdown")
try:
await client.logout()
except:
print("EnvironmentError")
client.clear()
else:
await ctx.send("You do not own this bot!")
client.run(discord_token)