From 0f4957f46472789aad7f9a7dfd88b7e72dc6530d Mon Sep 17 00:00:00 2001 From: arshsisodiya Date: Mon, 2 May 2022 15:38:48 +0530 Subject: [PATCH] improve logging - Fixed bot crashing if FSUB_CHAT_ID env var is left empty - Added seprate warning if AUTO_DELETE_UPLOAD_MESSAGE_DURATION env var is not added - Improve logging - Many misc improvements --- bot/__init__.py | 21 ++++++------ .../upload_utils/pyrogramEngine.py | 32 +++++++++---------- bot/modules/clone.py | 3 +- bot/modules/mirror.py | 15 ++++++--- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/bot/__init__.py b/bot/__init__.py index d059cb8765..9e7af4b454 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -175,7 +175,7 @@ def get_client(): for chats in achats: MIRROR_LOGS.add(int(chats)) except: - logging.warning('Logs Chat Details not provided!') + logging.warning('Mirror Logs Chat Details not provided! Proceeding Without it') pass if ospath.exists("link_logs.txt"): @@ -212,7 +212,7 @@ def get_client(): for chats in achats: LEECH_LOG.add(int(chats)) except: - logging.warning('Leech Log Channel ID not Provided!') + logging.warning('Leech Log Channel ID not Provided! You will not be able to use leech features') pass try: @@ -221,7 +221,6 @@ def get_client(): for chats in achats: LEECH_LOG_ALT.add(int(chats)) except: - logging.warning('Leech Log alt Channel ID not Provided!') pass try: BOT_TOKEN = getConfig('BOT_TOKEN') @@ -232,13 +231,16 @@ def get_client(): DOWNLOAD_STATUS_UPDATE_INTERVAL = int(getConfig('DOWNLOAD_STATUS_UPDATE_INTERVAL')) OWNER_ID = int(getConfig('OWNER_ID')) AUTO_DELETE_MESSAGE_DURATION = int(getConfig('AUTO_DELETE_MESSAGE_DURATION')) - AUTO_DELETE_UPLOAD_MESSAGE_DURATION = int(getConfig('AUTO_DELETE_UPLOAD_MESSAGE_DURATION')) TELEGRAM_API = getConfig('TELEGRAM_API') TELEGRAM_HASH = getConfig('TELEGRAM_HASH') except KeyError as e: - LOGGER.error("One or more env variables missing! Exiting now") + LOGGER.error("One or more Required env variables missing! Exiting now") + exit(1) +try: + AUTO_DELETE_UPLOAD_MESSAGE_DURATION = int(getConfig('AUTO_DELETE_UPLOAD_MESSAGE_DURATION')) +except KeyError as e: + LOGGER.error("AUTO_DELETE_UPLOAD_MESSAGE_DURATION var missing! Exiting now") exit(1) - LOGGER.info("Generating BOT_STRING_SESSION") app = Client('pyrogram', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN, no_updates=True) @@ -586,9 +588,10 @@ def aria2c_init(): try: FSUB_CHANNEL_ID = int(getConfig('FSUB_CHANNEL_ID')) -except KeyError: - FSUB_CHANNEL_ID = "" - +except Exception as error: + LOGGER.warning(f"FSUB_CHANNEL_ID env is empty:\n{error}") + FSUB_CHANNEL_ID = "-1001576780814" + pass try: CHANNEL_USERNAME: str = getConfig('CHANNEL_USERNAME').replace("@", "") if len(CHANNEL_USERNAME) == 0: diff --git a/bot/helper/mirror_utils/upload_utils/pyrogramEngine.py b/bot/helper/mirror_utils/upload_utils/pyrogramEngine.py index 7249a392ed..65cb3c5b8d 100644 --- a/bot/helper/mirror_utils/upload_utils/pyrogramEngine.py +++ b/bot/helper/mirror_utils/upload_utils/pyrogramEngine.py @@ -118,8 +118,8 @@ def __upload_file(self, up_path, file_, dirpath): if BOT_PM: try: app.send_video(chat_id=self.__user_id, video=self.__sent_msg.video.file_id, caption=cap_mono) - except Exception as f: - LOGGER.error(f"Failed To Send Video in PM:\n{f}") + except Exception as err: + LOGGER.error(f"Failed To Send Video in PM:\n{err}") if LEECH_LOG_ALT: try: for i in self.__leech_log_alt: @@ -143,13 +143,13 @@ def __upload_file(self, up_path, file_, dirpath): if BOT_PM: try: app.send_audio(chat_id=self.__user_id, audio=self.__sent_msg.audio.file_id, caption=cap_mono) - except Exception as f: - LOGGER.error(f"Failed To Send Audio in PM:\n{f}") + except Exception as err: + LOGGER.error(f"Failed To Send Audio in PM:\n{err}") if LEECH_LOG_ALT: try: for i in self.__leech_log_alt: app.send_audio(chat_id=i, audio=self.__sent_msg.audio.file_id, caption=cap_mono) - except Exception as f: + except Exception as err: LOGGER.error(f"Failed to send Audio in Alt Leech Log:\n{err}") elif file_.upper().endswith(IMAGE_SUFFIXES): @@ -166,18 +166,18 @@ def __upload_file(self, up_path, file_, dirpath): try: app.send_photo(chat_id=self.__user_id, photo=self.__sent_msg.photo.file_id, caption=cap_mono) - except Exception as f: - LOGGER.error(f"Failed To Send Image in PM:\n{f}") + except Exception as err: + LOGGER.error(f"Failed To Send Image in PM:\n{err}") if LEECH_LOG_ALT: try: app.send_photo(chat_id=i, photo=self.__sent_msg.photo.file_id, caption=cap_mono) - except Exception as f: - LOGGER.error(f"Failed To Send Image in Alt Leech Log:\n{f}") - except Exception: - LOGGER.warning("Image Leech is Blocked by Owner") + except Exception as err: + LOGGER.error(f"Failed To Send Image in Alt Leech Log:\n{err}") + except Exception as err: + LOGGER.warning(f"Image Leech is Blocked by Owner:\n{err}") else: - LOGGER.warning("Image Leech is Blocked by Owner") + LOGGER.warning(f"Image Leech is Blocked by Owner") pass elif file_.upper().endswith(TEXT_SUFFIXES): @@ -204,14 +204,14 @@ def __upload_file(self, up_path, file_, dirpath): if BOT_PM: try: app.send_document(chat_id=self.__user_id, document=self.__sent_msg.document.file_id, caption=cap_mono) - except Exception as f: - LOGGER.error(f"Failed To Send Document in PM:\n{f}") + except Exception as err: + LOGGER.error(f"Failed To Send Document in PM:\n{err}") if LEECH_LOG_ALT: try: for i in self.__leech_log_alt: app.send_document(chat_id=i, document=self.__sent_msg.document.file_id, caption=cap_mono) - except Exception as f: - LOGGER.error(f"Failed To Send Document in Alt Leech Log:\n{f}") + except Exception as err: + LOGGER.error(f"Failed To Send Document in Alt Leech Log:\n{err}") except FloodWait as f: LOGGER.warning(str(f)) sleep(f.x) diff --git a/bot/modules/clone.py b/bot/modules/clone.py index 7e4267af1f..3735a2232c 100644 --- a/bot/modules/clone.py +++ b/bot/modules/clone.py @@ -31,7 +31,8 @@ def cloneNode(update, context): message = sendMarkup(str(f"️Dear {uname}, You haven't join our Updates Channel yet.\n\nKindly Join @{CHANNEL_USERNAME} To Use Bots. "), bot, update, reply_markup) Thread(target=auto_delete_upload_message, args=(bot, update.message, message)).start() return - except: + except Exception as error: + LOGGER.warning(error) pass if BOT_PM: try: diff --git a/bot/modules/mirror.py b/bot/modules/mirror.py index b316e6b3a0..07a14804fa 100644 --- a/bot/modules/mirror.py +++ b/bot/modules/mirror.py @@ -191,7 +191,8 @@ def onDownloadError(self, error): if reply_to is not None: try: reply_to.delete() - except: + except Exception as error: + LOGGER.warning(error) pass error = error.replace('<', ' ').replace('>', ' ') with download_dict_lock: @@ -246,7 +247,8 @@ def onUploadComplete(self, link: str, size, files, folders, typ, name: str): if reply_to is not None: try: reply_to.delete() - except: + except Exception as error: + LOGGER.warning(error) pass auto_delete_message = int(AUTO_DELETE_UPLOAD_MESSAGE_DURATION / 60) if self.message.chat.type == 'private': @@ -375,7 +377,8 @@ def onUploadComplete(self, link: str, size, files, folders, typ, name: str): if self.isZip: try: osremove(f'{DOWNLOAD_DIR}{self.uid}/{name}') - except: + except Exception as error: + LOGGER.warning(error) pass msg = sendMarkup(msg + uploader + pmwarn_mirror + warnmsg, self.bot, self.update, InlineKeyboardMarkup(buttons.build_menu(2))) Thread(target=auto_delete_upload_message, args=(bot, self.message, msg)).start() @@ -400,7 +403,8 @@ def onUploadError(self, error): if reply_to is not None: try: reply_to.delete() - except: + except Exception as error: + LOGGER.warning(error) pass e_str = error.replace('<', '').replace('>', '') with download_dict_lock: @@ -433,7 +437,8 @@ def _mirror(bot, update, isZip=False, extract=False, isQbit=False, isLeech=False bot, update, reply_markup) Thread(target=auto_delete_upload_message, args=(bot, update.message, message)).start() return - except: + except Exception as error: + LOGGER.warning(error) pass if isLeech and length_of_leechlog == 0: try: