errors.py 1.6 KB

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