forked from kursadHD/TgMusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
509 lines (475 loc) · 16.9 KB
/
main.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
import os
import json
import logging
# pyrogram
from pyrogram import Client, filters
from pyrogram.types import Message
# pytgcalls
from pytgcalls import PyTgCalls, StreamType
from pytgcalls.types import Update
from pytgcalls.types.stream import StreamAudioEnded
from pytgcalls.types.input_stream import AudioImagePiped
from pytgcalls.types.input_stream.quality import HighQualityAudio, HighQualityVideo
# functions
from core import (search, command, extract_args,
get_youtube_playlist, get_spotify_playlist,
get_group, set_group, set_title,
get_queue, clear_queue, shuffle_queue,
add_bl, rem_bl, get_bl)
from core.decorators import register, handle_error, blacklist_check, only_admins
from core.song import Song
from lang import load
from config import config
app = Client(config.SESSION, api_id=config.API_ID, api_hash=config.API_HASH, parse_mode='markdown')
tgcalls = PyTgCalls(app)
logging.basicConfig(level=config.LOG_LEVEL)
lang = load('tr')
"""start"""
@app.on_message(command(['start', 'help']))
@handle_error
async def start(_, message: Message):
await app.send_message(message.chat.id, lang['start'].replace('<prefix>', config.PREFIXES[0]))
"""ping"""
@app.on_message(command('ping'))
async def ping(_, message: Message):
await message.reply_text(f'`{await tgcalls.ping}s`')
"""play"""
@app.on_message(command(['play', 'p']) & filters.group)
@register
@blacklist_check
@handle_error
async def play(_, message: Message):
chat_id = message.chat.id
group = get_group(chat_id)
song = search(message)
if song is None:
return await message.reply_text(lang['notFound'])
ok, status = await song.parse()
if not ok:
raise Exception(status)
if group['is_playing'] == False:
set_group(chat_id, is_playing=True, now_playing=song)
infomsg = await message.reply_text(lang['downloading'])
await tgcalls.join_group_call(
chat_id,
AudioImagePiped(
song.remote_url,
song.thumb,
HighQualityAudio(),
HighQualityVideo(),
song.headers
),
stream_type=StreamType().pulse_stream
)
await set_title(message, song.title)
await infomsg.edit_text(lang['playing'] % (song.thumb, song.title, song.yt_url, song.duration, song.requested_by.mention))
else:
queue = get_queue(chat_id)
await queue.put(song)
await message.reply_text(lang['addedToQueue'] % (song.title, song.yt_url, len(queue)), disable_web_page_preview=True)
"""radio"""
@app.on_message(command('radio') & filters.group)
@register
@blacklist_check
@handle_error
async def live(_, message: Message):
chat_id = message.chat.id
group = get_group(chat_id)
link = extract_args(message.text)
if not link:
return await message.reply_text(lang['notFound'])
song = Song({'url': link}, message)
check = await song.check_remote_url(song.remote_url)
if not check:
return await message.reply_text(lang['notFound'])
if group['is_playing'] == False:
set_group(chat_id, is_playing=True, now_playing=song)
infomsg = await message.reply_text(lang['downloading'])
await tgcalls.join_group_call(
chat_id,
AudioImagePiped(
song.remote_url,
song.thumb,
HighQualityAudio(),
HighQualityVideo()
),
stream_type=StreamType().live_stream
)
await set_title(message, song.title)
await infomsg.edit_text(lang['playing'] % (song.thumb, song.title, song.yt_url, song.duration, song.requested_by.mention or 'Unknown'))
else:
queue = get_queue(chat_id)
await queue.put(song)
await message.reply_text(lang['addedToQueue'] % (song.title, song.yt_url, len(queue)), disable_web_page_preview=True)
"""skip"""
@app.on_message(command(['skip', 'next', 's', 'n']) & filters.group)
@register
@blacklist_check
@handle_error
async def skip(_, message: Message):
chat_id = message.chat.id
group = get_group(chat_id)
if group['loop']:
await tgcalls.change_stream(
chat_id,
AudioImagePiped(
group['now_playing'].remote_url,
group['now_playing'].thumb,
HighQualityAudio(),
HighQualityVideo(),
group['now_playing'].headers
)
)
else:
queue = get_queue(chat_id)
if len(queue) > 0:
next_song = await queue.get()
if not group['quiet']:
infomsg = await next_song.request_msg.reply_text(lang['downloading'])
if not next_song.parsed:
ok, status = await next_song.parse()
if not ok:
raise Exception(status)
await tgcalls.change_stream(
chat_id,
AudioImagePiped(
next_song.remote_url,
next_song.thumb,
HighQualityAudio(),
HighQualityVideo(),
next_song.headers
)
)
set_group(chat_id, now_playing=next_song)
await set_title(message, next_song.title)
if not group['quiet']:
await infomsg.edit_text(lang['playing'] % (next_song.thumb, next_song.title, next_song.yt_url, next_song.duration, next_song.requested_by.mention))
else:
set_group(chat_id, is_playing=False, now_playing=None)
await set_title(message, '')
await message.reply_text(lang['queueEmpty'])
await tgcalls.leave_group_call(
chat_id
)
"""leave"""
@app.on_message(command(['leave', 'l']) & filters.group)
@register
@blacklist_check
@handle_error
async def leave(_, message: Message):
chat_id = message.chat.id
set_group(chat_id, is_playing=False, now_playing=None)
await set_title(message, '')
clear_queue(chat_id)
await tgcalls.leave_group_call(
chat_id
)
"""queue"""
@app.on_message(command('queue') & filters.group)
@register
@blacklist_check
@handle_error
async def queues(_, message: Message):
chat_id = message.chat.id
queue = get_queue(chat_id)
if len(queue) > 0:
await message.reply_text(str(queue), disable_web_page_preview=True)
else:
await message.reply_text(lang['queueEmpty'])
"""shuffle"""
@app.on_message(command('shuffle') & filters.group)
@register
@blacklist_check
@handle_error
async def shuffle(_, message: Message):
chat_id = message.chat.id
if len(get_queue(chat_id)) > 0:
shuffled = shuffle_queue(chat_id)
await message.reply_text(lang['shuffled'])
await message.reply_text(str(shuffled), disable_web_page_preview=True)
else:
await message.reply_text(lang['queueEmpty'])
"""now_playing"""
@app.on_message(command(['now', 'np', 'now_playing']) & filters.group)
@register
@blacklist_check
@handle_error
async def now_playing(_, message: Message):
chat_id = message.chat.id
group = get_group(chat_id)
if group['is_playing']:
song = group['now_playing']
await message.reply_text(lang['playing'] % (song.thumb, song.title, song.yt_url, song.duration, song.requested_by.mention))
else:
await message.reply_text(lang['notPlaying'])
"""loop"""
@app.on_message(command('loop') & filters.group)
@register
@blacklist_check
@handle_error
async def loop(_, message: Message):
chat_id = message.chat.id
group = get_group(chat_id)
if group['loop'] == True:
set_group(chat_id, loop=False)
await message.reply_text(lang['loopOff'])
elif group['loop'] == False:
set_group(chat_id, loop=True)
await message.reply_text(lang['loopOn'])
"""quiet"""
@app.on_message(command('quiet') & filters.group)
@register
@blacklist_check
@handle_error
async def quiet(_, message: Message):
chat_id = message.chat.id
group = get_group(chat_id)
if group['quiet']:
set_group(chat_id, quiet=False)
await message.reply_text(lang['quietModeOff'])
else:
set_group(chat_id, quiet=True)
await message.reply_text(lang['quietModeOn'])
"""language"""
@app.on_message(command(['language', 'lang']))
@register
@only_admins
@handle_error
async def set_lang(_, message: Message):
global lang
chat_id = message.chat.id
lng = extract_args(message.text)
if lng != '':
langs = [file.replace('.json', '') for file in os.listdir(f'{os.getcwd()}/lang/') if file.endswith('.json')]
if lng == 'list':
await message.reply_text("\n".join(langs))
else:
if lng in langs:
set_group(chat_id, lang=lng)
lang = load(lng)
await message.reply_text(lang['langSet'] % lng)
else:
await message.reply_text(lang['notFound'])
"""add blacklist"""
@app.on_message(command(['add_blacklist', 'addbl']) & filters.group)
@register
@only_admins
@handle_error
async def add_blacklist(_, message: Message):
chat_id = message.chat.id
args = extract_args(message.text)
uid = int(args) if args.isnumeric() else message.reply_to_message.from_user.id
if uid and uid not in get_bl(chat_id) and uid not in config.SUDO:
add_bl(chat_id, uid)
await message.reply_text(lang['blacklist'] % uid)
"""remove blacklist"""
@app.on_message(command(['remove_blacklist', 'rmbl']) & filters.group)
@register
@only_admins
@handle_error
async def rm_blacklist(_, message: Message):
chat_id = message.chat.id
args = extract_args(message.text)
uid = int(args) if args.isnumeric() else message.reply_to_message.from_user.id
if uid and uid in get_bl(chat_id):
rem_bl(chat_id, uid)
await message.reply_text(lang['rmBlacklist'] % uid)
"""get blacklist"""
@app.on_message(command(['get_blacklist', 'getbl']) & filters.group)
@register
@only_admins
@handle_error
async def get_blacklist(_, message: Message):
chat_id = message.chat.id
await message.reply_text("\n".join([f'`{str(uid)}`' for uid in get_bl(chat_id)]) or lang['blacklistEmpty'])
"""export"""
@app.on_message(command('export') & filters.group)
@register
@handle_error
async def export_queue(_, message: Message):
chat_id = message.chat.id
queue = get_queue(chat_id)
if len(queue) > 0:
data = json.dumps([song.to_dict() for song in queue], indent=2)
filename = f'{message.chat.username or message.chat.id}.json'
with open(filename, 'w') as file:
file.write(data)
await message.reply_document(filename, caption=lang['queueExported'] % len(queue))
os.remove(filename)
else:
await message.reply_text(lang['queueEmpty'])
"""import"""
@app.on_message(command('import') & filters.group)
@register
@handle_error
async def import_queue(_, message: Message):
if not message.reply_to_message or not message.reply_to_message.document:
return await message.reply_text(lang['replyToAFile'])
chat_id = message.chat.id
filename = await message.reply_to_message.download()
data_str = None
with open(filename, 'r') as file:
data_str = file.read()
try:
data = json.loads(data_str)
except json.JSONDecodeError:
return await message.reply_text(lang['invalidFile'])
try:
temp_queue = []
for song_dict in data:
song = Song(song_dict['yt_url'], message)
song.title = song_dict['title']
temp_queue.append(song)
except:
return await message.reply_text(lang['invalidFile'])
group = get_group(chat_id)
queue = get_queue(chat_id)
if group['is_playing']:
for _song in temp_queue:
await queue.put(_song)
await message.reply_text(lang['queueImported'] % len(temp_queue))
else:
song = temp_queue[0]
set_group(chat_id, is_playing=True, now_playing=song)
infomsg = await message.reply_text(lang['downloading'])
ok, status = await song.parse()
if not ok:
raise Exception(status)
await tgcalls.join_group_call(
chat_id,
AudioImagePiped(
song.remote_url,
song.thumb,
HighQualityAudio(),
HighQualityVideo(),
song.headers
),
stream_type=StreamType().pulse_stream
)
await set_title(message, song.title)
await infomsg.edit_text(lang['playing'] % (song.thumb, song.title, song.yt_url, song.duration, song.requested_by.mention))
for _song in temp_queue[1:]:
await queue.put(_song)
await message.reply_text(lang['queueImported'] % len(temp_queue))
"""playlist"""
@app.on_message(command('playlist') & filters.group)
@register
@handle_error
async def import_playlist(_, message: Message):
chat_id = message.chat.id
if message.reply_to_message:
text = message.reply_to_message.text
else:
text = extract_args(message.text)
if text == '':
return await message.reply_text(lang['notFound'])
if 'open.spotify.com/playlist/' in text:
try:
temp_queue = get_spotify_playlist(text, message)
except:
return await message.reply_text(lang['notFound'])
elif 'youtube.com/playlist?list=' in text:
try:
temp_queue = get_youtube_playlist(text, message)
except:
return await message.reply_text(lang['notFound'])
group = get_group(chat_id)
queue = get_queue(chat_id)
if not group['is_playing']:
song = await temp_queue.__anext__()
set_group(chat_id, is_playing=True, now_playing=song)
infomsg = await message.reply_text(lang['downloading'])
ok, status = await song.parse()
if not ok:
raise Exception(status)
await tgcalls.join_group_call(
chat_id,
AudioImagePiped(
song.remote_url,
song.thumb,
HighQualityAudio(),
HighQualityVideo(),
song.headers
),
stream_type=StreamType().pulse_stream
)
await set_title(message, song.title)
await infomsg.edit_text(lang['playing'] % (song.thumb, song.title, song.yt_url, song.duration, song.requested_by.mention))
async for _song in temp_queue:
await queue.put(_song)
queue.get_nowait()
else:
async for _song in temp_queue:
await queue.put(_song)
await message.reply_text(lang['queueImported'] % len(group['queue']))
"""on stream end"""
@tgcalls.on_stream_end()
@handle_error
async def stream_end(_, update: Update):
if not isinstance(update, StreamAudioEnded):
return
chat_id = update.chat_id
group = get_group(chat_id)
if group['loop']:
await tgcalls.change_stream(
chat_id,
AudioImagePiped(
group['now_playing'].remote_url,
group['now_playing'].thumb,
HighQualityAudio(),
HighQualityVideo(),
group['now_playing'].headers
)
)
else:
queue = get_queue(chat_id)
if len(queue) > 0:
next_song = await queue.get()
set_group(chat_id, now_playing=next_song)
if not group['quiet']:
infomsg = await next_song.request_msg.reply_text(lang['downloading'])
if not next_song.parsed:
ok, status = await next_song.parse()
if not ok:
raise Exception(status)
await tgcalls.change_stream(
chat_id,
AudioImagePiped(
next_song.remote_url,
next_song.thumb,
HighQualityAudio(),
HighQualityVideo(),
next_song.headers
)
)
await set_title(chat_id, next_song.title, client=app)
if not group['quiet']:
await infomsg.edit_text(lang['playing'] % (next_song.thumb, next_song.title, next_song.yt_url, next_song.duration, next_song.requested_by.mention))
else:
await set_title(chat_id, '', client=app)
set_group(chat_id, is_playing=False, now_playing=None)
await tgcalls.leave_group_call(
chat_id
)
"""on closed voice chat"""
@tgcalls.on_closed_voice_chat()
@handle_error
async def closed(_, chat_id: int):
await set_title(chat_id, '', client=app)
set_group(chat_id, now_playing=None, is_playing=False)
clear_queue(chat_id)
"""on kicked"""
@tgcalls.on_kicked()
@handle_error
async def kicked(_, chat_id: int):
await set_title(chat_id, '', client=app)
set_group(chat_id, now_playing=None, is_playing=False)
clear_queue(chat_id)
"""on left"""
@tgcalls.on_left()
@handle_error
async def left(_, chat_id: int):
await set_title(chat_id, '', client=app)
set_group(chat_id, now_playing=None, is_playing=False)
clear_queue(chat_id)
tgcalls.run()