diff --git a/bot/__init__.py b/bot/__init__.py index 808a7248bab..865f4e25b4a 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -297,6 +297,6 @@ def mktable(): IMAGE_URL = 'https://telegra.ph/file/db03910496f06094f1f7a.jpg' -updater = tg.Updater(token=BOT_TOKEN, use_context=True) +updater = tg.Updater(token=BOT_TOKEN) bot = updater.bot dispatcher = updater.dispatcher diff --git a/bot/__main__.py b/bot/__main__.py index c80e1cb63f8..6c7269b6342 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -10,7 +10,7 @@ import time from telegram import ParseMode, BotCommand -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from bot import bot, dispatcher, updater, botStartTime, IMAGE_URL from bot.helper.ext_utils import fs_utils from bot.helper.telegram_helper.bot_commands import BotCommands @@ -23,7 +23,6 @@ now=datetime.now(pytz.timezone('Asia/Jakarta')) -@run_async def stats(update, context): currentTime = get_readable_time(time.time() - botStartTime) current = now.strftime('%Y/%m/%d %I:%M:%S %p') @@ -49,7 +48,6 @@ def stats(update, context): update.effective_message.reply_photo(IMAGE_URL, stats, parse_mode=ParseMode.HTML) -@run_async def start(update, context): start_string = f''' This bot can mirror all your links to Google Drive! @@ -69,7 +67,6 @@ def start(update, context): sendMessage(f"Oops! not a Authorized user.", context.bot, update) -@run_async def restart(update, context): restart_message = sendMessage("Restarting, Please wait!", context.bot, update) LOGGER.info(f'Restarting the Bot...') @@ -81,7 +78,6 @@ def restart(update, context): os.execl(executable, executable, "-m", "bot") -@run_async def ping(update, context): start_time = int(round(time.time() * 1000)) reply = sendMessage("Starting Ping", context.bot, update) @@ -89,12 +85,10 @@ def ping(update, context): editMessage(f'{end_time - start_time} ms', reply) -@run_async def log(update, context): sendLogFile(context.bot, update) -@run_async def bot_help(update, context): help_string_adm = f''' /{BotCommands.HelpCommand}: To get this message @@ -222,16 +216,16 @@ def main(): os.remove(".restartmsg") bot.set_my_commands(botcmds) - start_handler = CommandHandler(BotCommands.StartCommand, start) + start_handler = CommandHandler(BotCommands.StartCommand, start, run_async=True) ping_handler = CommandHandler(BotCommands.PingCommand, ping, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) restart_handler = CommandHandler(BotCommands.RestartCommand, restart, - filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) help_handler = CommandHandler(BotCommands.HelpCommand, - bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) stats_handler = CommandHandler(BotCommands.StatsCommand, - stats, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) - log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + stats, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) + log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) dispatcher.add_handler(start_handler) dispatcher.add_handler(ping_handler) dispatcher.add_handler(restart_handler) diff --git a/bot/helper/mirror_utils/download_utils/aria2_download.py b/bot/helper/mirror_utils/download_utils/aria2_download.py index 41b3f1a3ba9..a83b5827c99 100644 --- a/bot/helper/mirror_utils/download_utils/aria2_download.py +++ b/bot/helper/mirror_utils/download_utils/aria2_download.py @@ -23,9 +23,9 @@ def __onDownloadStarted(self, api, gid): self.name = download.name sname = download.name if STOP_DUPLICATE_MIRROR: - if dl.getListener().isTar == True: + if self.listener.isTar: sname = sname + ".tar" - if dl.getListener().extract == True: + if self.listener.extract: smsg = None else: gdrive = GoogleDriveHelper(None) @@ -102,3 +102,4 @@ def add_download(self, link: str, path, listener, filename): with download_dict_lock: download_dict[listener.uid] = AriaDownloadStatus(download.gid, listener) LOGGER.info(f"Started: {download.gid} DIR:{download.dir} ") + self.listener = listener diff --git a/bot/helper/mirror_utils/download_utils/mega_downloader.py b/bot/helper/mirror_utils/download_utils/mega_downloader.py index f674ed361ec..4c6faa74409 100644 --- a/bot/helper/mirror_utils/download_utils/mega_downloader.py +++ b/bot/helper/mirror_utils/download_utils/mega_downloader.py @@ -167,9 +167,9 @@ def add_download(mega_link: str, path: str, listener): msg = sendMessage('Check the File/Folder if already in Drive...', listener.bot, listener.update) LOGGER.info(f'Check the File/Folder if already in Drive') mname = node.getName() - if listener.isTar == True: + if listener.isTar: mname = mname + ".tar" - if listener.extract == True: + if listener.extract: smsg = None else: gd = GoogleDriveHelper() diff --git a/bot/helper/telegram_helper/filters.py b/bot/helper/telegram_helper/filters.py index 792e841e116..4dabb47c926 100644 --- a/bot/helper/telegram_helper/filters.py +++ b/bot/helper/telegram_helper/filters.py @@ -1,35 +1,35 @@ -from telegram.ext import BaseFilter +from telegram.ext import MessageFilter from telegram import Message from bot import AUTHORIZED_CHATS, SUDO_USERS, OWNER_ID, download_dict, download_dict_lock class CustomFilters: - class _OwnerFilter(BaseFilter): + class _OwnerFilter(MessageFilter): def filter(self, message): return bool(message.from_user.id == OWNER_ID) owner_filter = _OwnerFilter() - class _AuthorizedUserFilter(BaseFilter): + class _AuthorizedUserFilter(MessageFilter): def filter(self, message): id = message.from_user.id return bool(id in AUTHORIZED_CHATS or id == OWNER_ID) authorized_user = _AuthorizedUserFilter() - class _AuthorizedChat(BaseFilter): + class _AuthorizedChat(MessageFilter): def filter(self, message): return bool(message.chat.id in AUTHORIZED_CHATS) authorized_chat = _AuthorizedChat() - class _SudoUser(BaseFilter): + class _SudoUser(MessageFilter): def filter(self,message): return bool(message.from_user.id in SUDO_USERS) sudo_user = _SudoUser() - class _MirrorOwner(BaseFilter): + class _MirrorOwner(MessageFilter): def filter(self, message: Message): user_id = message.from_user.id if user_id == OWNER_ID: diff --git a/bot/modules/anime.py b/bot/modules/anime.py index cae75cd3785..5211acc3a0b 100644 --- a/bot/modules/anime.py +++ b/bot/modules/anime.py @@ -4,9 +4,10 @@ import bs4 import requests -from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, ParseMode -from telegram.ext import run_async, CallbackContext, CommandHandler +from telegram import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode +from telegram.ext import CommandHandler +from bot.helper.telegram_helper.filters import CustomFilters from bot import dispatcher, IMAGE_URL def shorten(description, info = 'anilist.co'): @@ -148,8 +149,7 @@ def t(milliseconds: int) -> str: url = 'https://graphql.anilist.co' -@run_async -def anime(update: Update, context: CallbackContext): +def anime(update, context): message = update.effective_message search = message.text.split(' ', 1) if len(search) == 1: return @@ -190,8 +190,8 @@ def anime(update: Update, context: CallbackContext): else: update.effective_message.reply_text(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(buttons)) -@run_async -def character(update: Update, _): + +def character(update, _): message = update.effective_message search = message.text.split(' ', 1) if len(search) == 1: @@ -211,8 +211,8 @@ def character(update: Update, _): update.effective_message.reply_photo(photo = image, caption = msg, parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text(msg, parse_mode=ParseMode.MARKDOWN) -@run_async -def manga(update: Update, _): + +def manga(update, _): message = update.effective_message search = message.text.split(' ', 1) if len(search) == 1: @@ -249,7 +249,7 @@ def manga(update: Update, _): update.effective_message.reply_text(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(buttons)) else: update.effective_message.reply_text(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(buttons)) -@run_async + def weebhelp(update, context): help_string = ''' • `/anime`*:* Search Anime @@ -259,10 +259,10 @@ def weebhelp(update, context): update.effective_message.reply_photo(IMAGE_URL, help_string, parse_mode=ParseMode.MARKDOWN) -ANIME_HANDLER = CommandHandler("anime", anime) -CHARACTER_HANDLER = CommandHandler("character", character) -MANGA_HANDLER = CommandHandler("manga", manga) -WEEBHELP_HANDLER = CommandHandler("weebhelp", weebhelp) +ANIME_HANDLER = CommandHandler("anime", anime, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +CHARACTER_HANDLER = CommandHandler("character", character, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +MANGA_HANDLER = CommandHandler("manga", manga, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +WEEBHELP_HANDLER = CommandHandler("weebhelp", weebhelp, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(ANIME_HANDLER) dispatcher.add_handler(CHARACTER_HANDLER) diff --git a/bot/modules/authorize.py b/bot/modules/authorize.py index 25221e38d79..be1bd505048 100644 --- a/bot/modules/authorize.py +++ b/bot/modules/authorize.py @@ -1,5 +1,4 @@ from bot.helper.telegram_helper.message_utils import sendMessage -from telegram.ext import run_async from bot import AUTHORIZED_CHATS, SUDO_USERS, dispatcher from telegram.ext import CommandHandler from bot.helper.telegram_helper.filters import CustomFilters @@ -9,7 +8,6 @@ from bot.helper.ext_utils.db_handler import DbManger -@run_async def authorize(update, context): reply_message = None message_ = None @@ -40,7 +38,6 @@ def authorize(update, context): sendMessage(msg, context.bot, update) -@run_async def unauthorize(update, context): reply_message = None message_ = None @@ -70,7 +67,6 @@ def unauthorize(update, context): sendMessage(msg, context.bot, update) -@run_async def addSudo(update, context): reply_message = None message_ = None @@ -95,7 +91,6 @@ def addSudo(update, context): sendMessage(msg, context.bot, update) -@run_async def removeSudo(update, context): reply_message = None message_ = None @@ -119,7 +114,6 @@ def removeSudo(update, context): sendMessage(msg, context.bot, update) -@run_async def sendAuthChats(update, context): user = sudo = '' user += '\n'.join(str(id) for id in AUTHORIZED_CHATS) @@ -128,15 +122,15 @@ def sendAuthChats(update, context): send_auth_handler = CommandHandler(command=BotCommands.AuthorizedUsersCommand, callback=sendAuthChats, - filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize, - filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) unauthorize_handler = CommandHandler(command=BotCommands.UnAuthorizeCommand, callback=unauthorize, - filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) addsudo_handler = CommandHandler(command=BotCommands.AddSudoCommand, callback=addSudo, - filters=CustomFilters.owner_filter) + filters=CustomFilters.owner_filter, run_async=True) removesudo_handler = CommandHandler(command=BotCommands.RmSudoCommand, callback=removeSudo, - filters=CustomFilters.owner_filter) + filters=CustomFilters.owner_filter, run_async=True) dispatcher.add_handler(send_auth_handler) dispatcher.add_handler(authorize_handler) diff --git a/bot/modules/cancel_mirror.py b/bot/modules/cancel_mirror.py index 11729506037..2019415bb63 100644 --- a/bot/modules/cancel_mirror.py +++ b/bot/modules/cancel_mirror.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from bot import download_dict, dispatcher, download_dict_lock, DOWNLOAD_DIR from bot.helper.ext_utils.fs_utils import clean_download @@ -10,7 +10,6 @@ from bot.helper.ext_utils.bot_utils import getDownloadByGid, MirrorStatus -@run_async def cancel_mirror(update, context): args = update.message.text.split(" ", maxsplit=1) mirror_message = None @@ -55,7 +54,6 @@ def cancel_mirror(update, context): clean_download(f'{DOWNLOAD_DIR}{mirror_message.message_id}/') -@run_async def cancel_all(update, context): with download_dict_lock: count = 0 @@ -69,8 +67,8 @@ def cancel_all(update, context): cancel_mirror_handler = CommandHandler(BotCommands.CancelMirror, cancel_mirror, - filters=(CustomFilters.authorized_chat | CustomFilters.authorized_user) & CustomFilters.mirror_owner_filter | CustomFilters.sudo_user) + filters=(CustomFilters.authorized_chat | CustomFilters.authorized_user) & CustomFilters.mirror_owner_filter | CustomFilters.sudo_user, run_async=True) cancel_all_handler = CommandHandler(BotCommands.CancelAllCommand, cancel_all, - filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) dispatcher.add_handler(cancel_all_handler) dispatcher.add_handler(cancel_mirror_handler) diff --git a/bot/modules/clone.py b/bot/modules/clone.py index ed34f7bc0ad..ee568d29fcd 100644 --- a/bot/modules/clone.py +++ b/bot/modules/clone.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot.helper.telegram_helper.message_utils import sendMarkup, deleteMessage, sendMessage from bot.helper.telegram_helper.filters import CustomFilters @@ -7,7 +7,6 @@ from bot.helper.ext_utils.bot_utils import get_readable_file_size -@run_async def cloneNode(update, context): args = update.message.text.split(" ", maxsplit=1) if len(args) > 1: @@ -65,5 +64,5 @@ def cloneNode(update, context): else: sendMessage('Provide G-Drive Shareable Link to Clone.', context.bot, update) -clone_handler = CommandHandler(BotCommands.CloneCommand, cloneNode, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) +clone_handler = CommandHandler(BotCommands.CloneCommand, cloneNode, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(clone_handler) diff --git a/bot/modules/count.py b/bot/modules/count.py index 8fbd06bc77e..829f3fa44c6 100644 --- a/bot/modules/count.py +++ b/bot/modules/count.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot.helper.telegram_helper.message_utils import deleteMessage, sendMessage from bot.helper.telegram_helper.filters import CustomFilters @@ -6,7 +6,6 @@ from bot import dispatcher -@run_async def countNode(update, context): args = update.message.text.split(" ", maxsplit=1) if len(args) > 1: @@ -25,5 +24,5 @@ def countNode(update, context): else: sendMessage("Provide G-Drive Shareable Link to Count.", context.bot, update) -count_handler = CommandHandler(BotCommands.CountCommand, countNode, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) +count_handler = CommandHandler(BotCommands.CountCommand, countNode, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(count_handler) diff --git a/bot/modules/delete.py b/bot/modules/delete.py index 84552009247..32ebdd86966 100644 --- a/bot/modules/delete.py +++ b/bot/modules/delete.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler import threading from telegram import Update from bot import dispatcher, LOGGER @@ -7,7 +7,7 @@ from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.mirror_utils.upload_utils import gdriveTools -@run_async + def deletefile(update, context): msg_args = update.message.text.split(None, 1) msg = '' @@ -25,5 +25,5 @@ def deletefile(update, context): threading.Thread(target=auto_delete_message, args=(context.bot, update.message, reply_message)).start() -delete_handler = CommandHandler(command=BotCommands.DeleteCommand, callback=deletefile, filters=CustomFilters.owner_filter | CustomFilters.sudo_user) +delete_handler = CommandHandler(command=BotCommands.DeleteCommand, callback=deletefile, filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) dispatcher.add_handler(delete_handler) diff --git a/bot/modules/eval.py b/bot/modules/eval.py index a4e04557267..4a5472ddc9c 100644 --- a/bot/modules/eval.py +++ b/bot/modules/eval.py @@ -1,14 +1,13 @@ import io import os -from functools import wraps # Common imports for eval import textwrap import traceback from contextlib import redirect_stdout - -from bot import LOGGER, dispatcher, OWNER_ID -from telegram import ParseMode, Update -from telegram.ext import CallbackContext, CommandHandler, run_async +from bot.helper.telegram_helper.filters import CustomFilters +from bot import LOGGER, dispatcher +from telegram import ParseMode +from telegram.ext import CommandHandler namespaces = {} @@ -47,34 +46,13 @@ def send(msg, bot, update): text=f"`{msg}`", parse_mode=ParseMode.MARKDOWN) -def dev_plus(func): - - @wraps(func) - def is_dev_plus_func(update: Update, context: CallbackContext, *args, - **kwargs): - bot = context.bot - user = update.effective_user - - if user.id == OWNER_ID: - return func(update, context, *args, **kwargs) - elif not user: - pass - else: - update.effective_message.reply_text( - "This is a developer restricted command." - " You do not have permissions to run this.") - - return is_dev_plus_func -@dev_plus -@run_async -def evaluate(update: Update, context: CallbackContext): + +def evaluate(update, context): bot = context.bot send(do(eval, bot, update), bot, update) -@dev_plus -@run_async -def execute(update: Update, context: CallbackContext): +def execute(update, context): bot = context.bot send(do(exec, bot, update), bot, update) @@ -132,9 +110,7 @@ def do(func, bot, update): return result -@dev_plus -@run_async -def clear(update: Update, context: CallbackContext): +def clear(update, context): bot = context.bot log_input(update) global namespaces @@ -143,9 +119,9 @@ def clear(update: Update, context: CallbackContext): send("Cleared locals.", bot, update) -EVAL_HANDLER = CommandHandler(('e', 'ev', 'eva', 'eval'), evaluate) -EXEC_HANDLER = CommandHandler(('x', 'ex', 'exe', 'exec', 'py'), execute) -CLEAR_HANDLER = CommandHandler('clearlocals', clear) +EVAL_HANDLER = CommandHandler(('e', 'ev', 'eva', 'eval'), evaluate, filters=CustomFilters.owner_filter, run_async=True) +EXEC_HANDLER = CommandHandler(('x', 'ex', 'exe', 'exec', 'py'), execute, filters=CustomFilters.owner_filter, run_async=True) +CLEAR_HANDLER = CommandHandler('clearlocals', clear, filters=CustomFilters.owner_filter, run_async=True) dispatcher.add_handler(EVAL_HANDLER) dispatcher.add_handler(EXEC_HANDLER) diff --git a/bot/modules/list.py b/bot/modules/list.py index 1cba9f85d66..0a4893a128c 100644 --- a/bot/modules/list.py +++ b/bot/modules/list.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot import LOGGER, dispatcher from bot.helper.telegram_helper.message_utils import sendMessage, sendMarkup, editMessage @@ -6,7 +6,7 @@ import threading from bot.helper.telegram_helper.bot_commands import BotCommands -@run_async + def list_drive(update, context): try: search = update.message.text.split(' ',maxsplit=1)[1] @@ -24,5 +24,5 @@ def list_drive(update, context): sendMessage('Send a search key along with command', context.bot, update) -list_handler = CommandHandler(BotCommands.ListCommand, list_drive,filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) +list_handler = CommandHandler(BotCommands.ListCommand, list_drive, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(list_handler) diff --git a/bot/modules/mirror.py b/bot/modules/mirror.py index 43aa98bffe4..7e209923ddb 100644 --- a/bot/modules/mirror.py +++ b/bot/modules/mirror.py @@ -1,5 +1,5 @@ import requests -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from telegram import InlineKeyboardMarkup from bot import Interval, INDEX_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BUTTON_SIX_NAME, BUTTON_SIX_URL, BLOCK_MEGA_FOLDER, BLOCK_MEGA_LINKS, VIEW_LINK @@ -305,27 +305,24 @@ def _mirror(bot, update, isTar=False, extract=False): Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) -@run_async def mirror(update, context): _mirror(context.bot, update) -@run_async def tar_mirror(update, context): _mirror(context.bot, update, True) -@run_async def unzip_mirror(update, context): _mirror(context.bot, update, extract=True) mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) tar_mirror_handler = CommandHandler(BotCommands.TarMirrorCommand, tar_mirror, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) unzip_mirror_handler = CommandHandler(BotCommands.UnzipMirrorCommand, unzip_mirror, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_handler) dispatcher.add_handler(tar_mirror_handler) dispatcher.add_handler(unzip_mirror_handler) diff --git a/bot/modules/mirror_status.py b/bot/modules/mirror_status.py index d18c19dd864..5507df76c95 100644 --- a/bot/modules/mirror_status.py +++ b/bot/modules/mirror_status.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from bot import dispatcher, status_reply_dict, DOWNLOAD_STATUS_UPDATE_INTERVAL, status_reply_dict_lock from bot.helper.telegram_helper.message_utils import * from time import sleep @@ -9,7 +9,6 @@ import threading -@run_async def mirror_status(update, context): message = get_readable_message() if len(message) == 0: @@ -27,5 +26,5 @@ def mirror_status(update, context): mirror_status_handler = CommandHandler(BotCommands.StatusCommand, mirror_status, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_status_handler) diff --git a/bot/modules/search.py b/bot/modules/search.py index 09417d26c4c..d0082a191a7 100644 --- a/bot/modules/search.py +++ b/bot/modules/search.py @@ -3,7 +3,7 @@ import asyncio import aiohttp import feedparser -from telegram.ext import run_async, CommandHandler +from telegram.ext import CommandHandler from telegram import ParseMode from bot import dispatcher, IMAGE_URL from urllib.parse import quote as urlencode, urlsplit @@ -12,9 +12,13 @@ from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton from bot.helper import custom_filters from bot import app +from bot.helper.telegram_helper.filters import CustomFilters + session = aiohttp.ClientSession() search_lock = asyncio.Lock() search_info = {False: dict(), True: dict()} + + async def return_search(query, page=1, sukebei=False): page -= 1 query = query.lower().strip() @@ -59,6 +63,8 @@ async def return_search(query, page=1, sukebei=False): message_info = dict() ignore = set() + + @app.on_message(filters.command(['ts', 'nyaa', 'nyaasi'])) async def nyaa_search(client, message): text = message.text.split(' ') @@ -66,6 +72,7 @@ async def nyaa_search(client, message): query = ' '.join(text) await init_search(client, message, query, False) + @app.on_message(filters.command(['sts', 'sukebei'])) async def nyaa_search_sukebei(client, message): text = message.text.split(' ') @@ -73,6 +80,7 @@ async def nyaa_search_sukebei(client, message): query = ' '.join(text) await init_search(client, message, query, True) + async def init_search(client, message, query, sukebei): result, pages, ttl = await return_search(query, sukebei=sukebei) if not result: @@ -86,6 +94,7 @@ async def init_search(client, message, query, sukebei): ])) message_info[(reply.chat.id, reply.message_id)] = message.from_user.id, ttl, query, 1, pages, sukebei + @app.on_callback_query(custom_filters.callback_data('nyaa_nop')) async def nyaa_nop(client, callback_query): await callback_query.answer(cache_time=3600) @@ -135,7 +144,7 @@ async def nyaa_callback(client, callback_query): ignore.add(message_identifier) await callback_query.answer() -@run_async + def searchhelp(update, context): help_string = ''' • /ts [search query] @@ -148,5 +157,5 @@ def searchhelp(update, context): update.effective_message.reply_photo(IMAGE_URL, help_string, parse_mode=ParseMode.HTML) -SEARCHHELP_HANDLER = CommandHandler("tshelp", searchhelp) +SEARCHHELP_HANDLER = CommandHandler("tshelp", searchhelp, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(SEARCHHELP_HANDLER) diff --git a/bot/modules/shell.py b/bot/modules/shell.py index 7cd2524dbb4..2d8548bd395 100644 --- a/bot/modules/shell.py +++ b/bot/modules/shell.py @@ -1,33 +1,11 @@ import subprocess -from functools import wraps from bot import LOGGER, dispatcher -from bot import OWNER_ID -from telegram import ParseMode, Update -from telegram.ext import CallbackContext, CommandHandler -from telegram.ext.dispatcher import run_async +from telegram import ParseMode +from telegram.ext import CommandHandler +from bot.helper.telegram_helper.filters import CustomFilters -def dev_plus(func): - - @wraps(func) - def is_dev_plus_func(update: Update, context: CallbackContext, *args, - **kwargs): - bot = context.bot - user = update.effective_user - if user.id == OWNER_ID: - return func(update, context, *args, **kwargs) - elif not user: - pass - else: - update.effective_message.reply_text( - "This is a developer restricted command." - " You do not have permissions to run this.") - - return is_dev_plus_func - -@dev_plus -@run_async -def shell(update: Update, context: CallbackContext): +def shell(update, context): message = update.effective_message cmd = message.text.split(' ', 1) if len(cmd) == 1: @@ -59,5 +37,5 @@ def shell(update: Update, context: CallbackContext): message.reply_text(reply, parse_mode=ParseMode.MARKDOWN) -SHELL_HANDLER = CommandHandler(['sh', 'shell', 'term', 'terminal'], shell) +SHELL_HANDLER = CommandHandler(('sh', 'shell', 'term', 'terminal'), shell, filters=CustomFilters.owner_filter, run_async=True) dispatcher.add_handler(SHELL_HANDLER) diff --git a/bot/modules/speedtest.py b/bot/modules/speedtest.py index dc6d274ab10..fd3c96d7e2c 100644 --- a/bot/modules/speedtest.py +++ b/bot/modules/speedtest.py @@ -3,10 +3,9 @@ from bot import dispatcher, AUTHORIZED_CHATS from bot.helper.telegram_helper.bot_commands import BotCommands from telegram import Update, ParseMode -from telegram.ext import Filters, run_async, CommandHandler +from telegram.ext import Filters, CommandHandler -@run_async def speedtest(update, context): message = update.effective_message ed_msg = message.reply_text("Running Speed Test . . . ") @@ -47,6 +46,6 @@ def speed_convert(size): SPEED_HANDLER = CommandHandler(BotCommands.SpeedCommand, speedtest, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(SPEED_HANDLER) diff --git a/bot/modules/stickers.py b/bot/modules/stickers.py index 76cf9304829..fb71c1bb5be 100644 --- a/bot/modules/stickers.py +++ b/bot/modules/stickers.py @@ -4,16 +4,16 @@ from PIL import Image from html import escape +from bot.helper.telegram_helper.filters import CustomFilters from telegram import ParseMode, InlineKeyboardMarkup, InlineKeyboardButton -from telegram import TelegramError, Update -from telegram.ext import run_async, CallbackContext, CommandHandler +from telegram import TelegramError +from telegram.ext import CommandHandler from telegram.utils.helpers import mention_html from bot import dispatcher, IMAGE_URL -@run_async -def stickerid(update: Update, context: CallbackContext): +def stickerid(update, context): msg = update.effective_message if msg.reply_to_message and msg.reply_to_message.sticker: update.effective_message.reply_text( @@ -32,7 +32,7 @@ def stickerid(update: Update, context: CallbackContext): ) -def getsticker(update: Update, context: CallbackContext): +def getsticker(update, context): bot = context.bot msg = update.effective_message chat_id = update.effective_chat.id @@ -47,8 +47,7 @@ def getsticker(update: Update, context: CallbackContext): "Please reply to a sticker for me to upload its PNG.") -@run_async -def kang(update: Update, context: CallbackContext): +def kang(update, context): msg = update.effective_message user = update.effective_user args = context.args @@ -397,7 +396,6 @@ def makepack_internal( "Failed to create sticker pack. Possibly due to blek mejik.") -@run_async def delsticker(update, context): msg = update.effective_message if msg.reply_to_message and msg.reply_to_message.sticker: @@ -412,7 +410,6 @@ def delsticker(update, context): ) -@run_async def stickhelp(update, context): help_string = ''' • `/stickerid`*:* Reply to a Sticker to me to tell you its file ID. @@ -422,11 +419,11 @@ def stickhelp(update, context): ''' update.effective_message.reply_photo(IMAGE_URL, help_string, parse_mode=ParseMode.MARKDOWN) -STICKERID_HANDLER = CommandHandler("stickerid", stickerid) -GETSTICKER_HANDLER = CommandHandler("getsticker", getsticker) -KANG_HANDLER = CommandHandler("kang", kang) -DEL_HANDLER = CommandHandler("remove", delsticker) -STICKHELP_HANDLER = CommandHandler("stickerhelp", stickhelp) +STICKERID_HANDLER = CommandHandler("stickerid", stickerid, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +GETSTICKER_HANDLER = CommandHandler("getsticker", getsticker, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +KANG_HANDLER = CommandHandler("kang", kang, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +DEL_HANDLER = CommandHandler("remove", delsticker, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +STICKHELP_HANDLER = CommandHandler("stickerhelp", stickhelp, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(STICKERID_HANDLER) diff --git a/bot/modules/usage.py b/bot/modules/usage.py index cc7247df76a..e2c488097f3 100644 --- a/bot/modules/usage.py +++ b/bot/modules/usage.py @@ -8,10 +8,9 @@ from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.message_utils import sendMessage from telegram import update -from telegram.ext import run_async, CommandHandler +from telegram.ext import CommandHandler -@run_async def dyno_usage(update, context): heroku_api = "https://api.heroku.com" if HEROKU_API_KEY is not None and HEROKU_APP_NAME is not None: @@ -77,6 +76,6 @@ def dyno_usage(update, context): dyno_usage_handler = CommandHandler(command=BotCommands.UsageCommand, callback=dyno_usage, - filters=CustomFilters.owner_filter | CustomFilters.sudo_user) + filters=CustomFilters.owner_filter | CustomFilters.sudo_user, run_async=True) dispatcher.add_handler(dyno_usage_handler) diff --git a/bot/modules/watch.py b/bot/modules/watch.py index e267fa8e1f6..97c469e33b3 100644 --- a/bot/modules/watch.py +++ b/bot/modules/watch.py @@ -1,4 +1,4 @@ -from telegram.ext import CommandHandler, run_async +from telegram.ext import CommandHandler from telegram import Bot, Update from bot import Interval, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, dispatcher, LOGGER from bot.helper.ext_utils.bot_utils import setInterval @@ -54,7 +54,6 @@ def _watch(bot: Bot, update, isTar=False): Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) -@run_async def watchTar(update, context): _watch(context.bot, update, True) @@ -64,8 +63,8 @@ def watch(update, context): mirror_handler = CommandHandler(BotCommands.WatchCommand, watch, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) tar_mirror_handler = CommandHandler(BotCommands.TarWatchCommand, watchTar, - filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_handler) dispatcher.add_handler(tar_mirror_handler) diff --git a/requirements.txt b/requirements.txt index a23616064b4..93a339343d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,7 +21,7 @@ pybase64 pyrogram python-dotenv python-magic -python-telegram-bot==12.6.1 +python-telegram-bot pytz requests speedtest-cli