Skip to content

Commit

Permalink
Update ptb and small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anasty17 authored Jun 12, 2021
1 parent d3400ce commit 5807abf
Show file tree
Hide file tree
Showing 22 changed files with 103 additions and 165 deletions.
2 changes: 1 addition & 1 deletion bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 7 additions & 13 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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!
Expand All @@ -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...')
Expand All @@ -81,20 +78,17 @@ 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)
end_time = int(round(time.time() * 1000))
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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions bot/helper/mirror_utils/download_utils/aria2_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions bot/helper/mirror_utils/download_utils/mega_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions bot/helper/telegram_helper/filters.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
26 changes: 13 additions & 13 deletions bot/modules/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions bot/modules/authorize.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +8,6 @@
from bot.helper.ext_utils.db_handler import DbManger


@run_async
def authorize(update, context):
reply_message = None
message_ = None
Expand Down Expand Up @@ -40,7 +38,6 @@ def authorize(update, context):
sendMessage(msg, context.bot, update)


@run_async
def unauthorize(update, context):
reply_message = None
message_ = None
Expand Down Expand Up @@ -70,7 +67,6 @@ def unauthorize(update, context):
sendMessage(msg, context.bot, update)


@run_async
def addSudo(update, context):
reply_message = None
message_ = None
Expand All @@ -95,7 +91,6 @@ def addSudo(update, context):
sendMessage(msg, context.bot, update)


@run_async
def removeSudo(update, context):
reply_message = None
message_ = None
Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions bot/modules/cancel_mirror.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
5 changes: 2 additions & 3 deletions bot/modules/clone.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions bot/modules/count.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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
from bot.helper.telegram_helper.bot_commands import BotCommands
from bot import dispatcher


@run_async
def countNode(update, context):
args = update.message.text.split(" ", maxsplit=1)
if len(args) > 1:
Expand All @@ -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)
6 changes: 3 additions & 3 deletions bot/modules/delete.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = ''
Expand All @@ -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)
Loading

0 comments on commit 5807abf

Please sign in to comment.