From 52577e9bba9c1086d7cf7f3f7f033fcaa29a9992 Mon Sep 17 00:00:00 2001 From: Santiago Ramirez <94815926+SantiiRepair@users.noreply.github.com> Date: Sat, 5 Aug 2023 05:08:20 +0000 Subject: [PATCH] Update 7 files --- tlg_bot/kreacher/__init__.py | 1 + tlg_bot/kreacher/helpers/joiner.py | 8 --- tlg_bot/kreacher/helpers/voice_chats.py | 43 +++------------ tlg_bot/kreacher/plugins/callbacks.py | 12 ++-- tlg_bot/kreacher/plugins/join.py | 12 ++-- tlg_bot/kreacher/plugins/leave.py | 6 +- tlg_bot/kreacher/plugins/play_video.py | 73 ++++++++++++++----------- 7 files changed, 65 insertions(+), 90 deletions(-) delete mode 100644 tlg_bot/kreacher/helpers/joiner.py diff --git a/tlg_bot/kreacher/__init__.py b/tlg_bot/kreacher/__init__.py index 672ce20..900eb28 100644 --- a/tlg_bot/kreacher/__init__.py +++ b/tlg_bot/kreacher/__init__.py @@ -15,4 +15,5 @@ _bot = TelegramClient(None, api_id=config.API_ID, api_hash=config.API_HASH) kreacher = _bot.start(bot_token=config.BOT_TOKEN) client = TelegramClient(None, config.API_ID, config.API_HASH) +ins = GroupCallFactory(client, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON) client.start() diff --git a/tlg_bot/kreacher/helpers/joiner.py b/tlg_bot/kreacher/helpers/joiner.py deleted file mode 100644 index ff1cc34..0000000 --- a/tlg_bot/kreacher/helpers/joiner.py +++ /dev/null @@ -1,8 +0,0 @@ -from kreacher import kreacher, call_py -from telethon import events - - -@kreacher.on(events.NewMessage(outgoing=True, pattern="^[?!/]join")) -async def join_handler(event): - chat = await event.get_chat() - await call_py.start(chat.id) diff --git a/tlg_bot/kreacher/helpers/voice_chats.py b/tlg_bot/kreacher/helpers/voice_chats.py index 990a55d..a515e92 100644 --- a/tlg_bot/kreacher/helpers/voice_chats.py +++ b/tlg_bot/kreacher/helpers/voice_chats.py @@ -1,42 +1,13 @@ -import _pickle as pickle -from kreacher import client -from pytgcalls import GroupCallFactory +VOICE_CHATS = {} -VOICE_CHATS_FILE = "voice_chats.pkl" -try: - with open(VOICE_CHATS_FILE, "rb") as file: - VOICE_CHATS = pickle.load(file) -except (FileNotFoundError, EOFError): - VOICE_CHATS = {} +def get_voice_chat(chat): + return VOICE_CHATS.get(chat.id) -async def create_voice_chat(chat_id): - factory = GroupCallFactory(client, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON) - voice_chat = factory.get_group_call() - print(dir(voice_chat)) - get = VOICE_CHATS.get(chat_id) - if get is not None: - raise Exception("I'm joined in the Voice Chat") +def start_voice_chat(chat, instance): + VOICE_CHATS[chat.id] = instance - 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_chat = VOICE_CHATS.get(chat_id) - 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") - - del VOICE_CHATS[chat_id] - - with open(VOICE_CHATS_FILE, "wb") as file: - pickle.dump(VOICE_CHATS, file) +async def stop_voice_chat(chat): + VOICE_CHATS.pop(chat.id) diff --git a/tlg_bot/kreacher/plugins/callbacks.py b/tlg_bot/kreacher/plugins/callbacks.py index 88212a1..f9f76bc 100644 --- a/tlg_bot/kreacher/plugins/callbacks.py +++ b/tlg_bot/kreacher/plugins/callbacks.py @@ -11,19 +11,19 @@ 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) + proto = get_voice_chat(chat) + await proto.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) + proto = get_voice_chat(chat) + await proto.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() \ No newline at end of file + proto = get_voice_chat(chat) + await proto.stop_media() \ No newline at end of file diff --git a/tlg_bot/kreacher/plugins/join.py b/tlg_bot/kreacher/plugins/join.py index 3770511..eab4b3d 100644 --- a/tlg_bot/kreacher/plugins/join.py +++ b/tlg_bot/kreacher/plugins/join.py @@ -1,5 +1,5 @@ -from kreacher import kreacher -from kreacher.helpers.voice_chats import get_voice_chat, create_voice_chat +from kreacher import ins, kreacher +from kreacher.helpers.voice_chats import get_voice_chat, start_voice_chat from telethon import events @@ -7,11 +7,11 @@ 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: + proto = get_voice_chat(chat) + if proto is not None: raise Exception("Streaming is active") - await create_voice_chat(chat.id) - await call_py.start(chat.id) + start_voice_chat(chat, ins) + await proto.join(chat) await event.reply("Master, what do you need? \n\nVoice Chat joined successfully.", parse_mode="HTML") except Exception as e: return await event.reply(f"Oops master, something wrong has happened. \n\nError: {e}", parse_mode="HTML") diff --git a/tlg_bot/kreacher/plugins/leave.py b/tlg_bot/kreacher/plugins/leave.py index a2e834a..e2da8d4 100644 --- a/tlg_bot/kreacher/plugins/leave.py +++ b/tlg_bot/kreacher/plugins/leave.py @@ -7,10 +7,10 @@ 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: + proto = get_voice_chat(chat) + if proto is None: raise Exception("Streaming is not active") - await call_py.leave_current_group_call() + await proto.leave_current_group_call() await event.reply("Goodbye master, just call me if you need me. \n\nVoice Chat left successfully.", parse_mode="HTML") except Exception as e: return await event.reply(f"Oops master, something wrong has happened. \n\nError: {e}", parse_mode="HTML") diff --git a/tlg_bot/kreacher/plugins/play_video.py b/tlg_bot/kreacher/plugins/play_video.py index b6f9202..1bc94a1 100644 --- a/tlg_bot/kreacher/plugins/play_video.py +++ b/tlg_bot/kreacher/plugins/play_video.py @@ -1,14 +1,21 @@ import re from youtubesearchpython import VideosSearch -from kreacher import kreacher +from kreacher import ins, 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 kreacher.helpers.voice_chats import start_voice_chat, get_voice_chat, stop_voice_chat from telethon import Button, events from asyncio import sleep -from yt_dlp import YoutubeDL as ydl +from yt_dlp import YoutubeDL fotoplay = "https://telegra.ph/file/b6402152be44d90836339.jpg" ngantri = "https://telegra.ph/file/b6402152be44d90836339.jpg" +ydl_opts = { + "quiet": True, + "geo_bypass": True, + "nocheckcertificate": True, +} +ydl = YoutubeDL(ydl_opts) + @kreacher.on(events.NewMessage(pattern="^[?!/]play_video")) async def play_video(event): @@ -25,14 +32,14 @@ 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) - call_py = await get_voice_chat(chat.id) - if call_py is None or call_py is False: + proto = get_voice_chat(chat) + if proto is None: await msg.edit("Joining the voice chat...", parse_mode="HTML") - await create_voice_chat(chat.id) + start_voice_chat(chat.id, ins) if match: await msg.edit("🔄 Starting YouTube Video Stream...", parse_mode="HTML") try: - meta = ydl().extract_info(url=url, download=False) + meta = ydl.extract_info(url=url, download=False) formats = meta.get('formats', [meta]) for f in formats: ytstreamlink = f['url'] @@ -46,8 +53,8 @@ async def play_video(event): except Exception as e: await msg.edit(f"❌ **YouTube Download Error !** \n\n`{e}`") print(e) - await stop_voice_chat(chat.id) - return await call_py.stop() + stop_voice_chat(chat) + return await proto.stop() else: await msg.edit("🔄 `Starting Live Video Stream ...`") @@ -56,21 +63,22 @@ async def play_video(event): try: await sleep(2) - await call_py.start_video(link, with_audio=True, repeat=False) + await ins.join(chat.id) + await ins.start_video(link, with_audio=True, repeat=False) await msg.delete() await event.reply( f"▶️ **Started [Video Streaming]({url})!**", file=thumb, buttons=[ - [Button.inline("⏸", data="pause_callback"), - Button.inline("▶️", data="resume_callback")], - [Button.inline("⏹️", data="end_callback")], - ], + [Button.inline("⏸ Pause", data="pause_callback"), + Button.inline("▶️ Resume", data="resume_callback")], + [Button.inline("⏹️ Stop", data="end_callback")], + ] ) 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() + stop_voice_chat(chat.id) + return await proto.stop() elif media.video or media.file: await msg.edit("🔄 `Downloading ...`") @@ -85,22 +93,23 @@ async def play_video(event): try: await sleep(2) - await call_py.start_video(video, with_audio=True, repeat=False) + await ins.join(chat.id) + await ins.start_video(video, with_audio=True, repeat=False) await msg.delete() await event.reply( f"▶️ **Started [Video Streaming](https://t.me/AsmSafone)!**", file=thumb, buttons=[ - [Button.inline("⏸", data="pause_callback"), - Button.inline("▶️", data="resume_callback")], - [Button.inline("⏹️", data="end_callback")], + [Button.inline("⏸ Pause", data="pause_callback"), + Button.inline("▶️ Resume", data="resume_callback")], + [Button.inline("⏹️ Stop", data="end_callback")], ] ) 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() + return await proto.stop() else: await msg.edit("💁🏻‍♂️ Do you want to search for a YouTube video?") @@ -108,15 +117,15 @@ async def play_video(event): @kreacher.on(events.NewMessage(pattern="^[?!/]playlist")) async def playlist(event, perm): - chat_id = event.chat_id + chat = event.get_chat() user = event.get_sender() if not user.is_admin: await event.reply( "Sorry, you must be an administrator to execute this command." ) return - if chat_id in QUEUE: - chat_queue = get_queue(chat_id) + if chat.id in QUEUE: + chat_queue = get_queue(chat.id) if len(chat_queue) == 1: await event.reply( f"**�PlAYLIST:**\n• [{chat_queue[0][0]}]({chat_queue[0][2]}) | `{chat_queue[0][3]}`", @@ -139,16 +148,17 @@ async def playlist(event, perm): @kreacher.on(events.NewMessage(pattern="^[?!/]pause")) async def pause(event, perm): - chat_id = event.chat_id + chat = event.get_chat() user = event.get_sender() if not user.is_admin: await event.reply( "Sorry, you must be an administrator to execute this command." ) return - if chat_id in QUEUE: + if chat.id in QUEUE: + proto = get_voice_chat(chat) try: - #await call_py.pause_stream(chat_id) + await proto.pause_stream(chat.id) await event.reply("**Streaming Paused**") except Exception as e: await event.reply(f"**ERROR:** `{e}`") @@ -157,17 +167,18 @@ async def pause(event, perm): @kreacher.on(events.NewMessage(pattern="^[?!/]resume")) -async def vc_resume(event, perm): - chat_id = event.chat_id +async def resume(event, perm): + chat = event.get_chat() user = event.get_sender() if not user.is_admin: await event.reply( "Sorry, you must be an administrator to execute this command." ) return - if chat_id in QUEUE: + if chat.id in QUEUE: + proto = get_voice_chat(chat) try: - #await call_py.resume_stream(chat_id) + await proto.resume_stream(chat.id) await event.reply("**Streaming Started Back 🔙**") except Exception as e: await event.reply(f"**ERROR:** `{e}`")