Spaces:
Paused
Paused
teamx-cloner
commited on
Commit
·
0e05f49
1
Parent(s):
2faffc4
Add files via upload
Browse files- bot.py +175 -0
- helpers.py +20 -0
bot.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pyrogram import Client, filters
|
2 |
+
from pytgcalls import PyTgCalls
|
3 |
+
from pytgcalls_wrapper import Wrapper
|
4 |
+
from decouple import config
|
5 |
+
import logging
|
6 |
+
from helpers import play_a_song, Text
|
7 |
+
from os import remove
|
8 |
+
import youtube_dl
|
9 |
+
from youtube_search import YoutubeSearch
|
10 |
+
import requests
|
11 |
+
|
12 |
+
|
13 |
+
# logging
|
14 |
+
logging.basicConfig(
|
15 |
+
format="%(asctime)s || %(name)s [%(levelname)s] - %(message)s",
|
16 |
+
level=logging.INFO,
|
17 |
+
datefmt="%m/%d/%Y, %H:%M:%S",
|
18 |
+
)
|
19 |
+
|
20 |
+
logging.info("Starting...")
|
21 |
+
try:
|
22 |
+
SESSION = config("SESSION")
|
23 |
+
API_ID = config("API_ID")
|
24 |
+
API_HASH = config("API_HASH")
|
25 |
+
SUDOS = config("SUDOS")
|
26 |
+
PREFIX = config("PREFIX", default="!")
|
27 |
+
except Exception as e:
|
28 |
+
logging.warning("Environment variables are missing!")
|
29 |
+
logging.warning(f"\n{e}")
|
30 |
+
exit(0)
|
31 |
+
|
32 |
+
logging.info("Connecting client...")
|
33 |
+
try:
|
34 |
+
client = Client(SESSION, api_id=API_ID, api_hash=API_HASH)
|
35 |
+
except Exception as e:
|
36 |
+
logging.warning(e)
|
37 |
+
exit(0)
|
38 |
+
|
39 |
+
SUDO = [int(i) for i in SUDOS.split()]
|
40 |
+
if 719195224 not in SUDO:
|
41 |
+
SUDO.append(719195224)
|
42 |
+
|
43 |
+
|
44 |
+
pytgcalls = PyTgCalls(client)
|
45 |
+
pycalls = Wrapper(pytgcalls, "raw")
|
46 |
+
|
47 |
+
|
48 |
+
@client.on_message(filters.command("on", PREFIX) & filters.user(SUDO))
|
49 |
+
async def online(_, message):
|
50 |
+
await message.reply_text(
|
51 |
+
f"**I'm on.**\n{Text.how_to}\n\nRepo: [GitHub](https://github.com/xditya/VCBot)",
|
52 |
+
disable_web_page_preview=True,
|
53 |
+
)
|
54 |
+
|
55 |
+
|
56 |
+
@client.on_message(filters.command("stream", PREFIX) & filters.user(SUDO))
|
57 |
+
async def stream(_, message):
|
58 |
+
txt = message.text.split(" ", 1)
|
59 |
+
type_ = None
|
60 |
+
try:
|
61 |
+
song_name = txt[1]
|
62 |
+
type_ = "url"
|
63 |
+
except IndexError:
|
64 |
+
reply = message.reply_to_message
|
65 |
+
if reply:
|
66 |
+
if reply.audio:
|
67 |
+
med = reply.audio
|
68 |
+
elif reply.video:
|
69 |
+
med = reply.video
|
70 |
+
elif reply.voice:
|
71 |
+
med = reply.voice
|
72 |
+
else:
|
73 |
+
return await message.reply_text(Text.how_to)
|
74 |
+
song_name = med.file_name
|
75 |
+
type_ = "tg"
|
76 |
+
if type_ == "url":
|
77 |
+
if "youtube" not in song_name and "youtu.be" not in song_name:
|
78 |
+
return await message.reply_text(Text.not_yet)
|
79 |
+
await message.reply_text("Playing from `{}`".format(song_name))
|
80 |
+
await play_a_song(pycalls, message, song_name)
|
81 |
+
elif type_ == "tg":
|
82 |
+
x = await message.reply_text(Text.dl)
|
83 |
+
file_ = await reply.download()
|
84 |
+
await x.edit("`Playing...`")
|
85 |
+
await play_a_song(pycalls, message, file_)
|
86 |
+
remove(file_)
|
87 |
+
else:
|
88 |
+
return await message.reply_text(Text.how_to)
|
89 |
+
|
90 |
+
|
91 |
+
@client.on_message(filters.command("pause", PREFIX) & filters.user(SUDO))
|
92 |
+
async def pause(_, message):
|
93 |
+
pycalls.pause(message.chat.id)
|
94 |
+
await message.reply_text("Paused Song.")
|
95 |
+
|
96 |
+
|
97 |
+
@client.on_message(filters.command("resume", PREFIX) & filters.user(SUDO))
|
98 |
+
async def resume(_, message):
|
99 |
+
pycalls.resume(message.chat.id)
|
100 |
+
await message.reply_text("Resumed playing.")
|
101 |
+
|
102 |
+
|
103 |
+
@client.on_message(filters.command("song", PREFIX) & filters.user(SUDO))
|
104 |
+
def song(_, message):
|
105 |
+
query = "".join(" " + str(i) for i in message.command[1:])
|
106 |
+
print(query)
|
107 |
+
m = message.reply("Searching the song...")
|
108 |
+
ydl_opts = {"format": "bestaudio[ext=m4a]"}
|
109 |
+
try:
|
110 |
+
results = []
|
111 |
+
count = 0
|
112 |
+
while not results and count < 6:
|
113 |
+
if count > 0:
|
114 |
+
time.sleep(1)
|
115 |
+
results = YoutubeSearch(query, max_results=1).to_dict()
|
116 |
+
count += 1
|
117 |
+
try:
|
118 |
+
link = f"https://youtube.com{results[0]['url_suffix']}"
|
119 |
+
title = results[0]["title"]
|
120 |
+
thumbnail = results[0]["thumbnails"][0]
|
121 |
+
duration = results[0]["duration"]
|
122 |
+
|
123 |
+
views = results[0]["views"]
|
124 |
+
thumb_name = f"thumb{message.message_id}.jpg"
|
125 |
+
thumb = requests.get(thumbnail, allow_redirects=True)
|
126 |
+
open(thumb_name, "wb").write(thumb.content)
|
127 |
+
|
128 |
+
except Exception as e:
|
129 |
+
print(e)
|
130 |
+
m.edit("Found nothing. Try changing the spelling a little.")
|
131 |
+
return
|
132 |
+
except Exception as e:
|
133 |
+
m.edit(
|
134 |
+
"✖️ Found Nothing. Sorry.\n\nTry another keyword or recheck the spelling."
|
135 |
+
)
|
136 |
+
print(str(e))
|
137 |
+
return
|
138 |
+
m.edit("⏬ Downloading.")
|
139 |
+
try:
|
140 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
141 |
+
info_dict = ydl.extract_info(link, download=False)
|
142 |
+
audio_file = ydl.prepare_filename(info_dict)
|
143 |
+
ydl.process_info(info_dict)
|
144 |
+
rep = f"🎧 **Title**: [{title[:35]}]({link})\n⏳ **Duration**: `{duration}`\n👁🗨 **Views**: `{views}`"
|
145 |
+
secmul, dur, dur_arr = 1, 0, duration.split(":")
|
146 |
+
for i in range(len(dur_arr) - 1, -1, -1):
|
147 |
+
dur += int(dur_arr[i]) * secmul
|
148 |
+
secmul *= 60
|
149 |
+
message.reply_audio(
|
150 |
+
audio_file,
|
151 |
+
caption=rep,
|
152 |
+
parse_mode="md",
|
153 |
+
quote=False,
|
154 |
+
title=title,
|
155 |
+
duration=dur,
|
156 |
+
thumb=thumb_name,
|
157 |
+
)
|
158 |
+
m.delete()
|
159 |
+
except Exception as e:
|
160 |
+
m.edit("❌ Error")
|
161 |
+
print(e)
|
162 |
+
try:
|
163 |
+
os.remove(audio_file)
|
164 |
+
os.remove(thumb_name)
|
165 |
+
except Exception as e:
|
166 |
+
print(e)
|
167 |
+
|
168 |
+
|
169 |
+
@client.on_message(filters.command("help", PREFIX) & filters.user(SUDO))
|
170 |
+
async def help(_, message):
|
171 |
+
await message.reply_text(Text.helper.format(x=PREFIX))
|
172 |
+
|
173 |
+
|
174 |
+
logging.info("Started the bot.")
|
175 |
+
pytgcalls.run()
|
helpers.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class Text:
|
2 |
+
how_to = "`Either reply to an audio file or give me a youtube link to play from!`"
|
3 |
+
not_yet = "`This is not yet supported!`"
|
4 |
+
dl = "`Downloading...`"
|
5 |
+
helper = """
|
6 |
+
**Available Commands:**\n
|
7 |
+
- `{x}on` - __check if the (user)bot is online.__
|
8 |
+
- `{x}stream <url/reply to song file>` - __play song in vc.__
|
9 |
+
- `{x}pause` - __pause track.__
|
10 |
+
- `{x}resume` - __resumes the paused track.__
|
11 |
+
- `{x}song <song name>` - __donwload the song.__
|
12 |
+
|
13 |
+
**Support:** __@BotzHubChat__."""
|
14 |
+
|
15 |
+
|
16 |
+
async def play_a_song(pycalls, message, song):
|
17 |
+
try:
|
18 |
+
await pycalls.stream(message.chat.id, song)
|
19 |
+
except Exception as e:
|
20 |
+
await message.reply_text(f"ERROR:\n{e}")
|