From 6facf428c5e7bcf473c8ccde6e37a330cba3fde4 Mon Sep 17 00:00:00 2001 From: profitroll Date: Tue, 10 Jan 2023 12:52:44 +0100 Subject: [PATCH] Complete refactor --- modules/app.py | 4 + modules/callbacks/submission.py | 57 +++++ modules/colors.py | 22 -- modules/commands/general.py | 16 ++ modules/commands/mode_submit.py | 15 ++ modules/commands_register.py | 29 +++ modules/handlers/submission.py | 78 +++++++ modules/scheduler.py | 11 + modules/sender.py | 111 ++++++++++ modules/submissions.py | 32 +++ modules/utils.py | 2 +- poster.py | 354 ++------------------------------ 12 files changed, 369 insertions(+), 362 deletions(-) create mode 100644 modules/app.py create mode 100644 modules/callbacks/submission.py delete mode 100644 modules/colors.py create mode 100644 modules/commands/general.py create mode 100644 modules/commands/mode_submit.py create mode 100644 modules/commands_register.py create mode 100644 modules/handlers/submission.py create mode 100644 modules/scheduler.py create mode 100644 modules/sender.py create mode 100644 modules/submissions.py diff --git a/modules/app.py b/modules/app.py new file mode 100644 index 0000000..84ff5a9 --- /dev/null +++ b/modules/app.py @@ -0,0 +1,4 @@ +from pyrogram.client import Client +from modules.utils import configGet + +app = Client("duptsiaposter", bot_token=configGet("bot_token", "bot"), api_id=configGet("api_id", "bot"), api_hash=configGet("api_hash", "bot")) \ No newline at end of file diff --git a/modules/callbacks/submission.py b/modules/callbacks/submission.py new file mode 100644 index 0000000..8a8cffb --- /dev/null +++ b/modules/callbacks/submission.py @@ -0,0 +1,57 @@ +from os import sep +from pathlib import Path +from pyrogram import filters +from pyrogram.client import Client +from pyrogram.types import CallbackQuery +from modules.utils import jsonLoad, jsonSave, configGet, locale +from modules.submissions import subBlock, subUnblock +from modules.app import app + +@app.on_callback_query(filters.regex("sub_yes_[\s\S]*_[\s\S]*")) +def callback_query_yes(app: Client, clb: CallbackQuery): + fullclb = clb.data.split("_") + user_locale = clb.from_user.language_code + try: + submission = app.get_messages(int(fullclb[2]), int(fullclb[3])) + except: + clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) + return + try: + media = app.download_media(submission, file_name=configGet("queue", "locations")+sep) + if clb.data.endswith("_caption"): + index = jsonLoad(configGet("index", "locations")) + index["captions"][Path(media).name] = submission.caption + jsonSave(index, configGet("index", "locations")) + except: + clb.answer(text=locale("sub_media_unavail", "message", locale=user_locale), show_alert=True) + return + 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) + +@app.on_callback_query(filters.regex("sub_no_[\s\S]*_[\s\S]*")) +def callback_query_no(app: Client, clb: CallbackQuery): + fullclb = clb.data.split("_") + user_locale = clb.from_user.language_code + try: + submission = app.get_messages(int(fullclb[2]), int(fullclb[3])) + except: + clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) + return + 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) + +@app.on_callback_query(filters.regex("sub_block_[\s\S]*")) +def callback_query_block(app: Client, clb: CallbackQuery): + fullclb = clb.data.split("_") + user_locale = clb.from_user.language_code + app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale"))) + subBlock(int(fullclb[2])) + 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]*")) +def callback_query_unblock(app: Client, clb: CallbackQuery): + fullclb = clb.data.split("_") + user_locale = clb.from_user.language_code + app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale"))) + subUnblock(int(fullclb[2])) + clb.answer(text=locale("sub_unblock", "callback", locale=user_locale).format(fullclb[2]), show_alert=True) \ No newline at end of file diff --git a/modules/colors.py b/modules/colors.py deleted file mode 100644 index 3a623b1..0000000 --- a/modules/colors.py +++ /dev/null @@ -1,22 +0,0 @@ -RESET = '\u001b[0m' - -BLACK = '\u001b[30m' -RED = '\u001b[31m' -GREEN = '\u001b[32m' -YELLOW = '\u001b[33m' -BLUE = '\u001b[34m' -MAGENTA = '\u001b[35m' -CYAN = '\u001b[36m' -WHITE = '\u001b[37m' - -BBLACK = '\u001b[30;1m' -BRED = '\u001b[31;1m' -BGREEN = '\u001b[32;1m' -BYELLOW = '\u001b[33;1m' -BBLUE = '\u001b[34;1m' -BMAGENTA = '\u001b[35;1m' -BCYAN = '\u001b[36;1m' -BWHITE = '\u001b[37;1m' - -ULINE = '\u001b[4m' -REVERSE = '\u001b[7m' \ No newline at end of file diff --git a/modules/commands/general.py b/modules/commands/general.py new file mode 100644 index 0000000..9b58380 --- /dev/null +++ b/modules/commands/general.py @@ -0,0 +1,16 @@ +from os import getpid +from pyrogram import filters +from pyrogram.client import Client +from pyrogram.types import Message +from modules.app import app +from modules.logging import logWrite +from modules.utils import configGet, killProc, locale + +@app.on_message(~ filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"])) +def cmd_kill(app: Client, msg: Message): + + if msg.from_user.id == configGet("admin"): + pid = getpid() + logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid))) + msg.reply_text(locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) + killProc(pid) \ No newline at end of file diff --git a/modules/commands/mode_submit.py b/modules/commands/mode_submit.py new file mode 100644 index 0000000..a2796ed --- /dev/null +++ b/modules/commands/mode_submit.py @@ -0,0 +1,15 @@ +from pyrogram import filters +from pyrogram.client import Client +from pyrogram.types import Message +from modules.app import app +from modules.utils import jsonLoad, configGet, locale + +@app.on_message(~ filters.scheduled & filters.command(["start"], prefixes="/")) +def cmd_start(app: Client, msg: Message): + if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): + msg.reply_text(locale("start", "message", locale=msg.from_user.language_code)) + +@app.on_message(~ filters.scheduled & filters.command(["rules", "help"], prefixes="/")) +def cmd_rules(app: Client, msg: Message): + if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): + msg.reply_text(locale("rules", "message", locale=msg.from_user.language_code)) \ No newline at end of file diff --git a/modules/commands_register.py b/modules/commands_register.py new file mode 100644 index 0000000..a704572 --- /dev/null +++ b/modules/commands_register.py @@ -0,0 +1,29 @@ +from os import listdir +from pyrogram.client import Client +from pyrogram.types import BotCommand, BotCommandScopeChat +from modules.utils import configGet, locale + +def register_commands(app: Client): + if configGet("submit", "mode"): + # Registering user commands + for entry in listdir(configGet("locale", "locations")): + if entry.endswith(".json"): + commands_list = [] + for command in configGet("commands"): + commands_list.append(BotCommand(command, locale(command, "commands", locale=entry.replace(".json", "")))) + app.set_bot_commands(commands_list, language_code=entry.replace(".json", "")) + + # Registering user commands for fallback locale + commands_list = [] + for command in configGet("commands"): + commands_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale_fallback")))) + app.set_bot_commands(commands_list) + + # Registering admin commands + commands_admin_list = [] + if configGet("submit", "mode"): + for command in configGet("commands"): + commands_admin_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale")))) + for command in configGet("commands_admin"): + 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"))) \ No newline at end of file diff --git a/modules/handlers/submission.py b/modules/handlers/submission.py new file mode 100644 index 0000000..18b3f2f --- /dev/null +++ b/modules/handlers/submission.py @@ -0,0 +1,78 @@ +from pyrogram import filters +from pyrogram.client import Client +from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message +from modules.utils import jsonLoad, configGet, locale +from modules.logging import logWrite +from modules.app import app +from modules.submissions import subLimited, subLimit + +@app.on_message(~ filters.scheduled & filters.photo | filters.video | filters.animation | filters.document) +def get_submission(_: Client, msg: Message): + try: + if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): + user_locale = msg.from_user.language_code + if not subLimited(msg.from_user): + + if msg.document != None: + if msg.document.mime_type not in configGet("mime_types", "submission"): + msg.reply_text(locale("mime_not_allowed", "message", locale=user_locale), quote=True) + return + 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) + return + + if msg.video != None: + 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) + return + + buttons = [ + [ + InlineKeyboardButton(text=locale("sub_yes", "button", locale=configGet("locale")), callback_data=f"sub_yes_{msg.from_user.id}_{msg.id}") + ] + ] + + if msg.caption != None: + caption = str(msg.caption) + buttons[0].append( + InlineKeyboardButton(text=locale("sub_yes_caption", "button", locale=configGet("locale")), callback_data=f"sub_yes_{msg.from_user.id}_{msg.id}_caption") + ) + buttons[0].append( + InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{msg.from_user.id}_{msg.id}") + ) + else: + caption = "" + buttons[0].append( + InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{msg.from_user.id}_{msg.id}") + ) + + caption += locale("sub_by", "message", locale=locale(configGet("locale"))) + + if msg.from_user.first_name != None: + caption += f" {msg.from_user.first_name}" + if msg.from_user.last_name != None: + caption += f" {msg.from_user.last_name}" + if msg.from_user.username != None: + caption += f" (@{msg.from_user.username})" + if msg.from_user.phone_number != None: + caption += f" ({msg.from_user.phone_number})" + + msg.copy(configGet("admin"), caption=caption, reply_markup=InlineKeyboardMarkup(buttons)) + + if msg.from_user.id != configGet("admin"): + buttons += [ + [ + InlineKeyboardButton(text=locale("sub_block", "button", locale=configGet("locale")), callback_data=f"sub_block_{msg.from_user.id}") + ], + [ + InlineKeyboardButton(text=locale("sub_unblock", "button", locale=configGet("locale")), callback_data=f"sub_unblock_{msg.from_user.id}") + ] + ] + + msg.reply_text(locale("sub_sent", "message", locale=user_locale), quote=True) + subLimit(msg.from_user) + + else: + msg.reply_text(locale("sub_cooldown", "message", locale=user_locale).format(str(configGet("timeout", "submission")))) + except AttributeError: + logWrite(f"from_user in function get_submission does not seem to contain id") \ No newline at end of file diff --git a/modules/scheduler.py b/modules/scheduler.py new file mode 100644 index 0000000..1e401d3 --- /dev/null +++ b/modules/scheduler.py @@ -0,0 +1,11 @@ +from datetime import datetime +from apscheduler.schedulers.asyncio import AsyncIOScheduler +from modules.utils import configGet +from modules.sender import send_content + +scheduler = AsyncIOScheduler() + +if configGet("post", "mode"): + for entry in configGet("time", "posting"): + dt_obj = datetime.strptime(entry, "%H:%M") + scheduler.add_job(send_content, "cron", hour=dt_obj.hour, minute=dt_obj.minute) \ No newline at end of file diff --git a/modules/sender.py b/modules/sender.py new file mode 100644 index 0000000..35c0d49 --- /dev/null +++ b/modules/sender.py @@ -0,0 +1,111 @@ +from os import listdir, sep +from random import choice +from shutil import move +from traceback import format_exc +from pyrogram.client import Client +from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton +from modules.logging import logWrite +from modules.utils import jsonLoad, jsonSave, configGet, locale + +def send_content(app: Client): + + # Send post to channel + try: + + index = jsonLoad(configGet("index", "locations")) + list_queue = listdir(configGet("queue", "locations")) + + for file in list_queue: + + if not file in index["sent"]: + + ext_match = False + + for ext in configGet("photo", "posting", "extensions"): + if file.endswith(ext): + ext_match = True + ext_type = "photo" + break + + for ext in configGet("video", "posting", "extensions"): + if file.endswith(ext): + ext_match = True + ext_type = "video" + break + + if not ext_match: + list_queue.remove(file) + + else: + list_queue.remove(file) + + if len(list_queue) > 0: + candidate_file = choice(list_queue) + candidate = configGet("queue", "locations")+sep+candidate_file + else: + logWrite(locale("post_empty", "console", locale=configGet("locale"))) + if configGet("error", "reports"): + app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale"))) + return + + if candidate_file in index["captions"]: + caption = index["captions"][candidate_file] + else: + caption = "" + + if configGet("enabled", "caption"): + if configGet("link", "caption") != None: + caption = f"{caption}\n\n[{configGet('text', 'caption')}]({configGet('link', 'caption')})" + else: + caption = f"{caption}\n\n{configGet('text', 'caption')}" + else: + caption = caption + + if ext_type == "photo": + + if configGet("enabled", "caption"): + if configGet("link", "caption") != None: + sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) + else: + sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) + else: + sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) + + elif ext_type == "video": + + if configGet("enabled", "caption"): + if configGet("link", "caption") != None: + sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) + else: + sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) + else: + sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) + + else: + return + + index["sent"].append(candidate_file) + index["last_id"] = sent.id + + jsonSave(index, configGet("index", "locations")) + + if configGet("move_sent", "posting"): + move(candidate, configGet("sent", "locations")+sep+candidate_file) + + 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"): + 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)] + ])) + + except Exception as exp: + logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), format_exc())) + if configGet("error", "reports"): + app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, format_exc())) + pass + + + # Work in progress + # Check last posts forwards + # check_forwards(app) \ No newline at end of file diff --git a/modules/submissions.py b/modules/submissions.py new file mode 100644 index 0000000..f75c27e --- /dev/null +++ b/modules/submissions.py @@ -0,0 +1,32 @@ +from time import time +from modules.utils import jsonLoad, jsonSave, configGet + +def subLimit(user): + submit = jsonLoad(configGet("submit", "locations")) + submit[str(user.id)] = time() + jsonSave(submit, configGet("submit", "locations")) + +def subLimited(user): + if user.id == configGet("admin"): + return False + else: + submit = jsonLoad(configGet("submit", "locations")) + if str(user.id) in submit: + if (time() - submit[str(user.id)]) < configGet("timeout", "submission"): + return True + else: + return False + else: + return False + +def subBlock(user): + blocked = jsonLoad(configGet("blocked", "locations")) + if user not in blocked: + blocked.append(user) + jsonSave(blocked, configGet("blocked", "locations")) + +def subUnblock(user): + blocked = jsonLoad(configGet("blocked", "locations")) + if user in blocked: + blocked.remove(user) + jsonSave(blocked, configGet("blocked", "locations")) \ No newline at end of file diff --git a/modules/utils.py b/modules/utils.py index d8731b1..e5604e0 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -110,7 +110,7 @@ except ModuleNotFoundError: def killProc(pid): if osname == "posix": - from signal import SIGKILL # type: ignore + from signal import SIGKILL kill(pid, SIGKILL) else: p = Process(pid) diff --git a/poster.py b/poster.py index 2fce1e7..7a8f3da 100644 --- a/poster.py +++ b/poster.py @@ -1,15 +1,10 @@ -from datetime import datetime -from os import sep, remove, getpid, listdir -from random import choice +from os import sep, remove, getpid from shutil import move from sys import exit -from time import time -from traceback import format_exc -from pathlib import Path from argparse import ArgumentParser -from apscheduler.schedulers.asyncio import AsyncIOScheduler from modules.logging import logWrite +from modules.scheduler import scheduler from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale # Args ===================================================================================================================================== @@ -73,11 +68,8 @@ if args.norun: # Import =================================================================================================================================== try: - import schedule # type: ignore - from pyrogram import Client, filters, idle # type: ignore - from pyrogram.types import ChatPermissions, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton, BotCommand, BotCommandScopeChat # type: ignore - from pyrogram.raw.types import UpdateChannelMessageForwards, InputChannel, InputPeerChannel - from pyrogram.raw.functions.stats import GetMessagePublicForwards + from modules.app import app + from pyrogram import idle except ModuleNotFoundError: print(locale("deps_missing", "console", locale=configGet("locale")), flush=True) exit() @@ -85,10 +77,6 @@ except ModuleNotFoundError: pid = getpid() -app = Client("duptsiaposter", bot_token=configGet("bot_token", "bot"), api_id=configGet("api_id", "bot"), api_hash=configGet("api_hash", "bot")) - -if configGet("post", "mode"): - scheduler = AsyncIOScheduler() # Work in progress # def check_forwards(app): @@ -115,291 +103,26 @@ if configGet("post", "mode"): # logWrite("Could not get last posts forwards due to {0} with traceback {1}".format(str(exp), traceback.format_exc()), debug=True) # if configGet("error", "reports"): -# app.send_message(configGet("admin"), traceback.format_exc()) # type: ignore +# app.send_message(configGet("admin"), traceback.format_exc()) # pass -def send_content(): - - # Send post to channel - try: - - index = jsonLoad(configGet("index", "locations")) - list_queue = listdir(configGet("queue", "locations")) - - for file in list_queue: - - if not file in index["sent"]: - - ext_match = False - - for ext in configGet("photo", "posting", "extensions"): - if file.endswith(ext): - ext_match = True - ext_type = "photo" - break - - for ext in configGet("video", "posting", "extensions"): - if file.endswith(ext): - ext_match = True - ext_type = "video" - break - - if not ext_match: - list_queue.remove(file) - - else: - list_queue.remove(file) - - if len(list_queue) > 0: - candidate_file = choice(list_queue) - candidate = configGet("queue", "locations")+sep+candidate_file - else: - logWrite(locale("post_empty", "console", locale=configGet("locale"))) - if configGet("error", "reports"): - app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale"))) # type: ignore - return - - if candidate_file in index["captions"]: - caption = index["captions"][candidate_file] - else: - caption = "" - - if configGet("enabled", "caption"): - if configGet("link", "caption") != None: - caption = f"{caption}\n\n[{configGet('text', 'caption')}]({configGet('link', 'caption')})" - else: - caption = f"{caption}\n\n{configGet('text', 'caption')}" - else: - caption = caption - - if ext_type == "photo": # type: ignore - - if configGet("enabled", "caption"): - if configGet("link", "caption") != None: - sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) # type: ignore - else: - sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) # type: ignore - else: - sent = app.send_photo(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) # type: ignore - - elif ext_type == "video": # type: ignore - - if configGet("enabled", "caption"): - if configGet("link", "caption") != None: - sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) # type: ignore - else: - sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) # type: ignore - else: - sent = app.send_video(configGet("channel", "posting"), candidate, caption=caption, disable_notification=configGet("silent", "posting")) # type: ignore - - else: - return - - index["sent"].append(candidate_file) - index["last_id"] = sent.id - - jsonSave(index, configGet("index", "locations")) - - if configGet("move_sent", "posting"): - move(candidate, configGet("sent", "locations")+sep+candidate_file) - - logWrite(locale("post_sent", "console", locale=configGet("locale")).format(candidate, ext_type, str(configGet("channel", "posting")), caption.replace("\n", "%n"), str(configGet("silent", "posting")))) # type: ignore - - if configGet("sent", "reports"): - 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)] # type: ignore - ])) # type: ignore - - except Exception as exp: - logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), format_exc())) - if configGet("error", "reports"): - app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, traceback.format_exc())) # type: ignore - pass - - - # Work in progress - # Check last posts forwards - # check_forwards(app) - -if configGet("submit", "mode"): - @app.on_message(~ filters.scheduled & filters.command(["start"], prefixes="/")) - def cmd_start(app, msg): - if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): - msg.reply_text(locale("start", "message", locale=msg.from_user.language_code)) - -if configGet("submit", "mode"): - @app.on_message(~ filters.scheduled & filters.command(["rules", "help"], prefixes="/")) - def cmd_rules(app, msg): - if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): - msg.reply_text(locale("rules", "message", locale=msg.from_user.language_code)) - # Work in progress # @app.on_message(~ filters.scheduled & filters.command(["forwards"], prefixes="/")) # def cmd_forwards(app, msg): # check_forwards(app) -@app.on_message(~ filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"])) -def cmd_kill(app, msg): - if msg.from_user.id == configGet("admin"): - logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid))) - msg.reply_text(locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) - killProc(pid) +# Imports ===================================================================================================================================== - -# Submission ===================================================================================================================================== -def subLimit(user): - submit = jsonLoad(configGet("submit", "locations")) - submit[str(user.id)] = time() - jsonSave(submit, configGet("submit", "locations")) - -def subLimited(user): - if user.id == configGet("admin"): - return False - else: - submit = jsonLoad(configGet("submit", "locations")) - if str(user.id) in submit: - if (time() - submit[str(user.id)]) < configGet("timeout", "submission"): - return True - else: - return False - else: - return False - -def subBlock(user): - blocked = jsonLoad(configGet("blocked", "locations")) - if user not in blocked: - blocked.append(user) - jsonSave(blocked, configGet("blocked", "locations")) - -def subUnblock(user): - blocked = jsonLoad(configGet("blocked", "locations")) - if user in blocked: - blocked.remove(user) - jsonSave(blocked, configGet("blocked", "locations")) +from modules.commands.general import * +from modules.commands_register import register_commands if configGet("submit", "mode"): - @app.on_message(~ filters.scheduled & filters.photo | filters.video | filters.animation | filters.document) - def get_submission(_, msg): - try: - if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): - user_locale = msg.from_user.language_code - if not subLimited(msg.from_user): - - if msg.document != None: - if msg.document.mime_type not in configGet("mime_types", "submission"): - msg.reply_text(locale("mime_not_allowed", "message", locale=user_locale), quote=True) - return - 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) - return - - if msg.video != None: - 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) - return - - buttons = [ - [ - InlineKeyboardButton(text=locale("sub_yes", "button", locale=configGet("locale")), callback_data=f"sub_yes_{msg.from_user.id}_{msg.id}") - ] - ] - - if msg.caption != None: - caption = str(msg.caption) - buttons[0].append( - InlineKeyboardButton(text=locale("sub_yes_caption", "button", locale=configGet("locale")), callback_data=f"sub_yes_{msg.from_user.id}_{msg.id}_caption") - ) - buttons[0].append( - InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{msg.from_user.id}_{msg.id}") - ) - else: - caption = "" - buttons[0].append( - InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{msg.from_user.id}_{msg.id}") - ) - - caption += locale("sub_by", "message", locale=locale(configGet("locale"))) - - if msg.from_user.first_name != None: - caption += f" {msg.from_user.first_name}" - if msg.from_user.last_name != None: - caption += f" {msg.from_user.last_name}" - if msg.from_user.username != None: - caption += f" (@{msg.from_user.username})" - if msg.from_user.phone_number != None: - caption += f" ({msg.from_user.phone_number})" - - msg.copy(configGet("admin"), caption=caption, reply_markup=InlineKeyboardMarkup(buttons)) - - if msg.from_user.id != configGet("admin"): - buttons += [ - [ - InlineKeyboardButton(text=locale("sub_block", "button", locale=configGet("locale")), callback_data=f"sub_block_{msg.from_user.id}") - ], - [ - InlineKeyboardButton(text=locale("sub_unblock", "button", locale=configGet("locale")), callback_data=f"sub_unblock_{msg.from_user.id}") - ] - ] - - msg.reply_text(locale("sub_sent", "message", locale=user_locale), quote=True) - subLimit(msg.from_user) - - else: - msg.reply_text(locale("sub_cooldown", "message", locale=user_locale).format(str(configGet("timeout", "submission")))) - except AttributeError: - logWrite(f"from_user in function get_submission does not seem to contain id") - -@app.on_callback_query(filters.regex("sub_yes_[\s\S]*_[\s\S]*")) # type: ignore -def callback_query_yes(app, clb): # type: ignore - fullclb = clb.data.split("_") - user_locale = clb.from_user.language_code - try: - submission = app.get_messages(int(fullclb[2]), int(fullclb[3])) - except: - clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) - return - try: - media = app.download_media(submission, file_name=configGet("queue", "locations")+sep) - if clb.data.endswith("_caption"): - index = jsonLoad(configGet("index", "locations")) - index["captions"][Path(media).name] = submission.caption - jsonSave(index, configGet("index", "locations")) - except: - clb.answer(text=locale("sub_media_unavail", "message", locale=user_locale), show_alert=True) - return - 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) - -@app.on_callback_query(filters.regex("sub_no_[\s\S]*_[\s\S]*")) # type: ignore -def callback_query_no(app, clb): # type: ignore - fullclb = clb.data.split("_") - user_locale = clb.from_user.language_code - try: - submission = app.get_messages(int(fullclb[2]), int(fullclb[3])) - except: - clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) - return - 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) - -@app.on_callback_query(filters.regex("sub_block_[\s\S]*")) # type: ignore -def callback_query_block(app, clb): # type: ignore - fullclb = clb.data.split("_") - user_locale = clb.from_user.language_code - app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale"))) - subBlock(int(fullclb[2])) - 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]*")) # type: ignore -def callback_query_unblock(app, clb): # type: ignore - fullclb = clb.data.split("_") - user_locale = clb.from_user.language_code - app.send_message(int(fullclb[2]), locale("sub_msg_unavail", "message", locale=configGet("locale"))) - subUnblock(int(fullclb[2])) - clb.answer(text=locale("sub_unblock", "callback", locale=user_locale).format(fullclb[2]), show_alert=True) + from modules.callbacks.submission import * + from modules.commands.mode_submit import * + from modules.handlers.submission import * #=========================================================================================================================================== # Work in progress @@ -413,69 +136,22 @@ def callback_query_unblock(app, clb): # type: ignore # # else: # # logWrite(f"Got raw update of type {type(update)} with contents {update}", debug=True) -if configGet("post", "mode"): - - for entry in configGet("time", "posting"): - dt_obj = datetime.strptime(entry, "%H:%M") - scheduler.add_job(send_content, "cron", hour=dt_obj.hour, minute=dt_obj.minute) - # schedule.every().day.at(entry).do(send_content) - - # def background_task(): - # try: - # while True: - # try: - # schedule.run_pending() - # time.sleep(1) - # except: - # pass - # except Exception as exp: - # logWrite(locale("exception_occured", "console", locale=configGet("locale")).format(exp)) - # except KeyboardInterrupt: - # logWrite(locale("keyboard_interrupt", "console", locale=configGet("locale"))) - # if configGet("shutdown", "reports"): - # app.send_message(configGet("admin"), locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) # type: ignore - # killProc(pid) - if __name__ == "__main__": logWrite(locale("startup", "console", locale=configGet("locale")).format(str(pid))) - app.start() # type: ignore + app.start() if configGet("startup", "reports"): - app.send_message(configGet("admin"), locale("startup", "message", locale=configGet("locale")).format(str(pid))) # type: ignore + app.send_message(configGet("admin"), locale("startup", "message", locale=configGet("locale")).format(str(pid))) if configGet("post", "mode"): scheduler.start() - # t = Thread(target=background_task) - # t.start() - if configGet("submit", "mode"): - # Registering user commands - for entry in listdir(configGet("locale", "locations")): - if entry.endswith(".json"): - commands_list = [] - for command in configGet("commands"): - commands_list.append(BotCommand(command, locale(command, "commands", locale=entry.replace(".json", "")))) - app.set_bot_commands(commands_list, language_code=entry.replace(".json", "")) # type: ignore - - # Registering user commands for fallback locale - commands_list = [] - for command in configGet("commands"): - commands_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale_fallback")))) - app.set_bot_commands(commands_list) # type: ignore - - # Registering admin commands - commands_admin_list = [] - if configGet("submit", "mode"): - for command in configGet("commands"): - commands_admin_list.append(BotCommand(command, locale(command, "commands", locale=configGet("locale")))) - for command in configGet("commands_admin"): - 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"))) # type: ignore + register_commands(app) idle() - app.send_message(configGet("admin"), locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) # type: ignore + app.send_message(configGet("admin"), locale("shutdown", "message", locale=configGet("locale")).format(str(pid))) logWrite(locale("shutdown", "console", locale=configGet("locale")).format(str(pid))) killProc(pid) \ No newline at end of file