icalingua-python-bot/ica-rs/plugins/base.py

72 lines
2.7 KiB
Python
Raw Normal View History

2024-06-28 23:19:10 +08:00
import io
2024-06-29 00:56:40 +08:00
import psutil
import platform
from typing import TYPE_CHECKING, TypeVar
from PIL import (Image, ImageDraw, ImageFont)
2024-06-10 16:46:07 +08:00
if TYPE_CHECKING:
from ica_typing import IcaNewMessage, IcaClient
from ica_typing import TailchatReciveMessage, TailchatClient
else:
IcaNewMessage = TypeVar("NewMessage")
IcaClient = TypeVar("IcaClient")
TailchatReciveMessage = TypeVar("TailchatReciveMessage")
TailchatClient = TypeVar("TailchatClient")
2024-02-22 12:47:20 +08:00
2024-06-29 00:56:40 +08:00
# 生成一张本地信息图
def local_env_info() -> str:
cache = io.StringIO()
# 参考 DR 的 (crash report)
cache.write(f"系统: {platform.platform()}\n")
# 处理器
2024-06-29 01:04:25 +08:00
try:
cache.write("|".join([f"{x}%" for x in psutil.cpu_percent(interval=1, percpu=True)]))
cache.write("\n")
except OSError:
cache.write("CPU: 未知\n")
2024-06-29 00:56:40 +08:00
# Python 版本信息
cache.write(f"{platform.python_implementation()}: {platform.python_version()}-{platform.python_branch()}({platform.python_compiler()})\n")
# 内存信息
2024-06-29 01:03:44 +08:00
try:
memory = psutil.virtual_memory()
cache.write(f"内存: {memory.free / 1024 / 1024 / 1024:.3f}GB/{memory.total / 1024 / 1024 / 1024:.3f}GB\n")
except OSError:
cache.write("内存: 未知\n")
2024-06-29 00:56:40 +08:00
return cache.getvalue()
def local_env_image() -> bytes:
2024-06-29 01:35:44 +08:00
img = Image.new("RGB", (800, 140), (255, 255, 255))
2024-06-29 00:56:40 +08:00
# 往图片上写入一些信息
draw = ImageDraw.Draw(img)
2024-06-29 01:31:12 +08:00
font = ImageFont.truetype("./SMILEYSANS-OBLIQUE.TTF", size=25)
2024-06-29 00:56:40 +08:00
draw.text((10, 10), local_env_info(), fill=(0, 0, 0), font=font)
img_cache = io.BytesIO()
img.save(img_cache, format="PNG")
raw_img = img_cache.getvalue()
img_cache.close()
return raw_img
2024-03-16 16:58:18 +08:00
def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None:
2024-02-22 14:38:37 +08:00
if not (msg.is_from_self or msg.is_reply):
if msg.content == "/bot":
2024-03-16 00:15:49 +08:00
reply = msg.reply_with(f"ica-async-rs({client.version})-sync-py {client.ica_version}")
2024-02-22 12:47:20 +08:00
client.send_message(reply)
2024-06-29 00:56:40 +08:00
elif msg.content == "/bot-sys":
datas = local_env_info()
reply = msg.reply_with(datas)
reply.set_img(local_env_image(), "image/png", False)
client.send_message(reply)
2024-06-04 23:48:01 +08:00
2024-06-05 00:03:19 +08:00
def on_tailchat_message(msg: TailchatReciveMessage, client: TailchatClient) -> None:
2024-06-29 00:56:40 +08:00
if not (msg.is_reply or msg.is_from_self):
2024-06-05 00:03:19 +08:00
if msg.content == "/bot":
2024-06-05 21:52:15 +08:00
reply = msg.reply_with(f"tailchat-async-rs({client.version})-sync-py {client.tailchat_version}")
2024-06-05 00:03:19 +08:00
client.send_message(reply)
2024-06-29 00:56:40 +08:00
elif msg.content == "/bot-sys":
datas = local_env_info()
reply = msg.reply_with(datas)
reply.set_img(local_env_image(), "just_img.png")
2024-06-28 23:19:10 +08:00
client.send_message(reply)