eval.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import sys
  2. import traceback
  3. from io import StringIO
  4. import os
  5. from inspect import getfullargspec
  6. from wbb import app, OWNER_ID
  7. from pyrogram import filters
  8. from pyrogram.types import Message
  9. __MODULE__ = "Eval"
  10. __HELP__ = "/eval - You know what this does lol"
  11. # Eval module from nana-remix
  12. async def aexec(code, client, message):
  13. exec(
  14. "async def __aexec(client, message): "
  15. + "".join(f"\n {a}" for a in code.split("\n"))
  16. )
  17. return await locals()["__aexec"](client, message)
  18. async def edit_or_reply(msg: Message, **kwargs):
  19. func = msg.edit_text if msg.from_user.is_self else msg.reply
  20. spec = getfullargspec(func.__wrapped__).args
  21. await func(**{k: v for k, v in kwargs.items() if k in spec})
  22. @app.on_message(filters.user(OWNER_ID) & ~filters.forwarded & ~filters.via_bot & filters.command("eval"))
  23. async def executor(client, message):
  24. try:
  25. cmd = message.text.split(" ", maxsplit=1)[1]
  26. except IndexError:
  27. await message.delete()
  28. return
  29. reply_to_id = message.message_id
  30. if message.reply_to_message:
  31. reply_to_id = message.reply_to_message.message_id
  32. old_stderr = sys.stderr
  33. old_stdout = sys.stdout
  34. redirected_output = sys.stdout = StringIO()
  35. redirected_error = sys.stderr = StringIO()
  36. stdout, stderr, exc = None, None, None
  37. try:
  38. await aexec(cmd, client, message)
  39. except Exception:
  40. exc = traceback.format_exc()
  41. stdout = redirected_output.getvalue()
  42. stderr = redirected_error.getvalue()
  43. sys.stdout = old_stdout
  44. sys.stderr = old_stderr
  45. evaluation = ""
  46. if exc:
  47. evaluation = exc
  48. elif stderr:
  49. evaluation = stderr
  50. elif stdout:
  51. evaluation = stdout
  52. else:
  53. evaluation = "Success"
  54. final_output = (f"**OUTPUT**:\n```{evaluation.strip()}```")
  55. if len(final_output) > 4096:
  56. filename = "output.txt"
  57. with open(filename, "w+", encoding="utf8") as out_file:
  58. out_file.write(str(evaluation.strip()))
  59. await message.reply_document(
  60. document=filename,
  61. caption=cmd,
  62. disable_notification=True,
  63. reply_to_message_id=reply_to_id,
  64. )
  65. os.remove(filename)
  66. await message.delete()
  67. else:
  68. await edit_or_reply(message, text=final_output)