forked from TheHamkerCat/Telegram_VC_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
650 lines (574 loc) · 17.5 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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
from __future__ import unicode_literals
import youtube_dl
import asyncio
import aiohttp
import aiofiles
import time
import json
import os
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from pyrogram import Client, filters
from pyrogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup
from youtube_search import YoutubeSearch
from config import owner_id, bot_token, radio_link, sudo_chat_id
app = Client(
":memory:",
bot_token=bot_token,
api_id=6,
api_hash="eb06d4abfb49dc3eeb1aeb98ae0f581e",
)
# Get User Input
def kwairi(message):
query = ""
for i in message.command[1:]:
query += f"{i} "
return query
async def list_admins(group_id):
list_of_admins = []
async for member in app.iter_chat_members(
group_id, filter="administrators"):
list_of_admins.append(member.user.id)
return list_of_admins
def convert_seconds(seconds):
seconds = seconds % (24 * 3600)
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%02d:%02d" % (minutes, seconds)
# For Blacklist filter
blacks = []
# Ping
@app.on_message(
filters.command(["ping"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def ping(_, message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
start_time = int(round(time.time() * 1000))
m = await message.reply_text(".")
end_time = int(round(time.time() * 1000))
await m.edit(f"{end_time - start_time} ms")
# Start
@app.on_message(filters.command(["start"]) & ~filters.edited)
async def start(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
await message.reply_text(
"Hi I'm Telegram Voice Chat Bot. Join @TheHamkerChat For Support."
)
# Help
@app.on_message(
filters.command(["help"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def help(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
await message.reply_text(
"""Currently These Commands Are Supported.
/start To Start The bot.
/help To Show This Message.
/ping To Ping All Datacenters Of Telegram.
/end To Stop Any Playing Music.
"/jiosaavn <song_name>" To Play A Song From Jiosaavn.
"/youtube <song_name> or <song_link>" To Search For A Song And Play The Top-Most Song Or Play With A Link.
"/playlist <youtube_playlist_url> To Play A Playlist From Youtube".
/telegram To Play A Song Directly From Telegram File.
/radio To Play Radio Continuosly.
/black To Blacklist A User.
/white To Whitelist A User.
/users To Get A List Of Blacklisted Users.
NOTE: Do Not Assign These Commands To Bot Via BotFather"""
)
# Jiosaavn
# Global vars
s = None
m = None
@app.on_message(
filters.command(["jiosaavn"])
& filters.chat(sudo_chat_id)
& ~filters.edited
)
async def jiosaavn(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
global s
global m
if len(message.command) < 2:
await message.reply_text("/jiosaavn requires an argument")
return
try:
os.system("killall -9 mpv")
except:
pass
try:
await m.delete()
except:
pass
try:
await message.delete()
except:
pass
query = kwairi(message)
m = await message.reply_text(f"Searching for `{query}` on JioSaavn")
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://jiosaavnapi.bhadoo.uk/result/?query={query}"
) as resp:
r = json.loads(await resp.text())
sname = r[0]["song"]
slink = r[0]["media_url"]
ssingers = r[0]["singers"]
sthumb = r[0]["image"]
sduration = r[0]["duration"]
sduration_converted = convert_seconds(int(sduration))
await m.edit("Processing Thumbnail.")
async with aiohttp.ClientSession() as session:
async with session.get(sthumb) as resp:
if resp.status == 200:
f = await aiofiles.open("background.png", mode="wb")
await f.write(await resp.read())
await f.close()
def changeImageSize(maxWidth, maxHeight, image):
widthRatio = maxWidth / image.size[0]
heightRatio = maxHeight / image.size[1]
newWidth = int(widthRatio * image.size[0])
newHeight = int(heightRatio * image.size[1])
newImage = image.resize((newWidth, newHeight))
return newImage
image1 = Image.open("./background.png")
image2 = Image.open("etc/foreground.png")
image3 = changeImageSize(1280, 720, image1)
image4 = changeImageSize(1280, 720, image2)
image5 = image3.convert("RGBA")
image6 = image4.convert("RGBA")
Image.alpha_composite(image5, image6).save("temp.png")
img = Image.open("temp.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("etc/JetBrainsMonoNL-Regular.ttf", 32)
draw.text(
(190, 550), f"Title: {sname}", (255, 255, 255), font=font
)
draw.text(
(190, 590), f"Artist: {ssingers}", (255, 255, 255), font=font
)
draw.text(
(190, 630),
f"Duration: {sduration_converted} Seconds",
(255, 255, 255),
font=font,
)
draw.text(
(190, 670),
f"Played By: {message.from_user.first_name}",
(255, 255, 255),
font=font,
)
img.save("final.png")
os.system("rm temp.png")
os.system("rm background.png")
await m.delete()
m = await message.reply_photo(
caption=f"Playing `{sname}` Via Jiosaavn #music",
photo="final.png",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
f"STOP", callback_data="end"
)
]
]
),
parse_mode="markdown",
)
s = await asyncio.create_subprocess_shell(
f"mpv {slink} --no-video",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await s.wait()
await m.delete()
# Youtube Play
@app.on_message(
filters.command(["youtube"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def ytplay(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
global m
global s
if len(message.command) < 2:
await message.reply_text("/youtube requires one argument")
return
try:
await message.delete()
except:
pass
try:
await m.delete()
except:
pass
try:
os.system("killall -9 mpv")
except:
pass
try:
os.remove("audio.mp3")
except:
pass
ydl_opts = {"format": "bestaudio"}
query = kwairi(message)
m = await message.reply_text(f"Searching for `{query}` on YouTube")
results = YoutubeSearch(query, max_results=1).to_dict()
link = f"https://youtube.com{results[0]['url_suffix']}"
title = results[0]["title"]
thumbnail = results[0]["thumbnails"][0]
duration = results[0]["duration"]
views = results[0]["views"]
await m.edit("Processing Thumbnail.")
async with aiohttp.ClientSession() as session:
async with session.get(thumbnail) as resp:
if resp.status == 200:
f = await aiofiles.open("background.png", mode="wb")
await f.write(await resp.read())
await f.close()
def changeImageSize(maxWidth, maxHeight, image):
widthRatio = maxWidth / image.size[0]
heightRatio = maxHeight / image.size[1]
newWidth = int(widthRatio * image.size[0])
newHeight = int(heightRatio * image.size[1])
newImage = image.resize((newWidth, newHeight))
return newImage
image1 = Image.open("./background.png")
image2 = Image.open("etc/foreground.png")
image3 = changeImageSize(1280, 720, image1)
image4 = changeImageSize(1280, 720, image2)
image5 = image3.convert("RGBA")
image6 = image4.convert("RGBA")
Image.alpha_composite(image5, image6).save("temp.png")
img = Image.open("temp.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("etc/JetBrainsMonoNL-Regular.ttf", 32)
draw.text((190, 550), f"Title: {title}", (255, 255, 255), font=font)
draw.text(
(190, 590), f"Duration: {duration}", (255, 255, 255), font=font
)
draw.text((190, 630), f"Views: {views}", (255, 255, 255), font=font)
draw.text(
(190, 670),
f"Played By: {message.from_user.first_name}",
(255, 255, 255),
font=font,
)
img.save("final.png")
os.system("rm temp.png")
os.system("rm background.png")
await m.edit("Downloading Music.")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
audio_file = ydl.prepare_filename(info_dict)
ydl.process_info(info_dict)
os.rename(audio_file, "audio.webm")
await m.delete()
m = await message.reply_photo(
caption=f"Playing [{title}]({link}) Via YouTube #music",
photo="final.png",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
f"STOP", callback_data="end"
)
]
]
),
parse_mode="markdown",
)
os.system("rm final.png")
s = await asyncio.create_subprocess_shell(
"mpv audio.webm --no-video",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await s.wait()
await m.delete()
# youtube playlist
@app.on_message(
filters.command(["playlist"])
& filters.chat(sudo_chat_id)
& ~filters.edited
)
async def playlist(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
global m
global s
if len(message.command) != 2:
await message.reply_text(
"/playlist requires one youtube playlist link"
)
return
try:
await message.delete()
except:
pass
try:
await m.delete()
except:
pass
try:
os.system("killall -9 mpv")
except:
pass
try:
os.remove("audio.mp3")
except:
pass
link = message.command[1]
ydl_opts = {"format": "bestaudio"}
m = await message.reply_text("Processing Playlist...")
with youtube_dl.YoutubeDL():
result = youtube_dl.YoutubeDL().extract_info(link, download=False)
if "entries" in result:
video = result["entries"]
await m.edit(
f"Found {len(result['entries'])} Videos In Playlist, Playing Them All."
)
ii = 1
for i, item in enumerate(video):
video = result["entries"][i]["webpage_url"]
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(video, download=False)
audio_file = ydl.prepare_filename(info_dict)
ydl.process_info(info_dict)
os.rename(audio_file, "audio.webm")
await m.edit(
f"Playing `{result['entries'][i]['title']}`, Song Number `{ii}` In Playlist, `{len(result['entries']) - ii}` In Queue. \nRequested by - {message.from_user.mention}"
)
s = await asyncio.create_subprocess_shell(
"mpv audio.webm --no-video",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await s.wait()
ii += 1
os.system("rm audio.webm")
# Telegram Audio
@app.on_message(
filters.command(["telegram"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def tgplay(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
global m
global s
if not message.reply_to_message:
await message.reply_text("Reply To A Telegram Audio To Play It.")
return
try:
await message.delete()
except:
pass
try:
await m.delete()
except:
pass
try:
os.system("killall -9 mpv")
except:
pass
try:
os.remove("audio.mp3")
except:
pass
try:
os.remove("downloads/audio.mp3")
except:
pass
m = await message.reply_text("Downloading")
j = await app.download_media(message.reply_to_message, file_name="audio.mp3")
await m.edit(f"Playing `{message.reply_to_message.link}` via Telegram.")
s = await asyncio.create_subprocess_shell(
"mpv downloads/audio.mp3 --no-video",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await s.wait()
await m.delete()
os.system("rm downloads/audio.mp3")
# Radio
@app.on_message(
filters.command(["radio"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def radio(_, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
global m
global s
try:
os.system("killall -9 mpv")
except:
pass
try:
await m.delete()
except:
pass
try:
await message.delete()
except:
pass
try:
os.remove("audio.mp3")
except:
pass
m = await message.reply_text(
f"Playing Radio\nRequested by - {message.from_user.mention}"
)
s = await asyncio.create_subprocess_shell(
f"mpv {radio_link} --no-video",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await s.wait()
await m.delete()
# End Music
@app.on_message(
filters.command(["end"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def end(_, message: Message):
global blacks
global m
global s
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
try:
os.remove("audio.mp3")
except:
pass
try:
await message.delete()
except:
pass
try:
os.system("killall -9 mpv")
except:
pass
try:
s.terminate()
except:
pass
try:
await m.delete()
except:
pass
await message.reply_text(
f"{message.from_user.mention} Stopped The Music."
)
@app.on_callback_query(filters.regex("end"))
async def end_callback(_, CallbackQuery):
global blacks
global m
global s
chat_id = int(CallbackQuery.message.chat.id)
if CallbackQuery.from_user.id in blacks:
return
try:
os.remove("audio.mp3")
except:
pass
try:
os.system("killall -9 mpv")
except:
pass
try:
s.terminate()
except:
pass
try:
await m.delete()
except:
pass
await app.send_message(
chat_id,
f"{CallbackQuery.from_user.mention} - {CallbackQuery.from_user.id} Stopped The Music.",
)
# Ban
@app.on_message(
filters.command(["black"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def blacklist(_, message: Message):
global blacks
if message.from_user.id != owner_id:
await message.reply_text("Only owner can blacklist users.")
return
if not message.reply_to_message:
await message.reply_text(
"Reply to a message with /black to blacklist a user."
)
return
if message.reply_to_message.from_user.id in blacks:
await message.reply_text("This user is already blacklisted.")
return
blacks.append(message.reply_to_message.from_user.id)
await message.reply_text(
f"Blacklisted {message.reply_to_message.from_user.mention}"
)
# Unban
@app.on_message(
filters.command(["white"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def whitelist(_, message: Message):
global blacks
if message.from_user.id != owner_id:
await message.reply_text("Only owner can whitelist users.")
return
if not message.reply_to_message:
await message.reply_text("Reply to a message to whitelist a user.")
return
if message.reply_to_message.from_user.id in blacks:
blacks.remove(message.reply_to_message.from_user.id)
await message.reply_text(
f"Whitelisted {message.reply_to_message.from_user.mention}"
)
else:
await message.reply_text("This user is already whitelisted.")
# Blacklisted users
@app.on_message(
filters.command(["users"]) & filters.chat(sudo_chat_id) & ~filters.edited
)
async def users(client, message: Message):
global blacks
if message.from_user.id in blacks:
await message.reply_text("You're Blacklisted, So Stop Spamming.")
return
output = "Blacklisted Users:\n"
n = 1
for i in blacks:
usern = (await client.get_users(i)).mention
output += f"{n}. {usern}\n"
n += 1
if len(blacks) == 0:
await message.reply_text("No Users Are Blacklisted")
return
await message.reply_text(output)
print("Bot Starting...")
app.run()