forbid_joins.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  2. # █▀█ █ █ █ █▀█ █▀▄ █
  3. # © Copyright 2022
  4. # https://t.me/hikariatama
  5. #
  6. # 🔒 Licensed under the GNU AGPLv3
  7. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  8. from telethon import TelegramClient
  9. from telethon.utils import is_list_like
  10. import inspect
  11. import logging
  12. from . import loader
  13. logger = logging.getLogger(__name__)
  14. # ⚠️⚠️ WARNING! ⚠️⚠️
  15. # If you are a module developer, and you'll try to bypass this protection to
  16. # force user join your channel, you will be added to SCAM modules
  17. # list and you will be banned from Hikka federation.
  18. # Let USER decide, which channel he will follow. Do not be so petty
  19. # I hope, you understood me.
  20. # Thank you
  21. def install_join_forbidder(client: TelegramClient) -> TelegramClient:
  22. if hasattr(client, "_forbid_join_tag"):
  23. return client
  24. old_call = client._call
  25. async def new_call(
  26. sender: "MTProtoSender", # type: ignore
  27. request: "TLRequest", # type: ignore
  28. ordered: bool = False,
  29. flood_sleep_threshold: int = None,
  30. ):
  31. not_tuple = False
  32. if not is_list_like(request):
  33. not_tuple = True
  34. request = (request,)
  35. new_request = []
  36. for item in request:
  37. if item.CONSTRUCTOR_ID in {615851205, 1817183516}:
  38. try:
  39. if next(
  40. frame_info.frame.f_locals["self"]
  41. for frame_info in inspect.stack()
  42. if hasattr(frame_info, "frame")
  43. and hasattr(frame_info.frame, "f_locals")
  44. and isinstance(frame_info.frame.f_locals, dict)
  45. and "self" in frame_info.frame.f_locals
  46. and isinstance(frame_info.frame.f_locals["self"], loader.Module)
  47. and frame_info.frame.f_locals["self"].__class__.__name__
  48. not in {"APIRatelimiterMod", "ForbidJoinMod"}
  49. ).__class__.__name__ not in {"HelpMod", "LoaderMod"}:
  50. logger.debug(
  51. "🎉 I protected you from unintented"
  52. f" {item.__class__.__name__} ({item})!"
  53. )
  54. continue
  55. except StopIteration:
  56. pass
  57. new_request += [item]
  58. if not new_request:
  59. return
  60. return await old_call(
  61. sender,
  62. new_request[0] if not_tuple else tuple(new_request),
  63. ordered,
  64. flood_sleep_threshold,
  65. )
  66. client._call = new_call
  67. client._joins_forbidden = True
  68. logger.debug("🎉 JoinForbidder installed!")
  69. return client