Skip to content

Commit

Permalink
Update voice_chats.py and join.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Aug 5, 2023
1 parent 896d4c0 commit 991e3d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
47 changes: 19 additions & 28 deletions tlg_bot/kreacher/helpers/voice_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,38 @@

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)
try:
with open(VOICE_CHATS_FILE, "rb") as file:
VOICE_CHATS = pickle.load(file)
except (FileNotFoundError, EOFError):
VOICE_CHATS = {}


async def create_voice_chat(chat_id):
_factory = GroupCallFactory(client, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON)
voice_chat = _factory.get_group_call()

voice_chats = load_voice_chats()
current = voice_chats.get(chat_id)

if current is not None:
factory = GroupCallFactory(client, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON)
voice_chat = factory.get_group_call()
get = VOICE_CHATS.get(chat_id)
if get is not None:
raise Exception("I'm joined in the Voice Chat")

voice_chats[chat_id] = voice_chat
save_voice_chats(voice_chats)
VOICE_CHATS[chat_id] = voice_chat

with open(VOICE_CHATS_FILE, "wb") as file:
pickle.dump(VOICE_CHATS, file)

async def get_voice_chat(chat_id):
voice_chats = load_voice_chats()
voice_chat = voice_chats.get(chat_id)

async def get_voice_chat(chat_id):
voice_chat = VOICE_CHATS.get(chat_id)
return voice_chat


async def stop_voice_chat(chat_id):
voice_chats = load_voice_chats()
voice_chat = voice_chats.get(chat_id)
voice_chat = VOICE_CHATS.get(chat_id)

if voice_chat is None:
raise Exception("Streaming is not started")

del voice_chats[chat_id]
save_voice_chats(voice_chats)
del VOICE_CHATS[chat_id]

with open(VOICE_CHATS_FILE, "wb") as file:
pickle.dump(VOICE_CHATS, file)
3 changes: 2 additions & 1 deletion tlg_bot/kreacher/plugins/join.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from kreacher import kreacher
from kreacher.helpers.voice_chats import get_voice_chat
from kreacher.helpers.voice_chats import get_voice_chat, create_voice_chat
from telethon import events


Expand All @@ -10,6 +10,7 @@ async def join_handler(event):
call_py = await get_voice_chat(chat.id)
if call_py is not None:
raise Exception("Streaming is active")
await create_voice_chat(chat.id)
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

0 comments on commit 991e3d5

Please sign in to comment.