From e65d98deca0962b2be3b74fcc74c6189d11ffb16 Mon Sep 17 00:00:00 2001 From: profitroll Date: Tue, 9 Aug 2022 10:01:02 +0200 Subject: [PATCH] Fixed reports ignorance --- main.py | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 167b312..fb3ed98 100644 --- a/main.py +++ b/main.py @@ -8,9 +8,11 @@ import time import traceback import schedule +pid = os.getpid() + try: from pyrogram import Client, filters, idle # type: ignore - from pyrogram.types import ChatPermissions, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton # type: ignore + from pyrogram.types import ChatPermissions, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton, BotCommand, BotCommandScopeChat # type: ignore except ModuleNotFoundError: print(f"Module pyrogram is not installed. Run 'pip3 install -r requirements.txt' and restart the program.", flush=True) sys.exit() @@ -57,7 +59,8 @@ def send_content(): candidate = configGet("queue", "locations")+os.sep+candidate_file else: logWrite(f"Could not send content due to queue folder empty with allowed extensions") - app.send_message(configGet("admin", "reports"), f"Could not send content: `Queue folder is empty or contains only unsupported or already sent files.`") # type: ignore + if configGet("error", "reports"): + app.send_message(configGet("admin", "reports"), f"Could not send content: `Queue folder is empty or contains only unsupported or already sent files.`") # type: ignore return if ext_type == "photo": # type: ignore @@ -86,13 +89,23 @@ def send_content(): if configGet("move_sent", "posting"): shutil.move(candidate, configGet("sent", "locations")+os.sep+candidate_file) - app.send_message(configGet("admin", "reports"), f"Posted `{candidate_file}`", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup([ - [InlineKeyboardButton("View in channel", url=sent.link)] # type: ignore - ])) # type: ignore + if configGet("sent", "reports"): + app.send_message(configGet("admin", "reports"), f"Posted `{candidate_file}`", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup([ + [InlineKeyboardButton("View in channel", url=sent.link)] # type: ignore + ])) # type: ignore except Exception as exp: logWrite(f"Could not send content due to {exp}") - app.send_message(configGet("admin", "reports"), f"Could not send content due to `{exp}`\n\nTraceback:\n```{traceback.format_exc()}```") # type: ignore + if configGet("error", "reports"): + app.send_message(configGet("admin", "reports"), f"Could not send content due to `{exp}`\n\nTraceback:\n```{traceback.format_exc()}```") # type: ignore + + +@app.on_message(~ filters.scheduled & filters.command(["kill", "die", "reboot"], prefixes=["", "/"])) +def kill(app, msg): + + if msg.from_user.id == configGet("admin", "reports"): + msg.reply_text(f"Shutting down bot with pid `{pid}`") + os.system('kill -9 '+str(pid)) for entry in configGet("time", "posting"): @@ -111,22 +124,27 @@ def background_task(): logWrite(f"Exception {exp} happened on task execution") except KeyboardInterrupt: logWrite('\nShutting down...') - app.send_message(configGet("admin", "reports"), f"Shutting down with PID `{os.getpid()}`") # type: ignore - os.system('kill -9 '+str(os.getpid())) + app.send_message(configGet("admin", "reports"), f"Shutting down with PID `{pid}`") # type: ignore + os.system('kill -9 '+str(pid)) if __name__ == "__main__": - logWrite(f'Starting with PID {os.getpid()}') + logWrite(f'Starting with PID {pid}') app.start() # type: ignore - app.send_message(configGet("admin", "reports"), f"Starting with pid `{os.getpid()}`") # type: ignore + app.send_message(configGet("admin", "reports"), f"Starting with pid `{pid}`") # type: ignore t = Thread(target=background_task) t.start() + app.set_bot_commands([ # type: ignore + BotCommand("reboot", "Reboot the bot"), + ], + scope=BotCommandScopeChat(chat_id=configGet("admin", "reports"))) + idle() - app.send_message(configGet("admin", "reports"), f"Shutting down with pid `{os.getpid()}`") # type: ignore - logWrite(f'Shutting down with PID {os.getpid()}') + app.send_message(configGet("admin", "reports"), f"Shutting down with pid `{pid}`") # type: ignore + logWrite(f'Shutting down with PID {pid}') - subprocess.call(f'kill -9 {os.getpid()}', shell=True) # type: ignore \ No newline at end of file + subprocess.call(f'kill -9 {pid}', shell=True) # type: ignore \ No newline at end of file