Skip to content

Commit

Permalink
✨ add plugin shell
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Apr 12, 2024
1 parent 9d65c2d commit 38744a0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Empty file added plugins/shell/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions plugins/shell/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import asyncio

from avilla.core import MessageReceived, Context, MessageChain, Picture, RawResource
from graia.saya.builtins.broadcast.shortcut import listen

from arclet.alconna.graia import startswith

from app.image import md2img
from app.shortcut import permission, exclusive, accessable


@listen(MessageReceived)
@permission("master")
@startswith("shell", bind="echos")
@exclusive
@accessable
async def shell(ctx: Context, echos: MessageChain):
process = await asyncio.create_subprocess_shell(
str(echos),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
stdout, stderr = await asyncio.wait_for(process.communicate(), 20)
except asyncio.TimeoutError:
process.kill()
stdout, stderr = await process.communicate()
if stdout:
try:
res = stdout.decode("utf-8").strip()
except UnicodeDecodeError:
res = stdout.decode("gbk").strip()
elif stderr:
try:
res = stderr.decode("utf-8").strip()
except UnicodeDecodeError:
res = stderr.decode("gbk").strip()
else:
res = "No output"
md = f"""\
> exit code: {process.returncode}
```sh
{res}
```
"""
return await ctx.scene.send_message(
Picture(
RawResource(
await md2img(
md,
max(max(len(i.strip()) for i in md.splitlines()) * 14, 240)
)
)
)
)

0 comments on commit 38744a0

Please sign in to comment.