errors.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """ WRITTEN BY @pokurt, https://github.com/pokurt"""
  2. import sys
  3. import traceback
  4. from functools import wraps
  5. from pyrogram.errors.exceptions.forbidden_403 import ChatWriteForbidden
  6. from wbb import LOG_GROUP_ID, app
  7. def split_limits(text):
  8. if len(text) < 2048:
  9. return [text]
  10. lines = text.splitlines(True)
  11. small_msg = ""
  12. result = []
  13. for line in lines:
  14. if len(small_msg) + len(line) < 2048:
  15. small_msg += line
  16. else:
  17. result.append(small_msg)
  18. small_msg = line
  19. else:
  20. result.append(small_msg)
  21. return result
  22. def capture_err(func):
  23. @wraps(func)
  24. async def capture(client, message, *args, **kwargs):
  25. try:
  26. return await func(client, message, *args, **kwargs)
  27. except ChatWriteForbidden:
  28. await app.leave_chat(message.chat.id)
  29. return
  30. except Exception as err:
  31. exc_type, exc_obj, exc_tb = sys.exc_info()
  32. errors = traceback.format_exception(
  33. etype=exc_type,
  34. value=exc_obj,
  35. tb=exc_tb,
  36. )
  37. error_feedback = split_limits(
  38. "**ERROR** | `{}` | `{}`\n\n```{}```\n\n```{}```\n".format(
  39. 0 if not message.from_user else message.from_user.id,
  40. 0 if not message.chat else message.chat.id,
  41. message.text or message.caption,
  42. "".join(errors),
  43. ),
  44. )
  45. for x in error_feedback:
  46. await app.send_message(LOG_GROUP_ID, x)
  47. raise err
  48. return capture