Skip to content

Commit

Permalink
Updated deps
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Aug 7, 2023
1 parent 3961eeb commit 43214fb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bot/callbacks/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from bot import config, kreacher
from bot.instance.of_every_vc import VOICE_CHATS
from telethon import events, Button
from bot.handler.handler import next_item, skip_current
from bot.helpers.handler import next_item, skip_current

thumb = "https://telegra.ph/file/3e14128ad5c9ec47801bd.jpg"

Expand Down
13 changes: 12 additions & 1 deletion bot/handler/handler.py → bot/helpers/handler.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from bot.instance.of_every_vc import QUEUE, VOICE_CHATS
import os
import pickle
from bot.instance.of_every_vc import VOICE_CHATS
from bot.helpers.queues import (
clear_queue,
get_queue,
pop_an_item,
active,
)

dir = os.path.dirname(os.path.abspath(__file__))
queues = os.path.join(dir, "../dbs/queues.pkl")


async def skip_current(chat):
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if chat.id not in QUEUE:
return 0
chat_queue = get_queue(chat)
Expand All @@ -33,12 +40,16 @@ async def skip_current(chat):


async def next_item(chat, x: int):
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if chat.id not in QUEUE:
return 0
chat_queue = get_queue(chat)
try:
name = chat_queue[x][0]
chat_queue.pop(x)
with open(queues, "w") as q:
pickle.dump(chat_queue, q)
return name
except Exception as e:
print(e)
Expand Down
1 change: 0 additions & 1 deletion bot/instance/of_every_vc.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
QUEUE = {}
VOICE_CHATS = {}
27 changes: 16 additions & 11 deletions bot/plugins/play_song.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import uuid
import pickle
from asyncio import sleep

# from bot.helpers.thumbnail import gen_thumb
from telethon import Button, events
from bot.instance.of_every_vc import QUEUE, VOICE_CHATS
from bot.instance.of_every_vc import VOICE_CHATS
from bot.helpers.queues import (
add_to_queue,
clear_queue,
Expand All @@ -20,6 +21,10 @@
ngantri = "https://telegra.ph/file/b6402152be44d90836339.jpg"
owner = "1669178360"

dir = os.path.dirname(os.path.abspath(__file__))
download_as = os.path.join(dir, f"../downloads/songs/{str(uuid.uuid4())}")
queues = os.path.join(dir, "../dbs/queues.pkl")


def vcmention(user):
full_name = get_display_name(user)
Expand All @@ -32,12 +37,12 @@ def ytsearch(query: str):
try:
search = VideosSearch(query, limit=1).result()
data = search["result"][0]
songname = data["title"]
name = data["title"]
url = data["link"]
duration = data["duration"]
thumbnail = f"https://i.ytimg.com/vi/{data['id']}/hqdefault.jpg"
videoid = data["id"]
return [songname, url, duration, thumbnail, videoid]
return [name, url, duration, thumbnail, videoid]
except Exception as e:
print(e)
return 0
Expand All @@ -56,9 +61,9 @@ async def play_song(event):
replied = await event.get_reply_message()
chat = await event.get_chat()
msg = await event.reply("🔄 **__Processing...__**")
dir = os.path.dirname(os.path.abspath(__file__))
download_as = os.path.join(dir, f"../downloads/songs/{str(uuid.uuid4())}")
await sleep(2)
with open(queues, "r") as q:
QUEUE = pickle.load(q)
from_user = vcmention(event.sender)
if (
replied
Expand All @@ -78,14 +83,14 @@ async def play_song(event):
await msg.edit("__Joining the voice chat...__")
await ins.join(chat.id)
VOICE_CHATS[chat.id] = ins
await sleep(3)
await sleep(2)
except Exception as e:
await msg.edit(
f"__Oops master, something wrong has happened. \n\nError:__ `{e}`",
)
await VOICE_CHATS[chat.id].stop()
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)
if replied and not replied.audio and not replied.voice or not replied:
query = event.text.split(maxsplit=1)[1]
search = ytsearch(query)
Expand Down Expand Up @@ -115,7 +120,7 @@ async def play_song(event):
try:
await ins.start_audio(url, repeat=False)
add_to_queue(chat, name, url, ref, "audio")
await sleep(3)
await sleep(2)
await msg.edit(
f"**__Started Streaming__**\n\n **Title**: [{name}]({url})\n **Duration:** {duration} **Minutes\n Requested by:** {from_user}",
# file=thumb,
Expand All @@ -138,7 +143,7 @@ async def play_song(event):
await VOICE_CHATS[chat.id].stop()
await msg.edit(f"`{e}`")
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)

else:
await msg.edit("➕ __Downloading...__")
Expand Down Expand Up @@ -166,7 +171,7 @@ async def play_song(event):
)
else:
try:
await sleep(3)
await sleep(2)
await ins.start_audio(dl, repeat=False)
await msg.edit(
f"**__Started Streaming__**\n\n **Title:** [{name}]({link})\n **Requested by:** {from_user}",
Expand All @@ -189,4 +194,4 @@ async def play_song(event):
await VOICE_CHATS[chat.id].stop()
await msg.edit(f"`{e}`")
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)
29 changes: 20 additions & 9 deletions bot/plugins/play_video.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import re
import uuid
import pickle
from asyncio import sleep
from youtubesearchpython import VideosSearch
from bot import client, ins, kreacher
from bot.helpers.queues import get_queue
from bot.instance.of_every_vc import QUEUE, VOICE_CHATS
from bot.instance.of_every_vc import VOICE_CHATS
from telethon import Button, events

from yt_dlp import YoutubeDL
Expand All @@ -14,6 +15,10 @@
ngantri = "https://telegra.ph/file/b6402152be44d90836339.jpg"
thumb = "https://telegra.ph/file/3e14128ad5c9ec47801bd.jpg"

dir = os.path.dirname(os.path.abspath(__file__))
download_as = os.path.join(dir, f"../downloads/videos/{str(uuid.uuid4())}")
queues = os.path.join(dir, "../dbs/queues.pkl")

ydl_opts = {
"quiet": True,
"geo_bypass": True,
Expand All @@ -28,8 +33,8 @@ async def play_video(event):
media = await event.get_reply_message()
msg = await event.reply("🔄 **__Processing...__**")
await sleep(2)
dir = os.path.dirname(os.path.abspath(__file__))
download_as = os.path.join(dir, f"../downloads/videos/{str(uuid.uuid4())}")
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if not media and not " " in event.message.message:
await msg.edit(
"❗ __Master, try with an: \n\nLive stream link.\n\nYouTube video link.\n\nReply to an video to start video streaming!__",
Expand All @@ -49,14 +54,14 @@ async def play_video(event):
await msg.edit("__Joining the voice chat...__")
await ins.join(chat.id)
VOICE_CHATS[chat.id] = ins
await sleep(3)
await sleep(2)
except Exception as e:
await msg.edit(
f"__Oops master, something wrong has happened. \n\nError:__ `{e}`",
)
await VOICE_CHATS[chat.id].stop()
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)
if match:
await msg.edit("🔄 __Starting YouTube video stream...__")
try:
Expand All @@ -77,7 +82,7 @@ async def play_video(event):
print(e)
await VOICE_CHATS[chat.id].stop()
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)

else:
await msg.edit("🔄 __Starting live video stream...__")
Expand Down Expand Up @@ -106,7 +111,7 @@ async def play_video(event):
)
await VOICE_CHATS[chat.id].stop()
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)

elif media.video or media.file:
await msg.edit("🔄 __Downloading...__")
Expand Down Expand Up @@ -137,19 +142,21 @@ async def play_video(event):
print(e)
await VOICE_CHATS[chat.id].stop()
VOICE_CHATS.pop(chat.id)
return await sleep(3)
return await sleep(2)

else:
await msg.edit(
"__\U0001F9D9 Do you want to search for a YouTube video?__"
)
return await sleep(3)
return await sleep(2)


@kreacher.on(events.NewMessage(pattern="^[!?/]playlist"))
async def playlist(event):
chat = event.get_chat()
user = event.get_sender()
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if not user.is_admin:
await event.reply(
"Sorry, you must be an administrator to execute this command."
Expand Down Expand Up @@ -181,6 +188,8 @@ async def playlist(event):
async def pause(event):
chat = event.get_chat()
user = event.get_sender()
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if not user.is_admin:
await event.reply(
"Sorry, you must be an administrator to execute this command."
Expand All @@ -200,6 +209,8 @@ async def pause(event):
async def resume(event):
chat = event.get_chat()
user = event.get_sender()
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if not user.is_admin:
await event.reply(
"Sorry, you must be an administrator to execute this command."
Expand Down
20 changes: 18 additions & 2 deletions bot/tasks/background.py → bot/tasks/bot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import os
import pickle
from bot import ins
from pytgcalls.types import Update
from bot.handler.handler import skip_current
from bot.instance.of_every_vc import QUEUE
from bot.helpers.handler import skip_current
from bot.helpers.queues import (
clear_queue,
active,
)

dir = os.path.dirname(os.path.abspath(__file__))
queues = os.path.join(dir, "../dbs/queues.pkl")


@ins.on_stream_end()
async def stream_end_handler(_, u: Update):
Expand All @@ -16,23 +20,35 @@ async def stream_end_handler(_, u: Update):

@ins.on_closed_voice_chat()
async def closed(_, chat):
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if chat.id in QUEUE:
clear_queue(chat)
if chat.id in active:
active.remove(chat.id)
with open(actives, "w") as a:
pickle.dump(active, a)


@ins.on_left()
async def left(_, chat):
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if chat.id in QUEUE:
clear_queue(chat)
if chat.id in active:
active.remove(chat.id)
with open(actives, "w") as a:
pickle.dump(active, a)


@ins.on_kicked()
async def kicked(_, chat):
with open(queues, "r") as q:
QUEUE = pickle.load(q)
if chat.id in QUEUE:
clear_queue(chat)
if chat.id in active:
active.remove(chat.id)
with open(actives, "w") as a:
pickle.dump(active, a)

0 comments on commit 43214fb

Please sign in to comment.