devs.py 5.2 KB

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