From 38744a0852087b3aa09ed9cb080cb43b057469b9 Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Fri, 12 Apr 2024 22:19:41 +0800 Subject: [PATCH] :sparkles: add plugin `shell` --- plugins/shell/__init__.py | 0 plugins/shell/main.py | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 plugins/shell/__init__.py create mode 100644 plugins/shell/main.py diff --git a/plugins/shell/__init__.py b/plugins/shell/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plugins/shell/main.py b/plugins/shell/main.py new file mode 100644 index 0000000..27b7fc9 --- /dev/null +++ b/plugins/shell/main.py @@ -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) + ) + ) + ) + )