devs.py 4.8 KB

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