Skip to content

Commit

Permalink
Update 4 files
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Aug 4, 2023
1 parent 0c69100 commit 3950f2c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
3 changes: 0 additions & 3 deletions tlg_bot/kreacher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@
kreacher = _bot.start(bot_token=config.BOT_TOKEN)
client = TelegramClient(None, config.API_ID, config.API_HASH)
client.start()
_call_factory_py = GroupCallFactory(
client, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON)
call_py = _call_factory_py.get_group_call()
1 change: 0 additions & 1 deletion tlg_bot/kreacher/helpers/queues.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
QUEUE = {}
GROUP_CALLS = {}
active = []


Expand Down
32 changes: 32 additions & 0 deletions tlg_bot/kreacher/helpers/voice_chats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from kreacher import client
from pytgcalls import GroupCallFactory
VOICE_CHATS = {}


async def create_voice_chat(chat_id):
_factory = GroupCallFactory(
client, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON)
voice_chat = _factory.get_group_call()
current = VOICE_CHATS.get(chat_id)
if current is not None:
raise Exception("I'am joined in the Voice Chat")
else:
VOICE_CHATS[chat_id] = voice_chat
return True


async def get_voice_chat(chat_id):
voice_chat = VOICE_CHATS.get(chat_id)
if voice_chat is None:
raise Exception("Streaming is not started")
else:
return voice_chat

async def stop_voice_chat(chat_id):
voice_chat = VOICE_CHATS.get(chat_id)
if voice_chat is None:
raise Exception("Streaming is not started")
else:
del VOICE_CHATS[chat_id]
return True

28 changes: 20 additions & 8 deletions tlg_bot/kreacher/plugins/play_video.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from youtubesearchpython import VideosSearch
from kreacher import call_py, kreacher
from kreacher import kreacher
from kreacher.helpers.queues import QUEUE, get_queue
from kreacher.helpers.voice_chats import create_voice_chat, get_voice_chat, stop_voice_chat
from telethon import Button, events
from asyncio import sleep
from yt_dlp import YoutubeDL as ydl
Expand All @@ -16,23 +17,30 @@ async def _(event):

@kreacher.on(events.callbackquery.CallbackQuery(data="pause_callback"))
async def _(event):
chat = await event.get_chat()
call_py = await get_voice_chat(chat.id)
await call_py.set_pause(True)


@kreacher.on(events.callbackquery.CallbackQuery(data="resume_callback"))
async def _(event):
chat = await event.get_chat()
call_py = await get_voice_chat(chat.id)
await call_py.set_pause(False)


@kreacher.on(events.callbackquery.CallbackQuery(data="end_callback"))
async def _(event):
chat = await event.get_chat()
call_py = await get_voice_chat(chat.id)
await call_py.stop_media()


@kreacher.on(events.NewMessage(pattern="^[?!/]play_video"))
async def play_video(event):
msg = await event.reply("🔄 <i>Processing...</i>", parse_mode="HTML")
chat = await event.get_chat()
call_py = await get_voice_chat(chat.id)
msg = await event.reply("🔄 <i>Processing...</i>", parse_mode="HTML")
media = await event.get_reply_message()
if not media and not ' ' in event.message.message:
await msg.edit("❗ __Send Me An Live Stream Link / YouTube Video Link / Reply To An Video To Start Video Streaming!__")
Expand All @@ -44,9 +52,9 @@ async def play_video(event):
return await msg.edit("❗ __Send Me An Live Stream Link / YouTube Video Link / Reply To An Video To Start Video Streaming!__")
regex = r"^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+"
match = re.match(regex, url)
# if call_py.is_connected:
await msg.edit("<i>Joining the voice chat...</i>", parse_mode="HTML")
await call_py.start(chat.id)
if call_py is None:
await msg.edit("<i>Joining the voice chat...</i>", parse_mode="HTML")
await create_voice_chat(chat.id)
if match:
await msg.edit("🔄 <i>Starting YouTube Video Stream...</i>", parse_mode="HTML")
try:
Expand All @@ -62,8 +70,10 @@ async def play_video(event):
split = thumbid.split("?")
thumb = split[0].strip()
except Exception as e:
return await msg.edit(f"❌ **YouTube Download Error !** \n\n`{e}`")
await msg.edit(f"❌ **YouTube Download Error !** \n\n`{e}`")
print(e)
await stop_voice_chat(chat.id)
return await call_py.stop()

else:
await msg.edit("🔄 `Starting Live Video Stream ...`")
Expand All @@ -85,6 +95,7 @@ async def play_video(event):
)
except Exception as e:
await msg.edit(f"❌ **An Error Occoured !** \n\nError: `{e}`")
await stop_voice_chat(chat.id)
return await call_py.stop()

elif media.video or media.file:
Expand Down Expand Up @@ -114,6 +125,7 @@ async def play_video(event):
except Exception as e:
await msg.edit(f"❌ **An Error Occoured !** \n\nError: `{e}`")
print(e)
await stop_voice_chat(chat.id)
return await call_py.stop()

else:
Expand Down Expand Up @@ -162,7 +174,7 @@ async def pause(event, perm):
return
if chat_id in QUEUE:
try:
await call_py.pause_stream(chat_id)
#await call_py.pause_stream(chat_id)
await event.reply("**Streaming Paused**")
except Exception as e:
await event.reply(f"**ERROR:** `{e}`")
Expand All @@ -181,7 +193,7 @@ async def vc_resume(event, perm):
return
if chat_id in QUEUE:
try:
await call_py.resume_stream(chat_id)
#await call_py.resume_stream(chat_id)
await event.reply("**Streaming Started Back 🔙**")
except Exception as e:
await event.reply(f"**ERROR:** `{e}`")
Expand Down

0 comments on commit 3950f2c

Please sign in to comment.