devs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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__ = "Devs"
  10. __HELP__ = "/eval - Execute Python Code\n/sh - Execute Shell Code"
  11. # Eval and Sh 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)
  69. @app.on_message(
  70. filters.user(OWNER_ID)
  71. & ~filters.forwarded
  72. & ~filters.via_bot
  73. & filters.command('sh'),
  74. )
  75. async def shellrunner(client, message):
  76. if len(message.command) < 2:
  77. await message.reply_text(message, text='**Usage:**\n/sh git pull')
  78. return
  79. text = message.text.split(None, 1)[1]
  80. if '\n' in text:
  81. code = text.split('\n')
  82. output = ''
  83. for x in code:
  84. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  85. try:
  86. process = subprocess.Popen(
  87. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  88. )
  89. except Exception as err:
  90. print(err)
  91. await message.reply_text(
  92. message,
  93. text=f"**Error:**\n```{err}```")
  94. output += f'**{code}**\n'
  95. output += process.stdout.read()[:-1].decode('utf-8')
  96. output += '\n'
  97. else:
  98. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  99. for a in range(len(shell)):
  100. shell[a] = shell[a].replace('"', '')
  101. try:
  102. process = subprocess.Popen(
  103. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  104. )
  105. except Exception as err:
  106. print(err)
  107. exc_type, exc_obj, exc_tb = sys.exc_info()
  108. errors = traceback.format_exception(
  109. etype=exc_type, value=exc_obj, tb=exc_tb,
  110. )
  111. await message.reply_text(
  112. message,
  113. text="**Error:**\n```{''.join(errors)}```")
  114. return
  115. output = process.stdout.read()[:-1].decode('utf-8')
  116. if str(output) == '\n':
  117. output = None
  118. if output:
  119. if len(output) > 4096:
  120. with open('output.txt', 'w+') as file:
  121. file.write(output)
  122. await app.send_document(
  123. message.chat.id,
  124. 'output.txt',
  125. reply_to_message_id=message.message_id,
  126. caption='`Output`',
  127. )
  128. os.remove('output.txt')
  129. return
  130. await message.reply_text(
  131. message,
  132. text="**Output:**\n```{output}```"
  133. )
  134. else:
  135. await message.reply_text(
  136. message,
  137. text='**Output: **\n`No output`')