devs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import sys
  2. import traceback
  3. from io import StringIO
  4. import os
  5. import re
  6. import subprocess
  7. from inspect import getfullargspec
  8. from wbb import app, OWNER_ID
  9. from pyrogram import filters
  10. from pyrogram.types import Message
  11. __MODULE__ = "Devs"
  12. __HELP__ = "/eval - Execute Python Code\n/sh - Execute Shell Code"
  13. # Eval and Sh module from nana-remix
  14. async def aexec(code, client, message):
  15. exec(
  16. "async def __aexec(client, message): "
  17. + "".join(f"\n {a}" for a in code.split("\n"))
  18. )
  19. return await locals()["__aexec"](client, message)
  20. async def edit_or_reply(msg: Message, **kwargs):
  21. func = msg.edit_text if msg.from_user.is_self else msg.reply
  22. spec = getfullargspec(func.__wrapped__).args
  23. await func(**{k: v for k, v in kwargs.items() if k in spec})
  24. @app.on_message(filters.user(OWNER_ID) & ~filters.forwarded & ~filters.via_bot & filters.command("eval"))
  25. async def executor(client, message):
  26. try:
  27. cmd = message.text.split(" ", maxsplit=1)[1]
  28. except IndexError:
  29. await message.delete()
  30. return
  31. reply_to_id = message.message_id
  32. if message.reply_to_message:
  33. reply_to_id = message.reply_to_message.message_id
  34. old_stderr = sys.stderr
  35. old_stdout = sys.stdout
  36. redirected_output = sys.stdout = StringIO()
  37. redirected_error = sys.stderr = StringIO()
  38. stdout, stderr, exc = None, None, None
  39. try:
  40. await aexec(cmd, client, message)
  41. except Exception:
  42. exc = traceback.format_exc()
  43. stdout = redirected_output.getvalue()
  44. stderr = redirected_error.getvalue()
  45. sys.stdout = old_stdout
  46. sys.stderr = old_stderr
  47. evaluation = ""
  48. if exc:
  49. evaluation = exc
  50. elif stderr:
  51. evaluation = stderr
  52. elif stdout:
  53. evaluation = stdout
  54. else:
  55. evaluation = "Success"
  56. final_output = (f"**OUTPUT**:\n```{evaluation.strip()}```")
  57. if len(final_output) > 4096:
  58. filename = "output.txt"
  59. with open(filename, "w+", encoding="utf8") as out_file:
  60. out_file.write(str(evaluation.strip()))
  61. await message.reply_document(
  62. document=filename,
  63. caption=cmd,
  64. disable_notification=True,
  65. reply_to_message_id=reply_to_id,
  66. )
  67. os.remove(filename)
  68. await message.delete()
  69. else:
  70. await edit_or_reply(message, text=final_output)
  71. @app.on_message(
  72. filters.user(OWNER_ID)
  73. & ~filters.forwarded
  74. & ~filters.via_bot
  75. & filters.command('sh'),
  76. )
  77. async def shellrunner(client, message):
  78. if len(message.command) < 2:
  79. await edit_or_reply(message, text='**Usage:**\n/sh git pull')
  80. return
  81. text = message.text.split(None, 1)[1]
  82. if '\n' in text:
  83. code = text.split('\n')
  84. output = ''
  85. for x in code:
  86. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  87. try:
  88. process = subprocess.Popen(
  89. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  90. )
  91. except Exception as err:
  92. print(err)
  93. await edit_or_reply(
  94. message,
  95. text=f"**Error:**\n```{err}```")
  96. output += f'**{code}**\n'
  97. output += process.stdout.read()[:-1].decode('utf-8')
  98. output += '\n'
  99. else:
  100. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  101. for a in range(len(shell)):
  102. shell[a] = shell[a].replace('"', '')
  103. try:
  104. process = subprocess.Popen(
  105. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  106. )
  107. except Exception as err:
  108. print(err)
  109. exc_type, exc_obj, exc_tb = sys.exc_info()
  110. errors = traceback.format_exception(
  111. etype=exc_type, value=exc_obj, tb=exc_tb,
  112. )
  113. await edit_or_reply(
  114. message,
  115. text=f"**Error:**\n```{''.join(errors)}```")
  116. return
  117. output = process.stdout.read()[:-1].decode('utf-8')
  118. if str(output) == '\n':
  119. output = None
  120. if output:
  121. if len(output) > 4096:
  122. with open('output.txt', 'w+') as file:
  123. file.write(output)
  124. await app.send_document(
  125. message.chat.id,
  126. 'output.txt',
  127. reply_to_message_id=message.message_id,
  128. caption='`Output`',
  129. )
  130. os.remove('output.txt')
  131. return
  132. await edit_or_reply(
  133. message,
  134. text=f"**Output:**\n```{output}```"
  135. )
  136. else:
  137. await edit_or_reply(
  138. message,
  139. text='**Output: **\n`No output`')