From 9b9b3a7575cfb6066dd32c18601770b41bce239a Mon Sep 17 00:00:00 2001 From: levina <82658782+levina-lab@users.noreply.github.com> Date: Sun, 6 Feb 2022 11:19:18 +0700 Subject: [PATCH] improvements - use async - restrict gbanned user --- program/downloader.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/program/downloader.py b/program/downloader.py index 860fb15..82e7079 100644 --- a/program/downloader.py +++ b/program/downloader.py @@ -26,6 +26,7 @@ from yt_dlp import YoutubeDL from config import BOT_USERNAME as bn from driver.decorators import humanbytes from driver.filters import command, other_filters +from driver.database.dbpunish import is_gbanned_user ydl_opts = { @@ -41,16 +42,20 @@ is_downloading = False @Client.on_message(command(["song", f"song@{bn}"]) & ~filters.edited) -def song(_, message): +async def song_downloader(_, message: Message): global is_downloading + user_id = message.from_user.id + if await is_gbanned_user(user_id): + await message.reply_text(f"โ—๏ธ **You've been blocked from using this bot!") + return query = " ".join(message.command[1:]) if is_downloading: - message.reply( + await message.reply_text( "ยป Other download is in progress, please try again after some time !" ) return is_downloading = True - m = message.reply("๐Ÿ”Ž finding song...") + m = await message.reply_text("๐Ÿ”Ž finding song...") ydl_ops = {"format": "bestaudio[ext=m4a]"} try: results = YoutubeSearch(query, max_results=1).to_dict() @@ -63,10 +68,10 @@ def song(_, message): duration = results[0]["duration"] except Exception as e: - m.edit("โŒ song not found.\n\nplease give a valid song name !") + await m.edit("โŒ song not found.\n\nยป Give me a valid song name !") print(str(e)) return - m.edit("๐Ÿ“ฅ downloading song...") + await m.edit("๐Ÿ“ฅ downloading song...") try: with yt_dlp.YoutubeDL(ydl_ops) as ydl: info_dict = ydl.extract_info(link, download=False) @@ -78,8 +83,8 @@ def song(_, message): for i in range(len(dur_arr) - 1, -1, -1): dur += int(float(dur_arr[i])) * secmul secmul *= 60 - m.edit("๐Ÿ“ค uploading song...") - message.reply_audio( + await m.edit("๐Ÿ“ค uploading song...") + await message.reply_audio( audio_file, caption=rep, performer=host, @@ -88,10 +93,10 @@ def song(_, message): title=title, duration=dur, ) - m.delete() + await m.delete() is_downloading = False except Exception as e: - m.edit("โŒ error, wait for bot owner to fix") + await m.edit("โŒ error, wait for bot owner to fix") print(e) try: @@ -104,8 +109,12 @@ def song(_, message): @Client.on_message( command(["vsong", f"vsong@{bn}", "video", f"video@{bn}"]) & ~filters.edited ) -async def vsong(client, message): +async def vsong(_, message: Message): global is_downloading + user_id = message.from_user.id + if await is_gbanned_user(user_id): + await message.reply_text(f"โ—๏ธ **You've been blocked from using this bot!") + return ydl_opts = { "format": "best", "keepvideo": True, @@ -159,6 +168,10 @@ async def vsong(client, message): @Client.on_message(command(["lyric", f"lyric@{bn}", "lyrics"])) async def get_lyric_genius(_, message: Message): + user_id = message.from_user.id + if await is_gbanned_user(user_id): + await message.reply_text(f"โ—๏ธ **You've been blocked from using this bot!") + return if len(message.command) < 2: return await message.reply_text("**usage:**\n\n/lyrics (song name)") m = await message.reply_text("๐Ÿ” Searching lyrics...")