From 04093d0a2046e01785a859953ca10c7d9ef86130 Mon Sep 17 00:00:00 2001 From: profitroll Date: Tue, 13 Dec 2022 14:33:22 +0100 Subject: [PATCH] Exception messages implemented --- classes/holo_user.py | 105 ++++++++++++++++++++++--------------------- locale/uk.json | 2 + 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/classes/holo_user.py b/classes/holo_user.py index fb564c7..dbbd1ab 100644 --- a/classes/holo_user.py +++ b/classes/holo_user.py @@ -1,3 +1,4 @@ +from traceback import print_exc from app import app, isAnAdmin from typing import Any, List, Union from pyrogram.types import User, ChatMember, ChatPrivileges, Chat, Message, Photo, Video, Document, Animation, Voice @@ -72,8 +73,8 @@ class HoloUser(): logWrite(f"Set attribute {key} of user {self.id} to {value}") async def message(self, + context: Message, origin: Union[Message, None] = None, - context: Union[Message, None] = None, text: Union[str, None] = None, caption: Union[str, None] = None, photo: Union[str, Photo, None] = None, @@ -108,60 +109,64 @@ class HoloUser(): else: caption = locale("message_reply_notice", "message") - if origin is not None: + try: + + if origin is not None: + + if photo is not None: + if isinstance(photo, Photo): + photo = photo.file_id + new_message = await origin.reply_photo(photo, caption=caption, quote=True) + elif video is not None: + if isinstance(video, Video): + video = video.file_id + new_message = await origin.reply_video(video, caption=caption, quote=True) + elif file is not None: + if isinstance(file, Document): + file = file.file_id + new_message = await origin.reply_document(file, caption=caption, quote=True) + elif animation is not None: + if isinstance(animation, Animation): + animation = animation.file_id + new_message = await origin.reply_animation(animation, caption=caption, quote=True) + elif voice is not None: + if isinstance(voice, Voice): + voice = voice.file_id + new_message = await origin.reply_voice(voice, caption=caption, quote=True) + else: + new_message = await origin.reply_text(text, quote=True) - if photo is not None: - if isinstance(photo, Photo): - photo = photo.file_id - new_message = await origin.reply_photo(photo, caption=caption, quote=True) - elif video is not None: - if isinstance(video, Video): - video = video.file_id - new_message = await origin.reply_video(video, caption=caption, quote=True) - elif file is not None: - if isinstance(file, Document): - file = file.file_id - new_message = await origin.reply_document(file, caption=caption, quote=True) - elif animation is not None: - if isinstance(animation, Animation): - animation = animation.file_id - new_message = await origin.reply_animation(animation, caption=caption, quote=True) - elif voice is not None: - if isinstance(voice, Voice): - voice = voice.file_id - new_message = await origin.reply_voice(voice, caption=caption, quote=True) else: - new_message = await origin.reply_text(text, quote=True) - else: + if photo is not None: + if isinstance(photo, Photo): + photo = photo.file_id + new_message = await app.send_photo(self.id, photo, caption=caption) + elif video is not None: + if isinstance(video, Video): + video = video.file_id + new_message = await app.send_video(self.id, video, caption=caption) + elif file is not None: + if isinstance(file, Document): + file = file.file_id + new_message = await app.send_document(self.id, file, caption=caption) + elif animation is not None: + if isinstance(animation, Animation): + animation = animation.file_id + new_message = await app.send_animation(animation, caption=caption, quote=True) + elif voice is not None: + if isinstance(voice, Voice): + voice = voice.file_id + new_message = await app.send_voice(voice, caption=caption, quote=True) + else: + new_message = await app.send_message(self.id, text) - if photo is not None: - if isinstance(photo, Photo): - photo = photo.file_id - new_message = await app.send_photo(self.id, photo, caption=caption) - elif video is not None: - if isinstance(video, Video): - video = video.file_id - new_message = await app.send_video(self.id, video, caption=caption) - elif file is not None: - if isinstance(file, Document): - file = file.file_id - new_message = await app.send_document(self.id, file, caption=caption) - elif animation is not None: - if isinstance(animation, Animation): - animation = animation.file_id - new_message = await app.send_animation(animation, caption=caption, quote=True) - elif voice is not None: - if isinstance(voice, Voice): - voice = voice.file_id - new_message = await app.send_voice(voice, caption=caption, quote=True) - else: - new_message = await app.send_message(self.id, text) + await context.reply_text(locale("message_sent", "message"), quote=should_quote(context)) + col_messages.insert_one({"origin": {"chat": context.chat.id, "id": context.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}}) - # new_message = await app.send_message(self.id, text+locale("message_reply_notice", "message")) - await context.reply_text(locale("message_sent", "message"), quote=should_quote(context)) - - col_messages.insert_one({"origin": {"chat": context.chat.id, "id": context.id}, "destination": {"chat": new_message.chat.id, "id": new_message.id}}) + except Exception as exp: + await context.reply_text(locale("message_traceback", "message").format(context.from_user.id, self.id, exp, print_exc())) + await context.reply_text(locale("message_error", "message"), quote=should_quote(context)) async def set_label(self, chat: Chat, label: str): """Set label in destination group diff --git a/locale/uk.json b/locale/uk.json index b775900..7e62a86 100644 --- a/locale/uk.json +++ b/locale/uk.json @@ -60,6 +60,8 @@ "message_invalid_syntax": "Неправильний синтаксис!\nТреба: `/message ID ПОВІДОМЛЕННЯ`", "message_from": "Повідомлення від **{0}** (`{1}`):\n\n", "message_reply_notice": "\n\n**Щоб надіслати відповідь на це повідомлення, тегніть його.**", + "message_error": "⚠️ **Сталась помилка**\nНе вдалось надіслати ваше повідомлення. Розробника повідомлено про цю помилку.", + "message_traceback": "⚠️ **Сталась помилка**\nПомилка повідомлень: `{0}` -> `{1}`\nПомилка: `{2}`\n\nTraceback:\n```\n{3}\n```", "question_titles": { "question1": "Ім'я/звертання:", "question2": "День народження:",