PagerMaid_Plugins_Pyro/code/main.py

69 lines
2.0 KiB
Python
Raw Normal View History

# python3
# -*- coding: utf-8 -*-
# @Time : 2021/11/22 14:17
# @Author : yzyyz
# @Email : youzyyz1384@qq.com
# @File : run.py
# @Software: PyCharm
import re
import httpx
from pagermaid.listener import listener
from pagermaid.enums import Message, AsyncClient
codeType = {
2023-07-01 12:18:58 +00:00
"py": ["python", "py"],
"cpp": ["cpp", "cpp"],
"java": ["java", "java"],
"php": ["php", "php"],
"js": ["javascript", "js"],
"c": ["c", "c"],
"c#": ["csharp", "cs"],
"go": ["go", "go"],
"asm": ["assembly", "asm"],
}
async def run(string: str, client: AsyncClient):
2023-07-01 12:18:58 +00:00
string = string.replace("&", "&").replace("[", "[").replace("]", "]")
try:
2023-07-01 12:18:58 +00:00
a = re.findall(
r"(py|php|java|cpp|js|c#|c|go|asm)\s?(-i)?\s?(\w*)?(\n|\r)((?:.|\n)+)",
string,
)[0]
print(a)
except Exception:
return "输入有误汪\n目前仅支持c/cpp/c#/py/php/go/java/js"
lang, code_str = a[0], a[4]
if "-i" in string:
data_json = {
2023-07-01 12:18:58 +00:00
"files": [{"name": f"main.{codeType[lang][1]}", "content": code_str}],
"stdin": a[2],
2023-07-01 12:18:58 +00:00
"command": "",
}
else:
data_json = {
2023-07-01 12:18:58 +00:00
"files": [{"name": f"main.{codeType[lang][1]}", "content": code_str}],
"stdin": "",
2023-07-01 12:18:58 +00:00
"command": "",
}
headers = {
"Authorization": "Token 0123456-789a-bcde-f012-3456789abcde",
2023-07-01 12:18:58 +00:00
"content-type": "application/",
}
res = await client.post(
2023-07-01 12:18:58 +00:00
url=f"https://glot.io/run/{codeType[lang][0]}?version=latest",
headers=headers,
2023-07-01 12:18:58 +00:00
json=data_json,
)
if res.status_code != 200:
return "请求失败了呐~~~"
2023-07-01 12:18:58 +00:00
if res.json()["stdout"] == "":
return res.json()["stderr"].strip()
return f"<b>>>></b> <code>{code_str}</code> \n{res.json()['stdout']}"
@listener(command="code", description="运行代码", parameters="[语言] [-i] [inputText]\n[代码]")
async def code(message: Message, client: AsyncClient):
await message.edit(await run(message.arguments, client))