-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.py
47 lines (36 loc) · 1.52 KB
/
terminal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
# Coded by @maxunof with power of Senko!
import asyncio
import chardet
from .. import sdk
class Module(sdk.Module):
def __init__(self):
self.name: str = "Terminal"
def decode(self, data: bytes) -> str:
encoding = chardet.detect(data)["encoding"]
result = "No data received"
if encoding is not None:
result = data.decode(encoding).strip()
return result
async def run(self, event: sdk.Event, command: str):
sh = await asyncio.create_subprocess_shell(
command, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
await sdk.send(event.message, "<b>Process is running...</b>")
await sh.wait()
await sdk.send(
event.message,
"<b>Command: </b><code>{}</code>\n<b>Status code: </b><code>{}</code>\n\n<b>Output:</b>\n<code>{}</code>\n\n<b>Errors:</b>\n<code>{}</code>".format(
sdk.escape_html(command), sh.returncode,
sdk.escape_html(self.decode(await sh.stdout.read())),
sdk.escape_html(self.decode(await sh.stderr.read()))
),
)
async def neofetch_cmd(self, event: sdk.Event, command: sdk.Command):
await self.run(event, "neofetch --stdout")
async def terminal_cmd(self, event: sdk.Event, command: sdk.Command):
if command.arg == "":
await sdk.send(event.message, "<b>You need to specify command.</b>")
return
await self.run(event, command.arg)