Skip to content

Commit

Permalink
Merge pull request #4 from Limnium/main
Browse files Browse the repository at this point in the history
更新了正则的pattern,完善了输入、返回机制
  • Loading branch information
yzyyz1387 authored Jan 19, 2023
2 parents 5e2b0e0 + 6c08688 commit 4696e3e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 80 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish Python 🐍 distributions 📦 to PyPI

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build/
dist/
nonebot_plugin_code.egg-info/
nonebot_plugin_code.egg-info/
.idea
.idea/
54 changes: 29 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
</p>

<div align="center">


**你的star是我的动力**
****
<img src="https://img.shields.io/github/stars/yzyyz1387/nonebot_plugin_code.svg?style=social">

**你的star是我的动力**
****
<img src="https://img.shields.io/github/stars/yzyyz1387/nonebot_plugin_code.svg?style=social">

# 在线运行代码<_ (备考中 可能会更新)

Expand All @@ -20,14 +19,22 @@ _✨ NoneBot2 在线运行代码<_ ✨_
运行于 [https://glot.io/](https://glot.io/)

## 安装💿
`pip install nonebot-plugin-code`

`nb plugin install nonebot-plugin-code`



## 导入📲
**bot.py** 导入,语句:
`nonebot.load_plugin("nonebot_plugin_code")`
## 更新日志

## 更新
0.0.5
[@Limnium](https://github.com/Limnium)
- 更新了正则的pattern(应该能完美按预期匹配)
- 删去命令中的`-i`使其更简洁
- 完善了返回消息
- 以合并转发的方式发送返回消息

0.0.4
- 更新依赖

0.0.3

Expand All @@ -37,33 +44,30 @@ _✨ NoneBot2 在线运行代码<_ ✨_
0.0.2

## 指令💻

```
code [语言] [-i] [inputText]
code [语言] [inputText(空格将被转换为回车)]
[代码]
-i:可选 输入 后跟输入内容
运行代码示例(python)(无输入):
code py
print("sb")
运行代码示例(python)(有输入):
code py -i 你好
code py 你好
print(input())
```


**给个star吧~**

其他插件
[简易群管](https://github.com/yzyyz1387/nonebot_plugin_admin)
[在线运行代码](https://github.com/yzyyz1387/nonebot_plugin_code)
[it咨讯(垃圾插件)](https://github.com/yzyyz1387/nonebot_plugin_itnews "it资讯")
[工作性价比(还没更新beta不能用)](https://github.com/yzyyz1387/nonebot_plugin_workscore)
[黑丝插件(jsdelivr问题国内服务器不能用)](https://github.com/yzyyz1387/nonebot_plugin_heisi)

## 截图🖼

![](https://cdn.jsdelivr.net/gh/yzyyz1387/blogimages/nonebot_plugin_code.png)
`尚为旧版本截图`
![](code.png)

**给个star吧~**

其他插件
[(不)简易群管](https://github.com/yzyyz1387/nonebot_plugin_admin)
[在线运行代码](https://github.com/yzyyz1387/nonebot_plugin_code)
[工作性价比](https://github.com/yzyyz1387/nonebot_plugin_workscore)
[黑丝插件(停止维护)](https://github.com/yzyyz1387/nonebot_plugin_heisi)


Binary file added code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 17 additions & 13 deletions nonebot_plugin_code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,38 @@
# @Email : [email protected]
# @File : __init__.py.py
# @Software: PyCharm
from nonebot import on_command
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, Message

# @Time : 2023/01/19 21:00
# @UpdateBy: Limnium
# 更新了正则的pattern,完善了返回机制,“优化”代码风格。
from nonebot import on_command
from nonebot.params import CommandArg
from nonebot.adapters.onebot.v11 import MessageEvent, Message, Bot, GroupMessageEvent
from .run import run

runcode = on_command('code', priority=5)


@runcode.handle()
async def _(bot: Bot, event: MessageEvent):
code = str(event.get_message()).strip()
async def runcode_body(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
code = str(arg).strip()
res = await run(code)
await runcode.send(message=Message(res), at_sender=True)
messages = {"type": "node", "data": {"name": "return", "uin": bot.self_id, "content": Message(res)}}
if isinstance(event, GroupMessageEvent):
return await bot.call_api("send_group_forward_msg", group_id=event.group_id, messages=messages)
else:
return await bot.call_api("send_private_forward_msg", user_id=event.user_id, messages=messages)


__usage__ = """
发送
code [语言] [-i] [inputText]
code [语言] [stdin(空格将被替换为回车)]
[代码]
-i:可选 输入 后跟输入内容
运行代码示例(python)(无输入):
code py
print("sb")
运行代码示例(python)(有输入):
code py -i 你好
print(input())
运行代码示例(python):
code py 你好
print(input())
目前仅支持c/cpp/c#/py/php/go/java/js
运行于:https://glot.io/
Expand Down
63 changes: 23 additions & 40 deletions nonebot_plugin_code/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
# @Email : [email protected]
# @File : run.py
# @Software: PyCharm
import re

# @Time : 2023/01/19 21:00
# @UpdateBy: Limnium
# 更新了正则的pattern,完善了返回机制,“优化”代码风格。
import re
import httpx

codeType = {
Expand All @@ -25,46 +28,26 @@
async def run(strcode):
strcode = strcode.replace('&amp;', '&').replace('&#91;', '[').replace('&#93;', ']')
try:
a = re.findall(r'(py|php|java|cpp|js|c#|c|go|asm)\s?(-i)?\s?(\w*)?(\n|\r)((?:.|\n)+)', strcode)[0]
print(a)
a = re.match(r'(py|php|java|cpp|js|c#|c|go|asm)\b ?(.*)\n((?:.|\n)+)', strcode)
lang, stdin, code = a.group(1), a.group(2).replace(' ', '\n'), a.group(3)
except:
return "输入有误汪\n目前仅支持c/cpp/c#/py/php/go/java/js"
if "-i" in strcode:
lang, code = a[0], a[4]
dataJson = {
"files": [
{
"name": f"main.{codeType[lang][1]}",
"content": code
}
],
"stdin": a[2],
"command": ""
}
else:
lang, code = a[0], a[4]
dataJson = {
"files": [
{
"name": f"main.{codeType[lang][1]}",
"content": code
}
],
"stdin": "",
"command": ""
}
headers = {
"Authorization": "Token 0123456-789a-bcde-f012-3456789abcde",
"content-type": "application/"
return "输入有误,目前仅支持c/cpp/c#/py/php/go/java/js"
dataJson = {
"files": [
{
"name": f"main.{codeType[lang][1]}",
"content": code
}
],
"stdin": stdin,
"command": ""
}
headers = {"Authorization": "Token 0123456-789a-bcde-f012-3456789abcde",
"content-type": "application/"}
async with httpx.AsyncClient() as client:
res = (await client.post(url=f'https://glot.io/run/{codeType[lang][0]}?version=latest', headers=headers, json=dataJson))
print(dataJson)
res = await client.post(url=f'https://glot.io/run/{codeType[lang][0]}?version=latest', headers=headers, json=dataJson)
if res.status_code == 200:
if res.json()['stdout'] != "":
if len(repr(res.json()['stdout'])) < 100:
return res.json()['stdout']
else:
return "返回字符过长呐~~~"
else:
return res.json()['stderr'].strip()
res = res.json()
return res['stdout']+('\n---\n'+res['stderr'] if res['stderr'] else '')
else:
return '响应异常'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="nonebot_plugin_code",
version="0.0.4",
version="0.0.5",
author="yzyyz1387",
author_email="[email protected]",
keywords=("pip", "nonebot2", "nonebot", "nonebot_plugin"),
Expand Down

0 comments on commit 4696e3e

Please sign in to comment.