python.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. import contextlib
  9. import itertools
  10. import logging
  11. import sys
  12. from types import ModuleType
  13. import os
  14. from typing import Any
  15. import telethon
  16. from meval import meval
  17. from telethon.errors.rpcerrorlist import MessageIdInvalidError
  18. from telethon.tl.types import Message
  19. from .. import loader, main, utils
  20. from ..log import HikkaException
  21. logger = logging.getLogger(__name__)
  22. @loader.tds
  23. class PythonMod(loader.Module):
  24. """Evaluates python code"""
  25. strings = {
  26. "name": "Python",
  27. "eval": "<b>🎬 Code:</b>\n<code>{}</code>\n<b>🪄 Result:</b>\n<code>{}</code>",
  28. "err": "<b>🎬 Code:</b>\n<code>{}</code>\n\n<b>🚫 Error:</b>\n{}",
  29. }
  30. strings_ru = {
  31. "eval": "<b>🎬 Код:</b>\n<code>{}</code>\n<b>🪄 Результат:</b>\n<code>{}</code>",
  32. "err": "<b>🎬 Код:</b>\n<code>{}</code>\n\n<b>🚫 Ошибка:</b>\n{}",
  33. "_cmd_doc_eval": "Алиас для команды .e",
  34. "_cmd_doc_e": "Выполняет Python кодировка",
  35. "_cls_doc": "Выполняет Python код",
  36. }
  37. async def client_ready(self, client, _):
  38. self._phone = (await client.get_me()).phone
  39. @loader.owner
  40. async def evalcmd(self, message: Message):
  41. """Alias for .e command"""
  42. await self.ecmd(message)
  43. @loader.owner
  44. async def ecmd(self, message: Message):
  45. """Evaluates python code"""
  46. ret = self.strings("eval")
  47. try:
  48. result = await meval(
  49. utils.get_args_raw(message),
  50. globals(),
  51. **await self.getattrs(message),
  52. )
  53. except Exception:
  54. item = HikkaException.from_exc_info(*sys.exc_info())
  55. exc = (
  56. "\n<b>🪐 Full stack:</b>\n\n"
  57. + "\n".join(item.full_stack.splitlines()[:-1])
  58. + "\n\n"
  59. + "😵 "
  60. + item.full_stack.splitlines()[-1]
  61. )
  62. exc = exc.replace(str(self._phone), "📵")
  63. if os.environ.get("DATABASE_URL"):
  64. exc = exc.replace(
  65. os.environ.get("DATABASE_URL"),
  66. "postgre://**************************",
  67. )
  68. if os.environ.get("hikka_session"):
  69. exc = exc.replace(
  70. os.environ.get("hikka_session"),
  71. "StringSession(**************************)",
  72. )
  73. await utils.answer(
  74. message,
  75. self.strings("err").format(
  76. utils.escape_html(utils.get_args_raw(message)),
  77. exc,
  78. ),
  79. )
  80. return
  81. if callable(getattr(result, "stringify", None)):
  82. with contextlib.suppress(Exception):
  83. result = str(result.stringify())
  84. result = str(result)
  85. ret = ret.format(
  86. utils.escape_html(utils.get_args_raw(message)),
  87. utils.escape_html(result),
  88. )
  89. ret = ret.replace(str(self._phone), "📵")
  90. if postgre := os.environ.get("DATABASE_URL") or main.get_config_key(
  91. "postgre_uri"
  92. ):
  93. ret = ret.replace(postgre, "postgre://**************************")
  94. if redis := os.environ.get("REDIS_URL") or main.get_config_key("redis_uri"):
  95. ret = ret.replace(redis, "redis://**************************")
  96. if os.environ.get("hikka_session"):
  97. ret = ret.replace(
  98. os.environ.get("hikka_session"),
  99. "StringSession(**************************)",
  100. )
  101. with contextlib.suppress(MessageIdInvalidError):
  102. await utils.answer(message, ret)
  103. async def getattrs(self, message: Message) -> dict:
  104. reply = await message.get_reply_message()
  105. return {
  106. **{
  107. "message": message,
  108. "client": self._client,
  109. "reply": reply,
  110. "r": reply,
  111. **self.get_sub(telethon.tl.types),
  112. **self.get_sub(telethon.tl.functions),
  113. "event": message,
  114. "chat": message.to_id,
  115. "telethon": telethon,
  116. "utils": utils,
  117. "main": main,
  118. "loader": loader,
  119. "f": telethon.tl.functions,
  120. "c": self._client,
  121. "m": message,
  122. "lookup": self.lookup,
  123. "self": self,
  124. "db": self.db,
  125. },
  126. }
  127. def get_sub(self, obj: Any, _depth: int = 1) -> dict:
  128. """Get all callable capitalised objects in an object recursively, ignoring _*"""
  129. return {
  130. **dict(
  131. filter(
  132. lambda x: x[0][0] != "_"
  133. and x[0][0].upper() == x[0][0]
  134. and callable(x[1]),
  135. obj.__dict__.items(),
  136. )
  137. ),
  138. **dict(
  139. itertools.chain.from_iterable(
  140. [
  141. self.get_sub(y[1], _depth + 1).items()
  142. for y in filter(
  143. lambda x: x[0][0] != "_"
  144. and isinstance(x[1], ModuleType)
  145. and x[1] != obj
  146. and x[1].__package__.rsplit(".", _depth)[0]
  147. == "telethon.tl",
  148. obj.__dict__.items(),
  149. )
  150. ]
  151. )
  152. ),
  153. }