PagerMaid-Pyro/pagermaid/common/system.py

64 lines
1.6 KiB
Python
Raw Normal View History

2023-01-31 16:24:56 +00:00
import io
import sys
import traceback
2024-02-05 13:16:25 +00:00
from typing import Optional
2023-01-31 16:24:56 +00:00
from pagermaid import bot
2024-02-05 13:16:25 +00:00
from pagermaid.services import client as httpx_client
2023-01-31 16:24:56 +00:00
2024-02-05 13:16:25 +00:00
async def run_eval(cmd: str, message=None) -> str:
2023-01-31 16:24:56 +00:00
old_stderr = sys.stderr
old_stdout = sys.stdout
redirected_output = sys.stdout = io.StringIO()
redirected_error = sys.stderr = io.StringIO()
stdout, stderr, exc = None, None, None
try:
await aexec(cmd, message, bot)
except Exception: # noqa
exc = traceback.format_exc()
stdout = redirected_output.getvalue()
stderr = redirected_error.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
if exc:
evaluation = exc
elif stderr:
evaluation = stderr
elif stdout:
evaluation = stdout
else:
evaluation = "Success"
2024-02-05 13:16:25 +00:00
return evaluation
2023-01-31 16:24:56 +00:00
async def aexec(code, event, client):
exec(
(
2023-03-12 03:56:01 +00:00
(
("async def __aexec(e, client): " + "\n msg = message = e")
+ "\n reply = message.reply_to_message if message else None"
)
+ "\n chat = e.chat if e else None"
2023-01-31 16:24:56 +00:00
)
+ "".join(f"\n {x}" for x in code.split("\n"))
)
return await locals()["__aexec"](event, client)
2024-02-05 13:16:25 +00:00
async def paste_pb(
content: str, private: bool = True, sunset: int = 3600
) -> Optional[str]:
data = {
"c": content,
}
if private:
data["p"] = "1"
if sunset:
data["sunset"] = sunset
result = await httpx_client.post("https://fars.ee", data=data)
if result.is_error:
return None
return result.headers.get("location")