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