devs.py 5.5 KB

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