Skip to content

Commit

Permalink
Update 7 files
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Aug 5, 2023
1 parent 7ed45e6 commit 52577e9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 90 deletions.
1 change: 1 addition & 0 deletions tlg_bot/kreacher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
8 changes: 0 additions & 8 deletions tlg_bot/kreacher/helpers/joiner.py

This file was deleted.

43 changes: 7 additions & 36 deletions tlg_bot/kreacher/helpers/voice_chats.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 6 additions & 6 deletions tlg_bot/kreacher/plugins/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
proto = get_voice_chat(chat)
await proto.stop_media()
12 changes: 6 additions & 6 deletions tlg_bot/kreacher/plugins/join.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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


@kreacher.on(events.NewMessage(pattern="[!?/]join"))
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("<i>Master, what do you need? \n\nVoice Chat joined successfully.</i>", parse_mode="HTML")
except Exception as e:
return await event.reply(f"<i>Oops master, something wrong has happened. \n\nError:</i> <code>{e}</code>", parse_mode="HTML")
6 changes: 3 additions & 3 deletions tlg_bot/kreacher/plugins/leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<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:</i> <code>{e}</code>", parse_mode="HTML")
73 changes: 42 additions & 31 deletions tlg_bot/kreacher/plugins/play_video.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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("<i>Joining the voice chat...</i>", parse_mode="HTML")
await create_voice_chat(chat.id)
start_voice_chat(chat.id, ins)
if match:
await msg.edit("🔄 <i>Starting YouTube Video Stream...</i>", 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']
Expand All @@ -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 ...`")
Expand All @@ -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 ...`")
Expand All @@ -85,38 +93,39 @@ 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?")


@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]}`",
Expand All @@ -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}`")
Expand All @@ -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}`")
Expand Down

0 comments on commit 52577e9

Please sign in to comment.