Skip to content

Commit

Permalink
Update 6 files
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Aug 4, 2023
1 parent 2d5cb59 commit f2d15bb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
Empty file.
49 changes: 34 additions & 15 deletions tlg_bot/kreacher/helpers/voice_chats.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
import pickle
from kreacher import client
from pytgcalls import GroupCallFactory
VOICE_CHATS = {}

VOICE_CHATS_FILE = "voice_chats.pkl"


def load_voice_chats():
try:
with open(VOICE_CHATS_FILE, "rb") as file:
return pickle.load(file)
except FileNotFoundError:
return {}


def save_voice_chats(voice_chats):
with open(VOICE_CHATS_FILE, "wb") as file:
pickle.dump(voice_chats, file)


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)

voice_chats = load_voice_chats()
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
raise Exception("I'm joined in the Voice Chat")

voice_chats[chat_id] = voice_chat
save_voice_chats(voice_chats)


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
voice_chats = load_voice_chats()
voice_chat = voice_chats.get(chat_id)

return voice_chat


async def stop_voice_chat(chat_id):
voice_chat = VOICE_CHATS.get(chat_id)
voice_chats = load_voice_chats()
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

del voice_chats[chat_id]
save_voice_chats(voice_chats)
2 changes: 2 additions & 0 deletions tlg_bot/kreacher/plugins/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ async def join_handler(event):
try:
chat = await event.get_chat()
call_py = await get_voice_chat(chat.id)
if call_py is not None:
raise Exception("Streaming is active")
await call_py.start(chat.id)
await event.reply("<i>Master, what do you need? \n\nVoice Chat joined successfully.</i>", parse_mode="HTML")
except Exception as e:
Expand Down
4 changes: 3 additions & 1 deletion tlg_bot/kreacher/plugins/leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async def leave_handler(event):
try:
chat = await event.get_chat()
call_py = await get_voice_chat(chat.id)
if call_py is None or call_py is False:
raise Exception("Streaming is not active")
await call_py.leave_current_group_call()
await event.reply("<i>Goodbye master, just call me if you need me. \n\nVoice Chat left successfully.</i>", parse_mode="HTML")
except Exception as e:
return await event.reply(f"<i>Oops master, something wrong has happened. \n\nError: `{e}`</i>", parse_mode="HTML")
return await event.reply(f"<i>Oops master, something wrong has happened. \n\nError:</i> <code>{e}</code>", parse_mode="HTML")
4 changes: 2 additions & 2 deletions tlg_bot/kreacher/plugins/play_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
@kreacher.on(events.NewMessage(pattern="^[?!/]play_video"))
async def play_video(event):
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:
Expand All @@ -26,7 +25,8 @@ 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 None:
call_py = await get_voice_chat(chat.id)
if call_py is None or call_py is False:
await msg.edit("<i>Joining the voice chat...</i>", parse_mode="HTML")
await create_voice_chat(chat.id)
if match:
Expand Down
Empty file added tlg_bot/voice_chats.pkl
Empty file.

0 comments on commit f2d15bb

Please sign in to comment.