Moved to async

This commit is contained in:
Profitroll 2023-01-10 13:06:24 +01:00
parent 6facf428c5
commit c4323f0b00
8 changed files with 66 additions and 51 deletions

View File

@ -7,51 +7,55 @@ from modules.utils import jsonLoad, jsonSave, configGet, locale
from modules.submissions import subBlock, subUnblock from modules.submissions import subBlock, subUnblock
from modules.app import app from modules.app import app
@app.on_callback_query(filters.regex("sub_yes_[\s\S]*_[\s\S]*")) @app.on_callback_query(filters.regex("sub_yes_[\s\S]*_[\s\S]*"))
def callback_query_yes(app: Client, clb: CallbackQuery): async def callback_query_yes(app: Client, clb: CallbackQuery):
fullclb = clb.data.split("_") fullclb = clb.data.split("_")
user_locale = clb.from_user.language_code user_locale = clb.from_user.language_code
try: try:
submission = app.get_messages(int(fullclb[2]), int(fullclb[3])) submission = await app.get_messages(int(fullclb[2]), int(fullclb[3]))
except: except:
clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True)
return return
try: try:
media = app.download_media(submission, file_name=configGet("queue", "locations")+sep) media = await app.download_media(submission, file_name=configGet("queue", "locations")+sep)
if clb.data.endswith("_caption"): if clb.data.endswith("_caption"):
index = jsonLoad(configGet("index", "locations")) index = jsonLoad(configGet("index", "locations"))
index["captions"][Path(media).name] = submission.caption index["captions"][Path(media).name] = submission.caption
jsonSave(index, configGet("index", "locations")) jsonSave(index, configGet("index", "locations"))
except: except:
clb.answer(text=locale("sub_media_unavail", "message", locale=user_locale), show_alert=True) await clb.answer(text=locale("sub_media_unavail", "message", locale=user_locale), show_alert=True)
return return
submission.reply_text(locale("sub_yes", "message", locale=submission.from_user.language_code), quote=True) await submission.reply_text(locale("sub_yes", "message", locale=submission.from_user.language_code), quote=True)
clb.answer(text=locale("sub_yes", "callback", locale=user_locale).format(fullclb[2]), show_alert=True) await clb.answer(text=locale("sub_yes", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)
@app.on_callback_query(filters.regex("sub_no_[\s\S]*_[\s\S]*")) @app.on_callback_query(filters.regex("sub_no_[\s\S]*_[\s\S]*"))
def callback_query_no(app: Client, clb: CallbackQuery): async def callback_query_no(app: Client, clb: CallbackQuery):
fullclb = clb.data.split("_") fullclb = clb.data.split("_")
user_locale = clb.from_user.language_code user_locale = clb.from_user.language_code
try: try:
submission = app.get_messages(int(fullclb[2]), int(fullclb[3])) submission = await app.get_messages(int(fullclb[2]), int(fullclb[3]))
except: except:
clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True)
return return
submission.reply_text(locale("sub_no", "message", locale=submission.from_user.language_code), quote=True) await submission.reply_text(locale("sub_no", "message", locale=submission.from_user.language_code), quote=True)
clb.answer(text=locale("sub_no", "callback", locale=user_locale).format(fullclb[2]), show_alert=True) await clb.answer(text=locale("sub_no", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)
@app.on_callback_query(filters.regex("sub_block_[\s\S]*")) @app.on_callback_query(filters.regex("sub_block_[\s\S]*"))
def callback_query_block(app: Client, clb: CallbackQuery): async def callback_query_block(app: Client, clb: CallbackQuery):
fullclb = clb.data.split("_") fullclb = clb.data.split("_")
user_locale = clb.from_user.language_code user_locale = clb.from_user.language_code
app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale"))) await app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale")))
subBlock(int(fullclb[2])) subBlock(int(fullclb[2]))
clb.answer(text=locale("sub_block", "callback", locale=user_locale).format(fullclb[2]), show_alert=True) await clb.answer(text=locale("sub_block", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)
@app.on_callback_query(filters.regex("sub_unblock_[\s\S]*")) @app.on_callback_query(filters.regex("sub_unblock_[\s\S]*"))
def callback_query_unblock(app: Client, clb: CallbackQuery): async def callback_query_unblock(app: Client, clb: CallbackQuery):
fullclb = clb.data.split("_") fullclb = clb.data.split("_")
user_locale = clb.from_user.language_code user_locale = clb.from_user.language_code
app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale"))) await app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale")))
subUnblock(int(fullclb[2])) subUnblock(int(fullclb[2]))
clb.answer(text=locale("sub_unblock", "callback", locale=user_locale).format(fullclb[2]), show_alert=True) await clb.answer(text=locale("sub_unblock", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)

View File

@ -7,10 +7,10 @@ from modules.logging import logWrite
from modules.utils import configGet, killProc, locale from modules.utils import configGet, killProc, locale
@app.on_message(~ filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"])) @app.on_message(~ filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"]))
def cmd_kill(app: Client, msg: Message): async def cmd_kill(app: Client, msg: Message):
if msg.from_user.id == configGet("admin"): if msg.from_user.id == configGet("admin"):
pid = getpid() pid = getpid()
logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid))) logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid)))
msg.reply_text(locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) await msg.reply_text(locale("shutdown", "message", locale=configGet("locale")).format(str(pid)))
killProc(pid) killProc(pid)

View File

@ -5,11 +5,11 @@ from modules.app import app
from modules.utils import jsonLoad, configGet, locale from modules.utils import jsonLoad, configGet, locale
@app.on_message(~ filters.scheduled & filters.command(["start"], prefixes="/")) @app.on_message(~ filters.scheduled & filters.command(["start"], prefixes="/"))
def cmd_start(app: Client, msg: Message): async def cmd_start(app: Client, msg: Message):
if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")):
msg.reply_text(locale("start", "message", locale=msg.from_user.language_code)) await msg.reply_text(locale("start", "message", locale=msg.from_user.language_code))
@app.on_message(~ filters.scheduled & filters.command(["rules", "help"], prefixes="/")) @app.on_message(~ filters.scheduled & filters.command(["rules", "help"], prefixes="/"))
def cmd_rules(app: Client, msg: Message): async def cmd_rules(app: Client, msg: Message):
if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")):
msg.reply_text(locale("rules", "message", locale=msg.from_user.language_code)) await msg.reply_text(locale("rules", "message", locale=msg.from_user.language_code))

View File

@ -3,7 +3,8 @@ from pyrogram.client import Client
from pyrogram.types import BotCommand, BotCommandScopeChat from pyrogram.types import BotCommand, BotCommandScopeChat
from modules.utils import configGet, locale from modules.utils import configGet, locale
def register_commands(app: Client): async def register_commands(app: Client):
if configGet("submit", "mode"): if configGet("submit", "mode"):
# Registering user commands # Registering user commands
for entry in listdir(configGet("locale", "locations")): for entry in listdir(configGet("locale", "locations")):
@ -11,19 +12,22 @@ def register_commands(app: Client):
commands_list = [] commands_list = []
for command in configGet("commands"): for command in configGet("commands"):
commands_list.append(BotCommand(command, locale(command, "commands", locale=entry.replace(".json", "")))) commands_list.append(BotCommand(command, locale(command, "commands", locale=entry.replace(".json", ""))))
app.set_bot_commands(commands_list, language_code=entry.replace(".json", "")) await app.set_bot_commands(commands_list, language_code=entry.replace(".json", ""))
# Registering user commands for fallback locale # Registering user commands for fallback locale
commands_list = [] commands_list = []
for command in configGet("commands"): for command in configGet("commands"):
commands_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale_fallback")))) commands_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale_fallback"))))
app.set_bot_commands(commands_list) await app.set_bot_commands(commands_list)
# Registering admin commands # Registering admin commands
commands_admin_list = [] commands_admin_list = []
if configGet("submit", "mode"): if configGet("submit", "mode"):
for command in configGet("commands"): for command in configGet("commands"):
commands_admin_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale")))) commands_admin_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale"))))
for command in configGet("commands_admin"): for command in configGet("commands_admin"):
commands_admin_list.append(BotCommand(command, locale(command, "commands_admin", locale=configGet("locale")))) commands_admin_list.append(BotCommand(command, locale(command, "commands_admin", locale=configGet("locale"))))
app.set_bot_commands(commands_admin_list, scope=BotCommandScopeChat(chat_id=configGet("admin")))
await app.set_bot_commands(commands_admin_list, scope=BotCommandScopeChat(chat_id=configGet("admin")))

View File

@ -7,23 +7,27 @@ from modules.app import app
from modules.submissions import subLimited, subLimit from modules.submissions import subLimited, subLimit
@app.on_message(~ filters.scheduled & filters.photo | filters.video | filters.animation | filters.document) @app.on_message(~ filters.scheduled & filters.photo | filters.video | filters.animation | filters.document)
def get_submission(_: Client, msg: Message): async def get_submission(_: Client, msg: Message):
try: try:
if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")):
user_locale = msg.from_user.language_code user_locale = msg.from_user.language_code
if not subLimited(msg.from_user): if not subLimited(msg.from_user):
if msg.document != None: if msg.document != None:
if msg.document.mime_type not in configGet("mime_types", "submission"): if msg.document.mime_type not in configGet("mime_types", "submission"):
msg.reply_text(locale("mime_not_allowed", "message", locale=user_locale), quote=True) await msg.reply_text(locale("mime_not_allowed", "message", locale=user_locale), quote=True)
return return
if msg.document.file_size > configGet("file_size", "submission"): if msg.document.file_size > configGet("file_size", "submission"):
msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True)
return return
if msg.video != None: if msg.video != None:
if msg.video.file_size > configGet("file_size", "submission"): if msg.video.file_size > configGet("file_size", "submission"):
msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True)
return return
buttons = [ buttons = [
@ -57,7 +61,7 @@ def get_submission(_: Client, msg: Message):
if msg.from_user.phone_number != None: if msg.from_user.phone_number != None:
caption += f" ({msg.from_user.phone_number})" caption += f" ({msg.from_user.phone_number})"
msg.copy(configGet("admin"), caption=caption, reply_markup=InlineKeyboardMarkup(buttons)) await msg.copy(configGet("admin"), caption=caption, reply_markup=InlineKeyboardMarkup(buttons))
if msg.from_user.id != configGet("admin"): if msg.from_user.id != configGet("admin"):
buttons += [ buttons += [
@ -69,10 +73,11 @@ def get_submission(_: Client, msg: Message):
] ]
] ]
msg.reply_text(locale("sub_sent", "message", locale=user_locale), quote=True) await msg.reply_text(locale("sub_sent", "message", locale=user_locale), quote=True)
subLimit(msg.from_user) subLimit(msg.from_user)
else: else:
msg.reply_text(locale("sub_cooldown", "message", locale=user_locale).format(str(configGet("timeout", "submission")))) await msg.reply_text(locale("sub_cooldown", "message", locale=user_locale).format(str(configGet("timeout", "submission"))))
except AttributeError: except AttributeError:
logWrite(f"from_user in function get_submission does not seem to contain id") logWrite(f"from_user in function get_submission does not seem to contain id")

View File

@ -1,11 +1,15 @@
from datetime import datetime from datetime import datetime, timedelta
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
from modules.utils import configGet from modules.utils import configGet
from modules.sender import send_content from modules.sender import send_content
from modules.commands_register import register_commands
from modules.app import app
scheduler = AsyncIOScheduler() scheduler = AsyncIOScheduler()
if configGet("post", "mode"): if configGet("post", "mode"):
for entry in configGet("time", "posting"): for entry in configGet("time", "posting"):
dt_obj = datetime.strptime(entry, "%H:%M") dt_obj = datetime.strptime(entry, "%H:%M")
scheduler.add_job(send_content, "cron", hour=dt_obj.hour, minute=dt_obj.minute) scheduler.add_job(send_content, "cron", hour=dt_obj.hour, minute=dt_obj.minute, args=[app])
scheduler.add_job(register_commands, "date", run_date=datetime.now()+timedelta(seconds=10), args=[app])

View File

@ -7,7 +7,7 @@ from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from modules.logging import logWrite from modules.logging import logWrite
from modules.utils import jsonLoad, jsonSave, configGet, locale from modules.utils import jsonLoad, jsonSave, configGet, locale
def send_content(app: Client): async def send_content(app: Client):
# Send post to channel # Send post to channel
try: try:
@ -45,7 +45,7 @@ def send_content(app: Client):
else: else:
logWrite(locale("post_empty", "console", locale=configGet("locale"))) logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"): if configGet("error", "reports"):
app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale"))) await app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale")))
return return
if candidate_file in index["captions"]: if candidate_file in index["captions"]:
@ -65,21 +65,21 @@ def send_content(app: Client):
if configGet("enabled", "caption"): if configGet("enabled", "caption"):
if configGet("link", "caption") != None: if configGet("link", "caption") != None:
sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) sent = await app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting"))
else: else:
sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) sent = await app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting"))
else: else:
sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) sent = await app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting"))
elif ext_type == "video": elif ext_type == "video":
if configGet("enabled", "caption"): if configGet("enabled", "caption"):
if configGet("link", "caption") != None: if configGet("link", "caption") != None:
sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) sent = await app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting"))
else: else:
sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) sent = await app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting"))
else: else:
sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) sent = await app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting"))
else: else:
return return
@ -95,14 +95,14 @@ def send_content(app: Client):
logWrite(locale("post_sent", "console", locale=configGet("locale")).format(candidate, ext_type, str(configGet("channel", "posting")), caption.replace("\n", "%n"), str(configGet("silent", "posting")))) logWrite(locale("post_sent", "console", locale=configGet("locale")).format(candidate, ext_type, str(configGet("channel", "posting")), caption.replace("\n", "%n"), str(configGet("silent", "posting"))))
if configGet("sent", "reports"): if configGet("sent", "reports"):
app.send_message(configGet("admin"), f"Posted `{candidate_file}`", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup([ await app.send_message(configGet("admin"), f"Posted `{candidate_file}`", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton(locale("post_view", "button", locale=configGet("locale")), url=sent.link)] [InlineKeyboardButton(locale("post_view", "button", locale=configGet("locale")), url=sent.link)]
])) ]))
except Exception as exp: except Exception as exp:
logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), format_exc())) logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), format_exc()))
if configGet("error", "reports"): if configGet("error", "reports"):
app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, format_exc())) await app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, format_exc()))
pass pass

View File

@ -114,8 +114,7 @@ pid = getpid()
# check_forwards(app) # check_forwards(app)
# Imports ===================================================================================================================================== # Imports ==================================================================================================================================
from modules.commands.general import * from modules.commands.general import *
from modules.commands_register import register_commands from modules.commands_register import register_commands
@ -141,14 +140,13 @@ if __name__ == "__main__":
logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid))) logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid)))
app.start() app.start()
if configGet("startup", "reports"): if configGet("startup", "reports"):
app.send_message(configGet("admin"), locale("startup", "message", locale=configGet("locale")).format(str(pid))) app.send_message(configGet("admin"), locale("startup", "message", locale=configGet("locale")).format(str(pid)))
if configGet("post", "mode"): if configGet("post", "mode"):
scheduler.start() scheduler.start()
register_commands(app)
idle() idle()
app.send_message(configGet("admin"), locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) app.send_message(configGet("admin"), locale("shutdown", "message", locale=configGet("locale")).format(str(pid)))