From e19a231a37a1575722177bdfe5b8db0ed48bef84 Mon Sep 17 00:00:00 2001 From: 5hojib Date: Sun, 29 Dec 2024 22:02:21 +0600 Subject: [PATCH] cancel regex, attempt to fix tg download, fix PORT logic for VPS --- bot/core/handlers.py | 7 +- bot/core/startup.py | 2 +- bot/helper/ext_utils/status_utils.py | 2 +- .../download_utils/telegram_download.py | 17 +--- bot/helper/telegram_helper/bot_commands.py | 85 +++++++++---------- bot/helper/telegram_helper/message_utils.py | 22 ++--- bot/modules/cancel_task.py | 15 ++-- bot/modules/mirror_leech.py | 2 +- config_sample.py | 2 +- 9 files changed, 69 insertions(+), 85 deletions(-) diff --git a/bot/core/handlers.py b/bot/core/handlers.py index 9b5e6abba..5823a68eb 100644 --- a/bot/core/handlers.py +++ b/bot/core/handlers.py @@ -28,7 +28,6 @@ def add_handlers(): BotCommands.BotSetCommand, CustomFilters.sudo, ), - "cancel": (cancel, BotCommands.CancelTaskCommand, CustomFilters.authorized), "cancel_all_buttons": ( cancel_all_buttons, BotCommands.CancelAllCommand, @@ -151,3 +150,9 @@ def add_handlers(): & CustomFilters.owner, ), ) + TgClient.bot.add_handler( + MessageHandler( + cancel, + filters=regex(r"^/stop(_\w+)?(?!all)") & CustomFilters.authorized, + ), + ) diff --git a/bot/core/startup.py b/bot/core/startup.py index bc038c882..2067d9574 100644 --- a/bot/core/startup.py +++ b/bot/core/startup.py @@ -217,7 +217,7 @@ async def load_configurations(): subprocess.run(["chmod", "600", ".netrc"], check=False) subprocess.run(["cp", ".netrc", "/root/.netrc"], check=False) - PORT = environ.get("BASE_URL_PORT") or environ.get("PORT") + PORT = environ.get("PORT") or environ.get("BASE_URL_PORT", 80) await create_subprocess_shell( f"gunicorn web.wserver:app --bind 0.0.0.0:{PORT} --worker-class gevent", ) diff --git a/bot/helper/ext_utils/status_utils.py b/bot/helper/ext_utils/status_utils.py index 642d60a24..c6f58053e 100644 --- a/bot/helper/ext_utils/status_utils.py +++ b/bot/helper/ext_utils/status_utils.py @@ -228,7 +228,7 @@ async def get_readable_message(sid, is_user, page_no=1, status="All", page_step= msg += f" | Time: {task.seeding_time()}" else: msg += f"\nSize: {task.size()}" - msg += f"\nGid: {task.gid()}\n\n" + msg += f"\n/stop_{task.gid()}\n\n" if len(msg) == 0: if status == "All": diff --git a/bot/helper/mirror_leech_utils/download_utils/telegram_download.py b/bot/helper/mirror_leech_utils/download_utils/telegram_download.py index 0fca3acba..f411b480f 100644 --- a/bot/helper/mirror_leech_utils/download_utils/telegram_download.py +++ b/bot/helper/mirror_leech_utils/download_utils/telegram_download.py @@ -94,23 +94,10 @@ async def _download(self, message, path): async def add_download(self, message, path, session): self.session = session - if ( - self.session is None - and self._listener.user_transmission - and self._listener.is_super_chat - ): - self.session = "user" - message = await TgClient.user.get_messages( - chat_id=message.chat.id, - message_ids=message.id, - ) - elif self.session and self.session != TgClient.bot: + if self.session != TgClient.bot: message = await self.session.get_messages( - chat_id=message.chat.id, - message_ids=message.id, + chat_id=message.chat.id, message_ids=message.id ) - else: - self.session = "bot" media = ( message.document diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index 73ef93c8b..e3ea60adf 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -1,64 +1,61 @@ from bot.core.config_manager import Config +i = Config.CMD_SUFFIX class _BotCommands: def __init__(self): - self.StartCommand = f"start{Config.CMD_SUFFIX}" - self.MirrorCommand = [f"mirror{Config.CMD_SUFFIX}", f"m{Config.CMD_SUFFIX}"] + self.StartCommand = f"start{i}" + self.MirrorCommand = [f"mirror{i}", f"m{i}"] self.QbMirrorCommand = [ - f"qbmirror{Config.CMD_SUFFIX}", - f"qm{Config.CMD_SUFFIX}", + f"qbmirror{i}", + f"qm{i}", ] - self.YtdlCommand = [f"ytdl{Config.CMD_SUFFIX}", f"y{Config.CMD_SUFFIX}"] - self.LeechCommand = [f"leech{Config.CMD_SUFFIX}", f"l{Config.CMD_SUFFIX}"] + self.YtdlCommand = [f"ytdl{i}", f"y{i}"] + self.LeechCommand = [f"leech{i}", f"l{i}"] self.QbLeechCommand = [ - f"qbleech{Config.CMD_SUFFIX}", - f"ql{Config.CMD_SUFFIX}", + f"qbleech{i}", + f"ql{i}", ] self.YtdlLeechCommand = [ - f"ytdlleech{Config.CMD_SUFFIX}", - f"yl{Config.CMD_SUFFIX}", + f"ytdlleech{i}", + f"yl{i}", ] - self.CloneCommand = f"clone{Config.CMD_SUFFIX}" - self.CountCommand = f"count{Config.CMD_SUFFIX}" - self.DeleteCommand = f"del{Config.CMD_SUFFIX}" - self.CancelTaskCommand = [ - f"cancel{Config.CMD_SUFFIX}", - f"c{Config.CMD_SUFFIX}", - ] - self.CancelAllCommand = f"cancelall{Config.CMD_SUFFIX}" + self.CloneCommand = f"clone{i}" + self.CountCommand = f"count{i}" + self.DeleteCommand = f"del{i}" + self.CancelAllCommand = f"cancelall{i}" self.ForceStartCommand = [ - f"forcestart{Config.CMD_SUFFIX}", - f"fs{Config.CMD_SUFFIX}", + f"forcestart{i}", + f"fs{i}", ] - self.ListCommand = f"list{Config.CMD_SUFFIX}" - self.SearchCommand = f"search{Config.CMD_SUFFIX}" - self.StatusCommand = f"status{Config.CMD_SUFFIX}" - self.UsersCommand = f"users{Config.CMD_SUFFIX}" - self.AuthorizeCommand = f"authorize{Config.CMD_SUFFIX}" - self.UnAuthorizeCommand = f"unauthorize{Config.CMD_SUFFIX}" - self.AddSudoCommand = f"addsudo{Config.CMD_SUFFIX}" - self.RmSudoCommand = f"rmsudo{Config.CMD_SUFFIX}" - self.PingCommand = f"ping{Config.CMD_SUFFIX}" - self.RestartCommand = f"restart{Config.CMD_SUFFIX}" - self.RestartSessionsCommand = f"restartses{Config.CMD_SUFFIX}" - self.StatsCommand = f"stats{Config.CMD_SUFFIX}" - self.HelpCommand = f"help{Config.CMD_SUFFIX}" - self.LogCommand = f"log{Config.CMD_SUFFIX}" - self.ShellCommand = f"shell{Config.CMD_SUFFIX}" - self.AExecCommand = f"aexec{Config.CMD_SUFFIX}" - self.ExecCommand = f"exec{Config.CMD_SUFFIX}" - self.ClearLocalsCommand = f"clearlocals{Config.CMD_SUFFIX}" + self.ListCommand = f"list{i}" + self.SearchCommand = f"search{i}" + self.StatusCommand = f"status{i}" + self.UsersCommand = f"users{i}" + self.AuthorizeCommand = f"authorize{i}" + self.UnAuthorizeCommand = f"unauthorize{i}" + self.AddSudoCommand = f"addsudo{i}" + self.RmSudoCommand = f"rmsudo{i}" + self.PingCommand = f"ping{i}" + self.RestartCommand = f"restart{i}" + self.RestartSessionsCommand = f"restartses{i}" + self.StatsCommand = f"stats{i}" + self.HelpCommand = f"help{i}" + self.LogCommand = f"log{i}" + self.ShellCommand = f"shell{i}" + self.AExecCommand = f"aexec{i}" + self.ExecCommand = f"exec{i}" + self.ClearLocalsCommand = f"clearlocals{i}" self.BotSetCommand = [ - f"bsetting{Config.CMD_SUFFIX}", - f"bs{Config.CMD_SUFFIX}", + f"bsetting{i}", + f"bs{i}", ] self.UserSetCommand = [ - f"usetting{Config.CMD_SUFFIX}", - f"us{Config.CMD_SUFFIX}", + f"usetting{i}", + f"us{i}", ] - self.SelectCommand = f"sel{Config.CMD_SUFFIX}" - self.RssCommand = f"rss{Config.CMD_SUFFIX}" + self.SelectCommand = f"sel{i}" + self.RssCommand = f"rss{i}" BotCommands = _BotCommands() diff --git a/bot/helper/telegram_helper/message_utils.py b/bot/helper/telegram_helper/message_utils.py index 77a8a95e4..14e559d7f 100644 --- a/bot/helper/telegram_helper/message_utils.py +++ b/bot/helper/telegram_helper/message_utils.py @@ -188,26 +188,26 @@ async def delete_status(): async def get_tg_link_message(link, user_id=""): message = None links = [] - user_s = None + user_session = None if user_id: if user_id in session_cache: - user_s = session_cache[user_id] + user_session = session_cache[user_id] else: user_dict = user_data.get(user_id, {}) session_string = user_dict.get("session_string") if session_string: - user_s = Client( + user_session = Client( f"session_{user_id}", Config.TELEGRAM_API, Config.TELEGRAM_HASH, session_string=session_string, no_updates=True, ) - await user_s.start() - session_cache[user_id] = user_s + await user_session.start() + session_cache[user_id] = user_session else: - user_s = TgClient.user + user_session = TgClient.user if link.startswith("https://t.me/"): private = False @@ -221,7 +221,7 @@ async def get_tg_link_message(link, user_id=""): r"tg:\/\/openmessage\?user_id=([0-9]+)&message_id=([0-9-]+)", link, ) - if not TgClient.user: + if not user_session: raise TgLinkException( "USER_SESSION_STRING required for this private link!", ) @@ -260,21 +260,21 @@ async def get_tg_link_message(link, user_id=""): private = True except Exception as e: private = True - if not user_s: + if not user_session: raise e if not private: return (links, TgClient.bot) if links else (message, TgClient.bot) - if user_s: + if user_session: try: - user_message = await user_s.get_messages( + user_message = await user_session.get_messages( chat_id=chat, message_ids=msg_id, ) except Exception as e: raise TgLinkException("We don't have access to this chat!") from e if not user_message.empty: - return (links, user_s) if links else (user_message, user_s) + return (links, user_session) if links else (user_message, user_session) return None raise TgLinkException("Private: Please report!") diff --git a/bot/modules/cancel_task.py b/bot/modules/cancel_task.py index 77533b133..eba886563 100644 --- a/bot/modules/cancel_task.py +++ b/bot/modules/cancel_task.py @@ -22,33 +22,28 @@ @new_task async def cancel(_, message): user_id = message.from_user.id if message.from_user else message.sender_chat.id - msg = message.text.split() + msg = message.text.split("_", maxsplit=1) + await delete_message(message) if len(msg) > 1: - gid = msg[1] + gid = msg[1].split("@", maxsplit=1) + gid = gid[0] if len(gid) == 4: multi_tags.discard(gid) return task = await get_task_by_gid(gid) if task is None: - await send_message(message, f"GID: {gid} Not Found.") + await delete_message(message) return elif reply_to_id := message.reply_to_message_id: async with task_dict_lock: task = task_dict.get(reply_to_id) if task is None: - await send_message(message, "This is not an active task!") return elif len(msg) == 1: - msg = ( - "Reply to an active Command message which was used to start the download" - f" or send /{BotCommands.CancelTaskCommand[0]} GID to cancel it!" - ) - await send_message(message, msg) return if user_id not in (Config.OWNER_ID, task.listener.user_id) and ( user_id not in user_data or not user_data[user_id].get("is_sudo") ): - await send_message(message, "This task is not for you!") return obj = task.task() await obj.cancel_task() diff --git a/bot/modules/mirror_leech.py b/bot/modules/mirror_leech.py index c1aed0eb7..f418eb730 100644 --- a/bot/modules/mirror_leech.py +++ b/bot/modules/mirror_leech.py @@ -155,7 +155,7 @@ async def new_event(self): seed_time = None reply_to = None file_ = None - session = "" + session = TgClient.bot try: self.multi = int(args["-i"]) diff --git a/config_sample.py b/config_sample.py index 98079a893..60c1206df 100644 --- a/config_sample.py +++ b/config_sample.py @@ -64,7 +64,7 @@ # qBittorrent/Aria2c TORRENT_TIMEOUT = 0 BASE_URL = "" -BASE_URL_PORT = 0 +BASE_URL_PORT = 80 WEB_PINCODE = False # Queueing system