mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-12 06:55:49 +08:00
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
from io import BytesIO
|
|
from base64 import b64encode
|
|
from typing import List, Union, Literal
|
|
|
|
from PIL import Image
|
|
|
|
from gsuid_core.models import Message
|
|
|
|
|
|
class MessageSegment:
|
|
def __add__(self, other):
|
|
return [self, other]
|
|
|
|
@staticmethod
|
|
def image(img: Union[str, Image.Image, bytes]) -> Message:
|
|
if isinstance(img, Image.Image):
|
|
img = img.convert('RGB')
|
|
result_buffer = BytesIO()
|
|
img.save(result_buffer, format='PNG', quality=80, subsampling=0)
|
|
img = result_buffer.getvalue()
|
|
elif isinstance(img, bytes):
|
|
pass
|
|
else:
|
|
if img.startswith('base64://'):
|
|
return Message(type='image', data=img)
|
|
with open(img, 'rb') as fp:
|
|
img = fp.read()
|
|
msg = Message(type='image', data=f'base64://{b64encode(img).decode()}')
|
|
return msg
|
|
|
|
@staticmethod
|
|
def text(content: str) -> Message:
|
|
return Message(type='text', data=content)
|
|
|
|
@staticmethod
|
|
def at(user: str) -> Message:
|
|
return Message(type='at', data=user)
|
|
|
|
@staticmethod
|
|
def node(content_list: List[Message]) -> Message:
|
|
return Message(type='node', data=content_list)
|
|
|
|
@staticmethod
|
|
def record(content: str) -> Message:
|
|
return Message(type='record', data=content)
|
|
|
|
@staticmethod
|
|
def log(
|
|
type: Literal['INFO', 'WARNING', 'ERROR', 'SUCCESS'], content: str
|
|
) -> Message:
|
|
return Message(type=f'log_{type}', data=content)
|