devs.py 5.5 KB

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