-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d65c2d
commit 38744a0
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
) | ||
) | ||
) |