mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-12 06:55:49 +08:00
✨ 新增MessageSegment.markdown
, 调整Bot.receive_resp()
, 适配QQGroup
This commit is contained in:
parent
9f41cf0ea5
commit
6f04bc2180
@ -7,6 +7,7 @@ from msgspec import json as msgjson
|
||||
|
||||
from gsuid_core.logger import logger
|
||||
from gsuid_core.gs_logger import GsLogger
|
||||
from gsuid_core.message_models import Button
|
||||
from gsuid_core.segment import MessageSegment
|
||||
from gsuid_core.utils.image.convert import text2pic
|
||||
from gsuid_core.models import Event, Message, MessageSend
|
||||
@ -127,7 +128,38 @@ class Bot:
|
||||
def set_event(self):
|
||||
self.event.set()
|
||||
|
||||
async def receive_resp(self, timeout: float = 60) -> Optional[Event]:
|
||||
async def receive_resp(
|
||||
self,
|
||||
reply_text: Optional[str] = None,
|
||||
option_list: Optional[List[Union[str, Button]]] = None,
|
||||
timeout: float = 60,
|
||||
) -> Optional[Event]:
|
||||
if option_list:
|
||||
if reply_text is None:
|
||||
reply_text = '请在60秒内做出选择...'
|
||||
|
||||
if self.ev.real_bot_id in ['qqguild', 'qqgroup']:
|
||||
_buttons: List[Button] = []
|
||||
for option in option_list:
|
||||
if isinstance(option, Button):
|
||||
_buttons.append(option)
|
||||
else:
|
||||
_buttons.append(Button(option, option, option))
|
||||
await self.send(MessageSegment.markdown(reply_text, _buttons))
|
||||
else:
|
||||
_options: List[str] = []
|
||||
for option in option_list:
|
||||
if isinstance(option, Button):
|
||||
_options.append(option.text)
|
||||
else:
|
||||
_options.append(option)
|
||||
|
||||
reply_text += '/'.join(_options)
|
||||
await self.send(reply_text)
|
||||
|
||||
elif reply_text:
|
||||
await self.send(reply_text)
|
||||
|
||||
return await self.wait_for_key(timeout)
|
||||
|
||||
async def send(
|
||||
|
15
gsuid_core/message_models.py
Normal file
15
gsuid_core/message_models.py
Normal file
@ -0,0 +1,15 @@
|
||||
from typing import List, Literal, Optional
|
||||
|
||||
from msgspec import Struct
|
||||
|
||||
|
||||
class Button(Struct):
|
||||
text: str
|
||||
data: Optional[str] # 具体数据
|
||||
pressed_text: Optional[str] # 按下之后显示的值
|
||||
style: Literal[0, 1] = 1 # 0灰色线框,1蓝色线框
|
||||
action: Literal[0, 1, 2] = 2 # 0跳转按钮,1回调按钮,2命令按钮
|
||||
permisson: Literal[0, 1, 2, 3] = 2 # 0指定用户,1管理者,2所有人可按,3指定身份组
|
||||
specify_role_ids: List[str] = [] # 仅限频道可用
|
||||
specify_user_ids: List[str] = [] # 指定用户
|
||||
unsupport_tips: str = '您的客户端暂不支持该功能, 请升级后适配...'
|
@ -31,8 +31,17 @@ async def get_fullmatch_msg(bot: Bot, ev: Event):
|
||||
await bot.send('正在进行[全匹配测试]')
|
||||
await asyncio.sleep(2)
|
||||
await bot.send('[全匹配测试]校验成功!')
|
||||
await bot.send('请输入一个数字')
|
||||
resp = await bot.receive_resp()
|
||||
|
||||
|
||||
@sv_switch.on_fullmatch('开始游戏')
|
||||
async def get_resp_msg(bot: Bot, ev: Event):
|
||||
await bot.send('正在进行[开始游戏测试]')
|
||||
await asyncio.sleep(2)
|
||||
await bot.send('[开始游戏测试]校验成功!')
|
||||
resp = await bot.receive_resp(
|
||||
'请选择一个选项!',
|
||||
['🎨可爱的丛林', '🚀遥远的星空', '📝不如在家写作业', '✨或者看星星', '🚧这里是维护选项'],
|
||||
)
|
||||
if resp is not None:
|
||||
await bot.send(f'你输入的是{resp.text}')
|
||||
|
||||
|
@ -4,10 +4,12 @@ from pathlib import Path
|
||||
from base64 import b64encode
|
||||
from typing import List, Union, Literal
|
||||
|
||||
import msgspec
|
||||
from PIL import Image
|
||||
|
||||
from gsuid_core.models import Message
|
||||
from gsuid_core.data_store import image_res
|
||||
from gsuid_core.message_models import Button
|
||||
from gsuid_core.utils.plugins_config.gs_config import core_plugins_config
|
||||
|
||||
enable_pic_srv = core_plugins_config.get_config('EnablePicSrv').data
|
||||
@ -53,6 +55,13 @@ class MessageSegment:
|
||||
def text(content: str) -> Message:
|
||||
return Message(type='text', data=content)
|
||||
|
||||
@staticmethod
|
||||
def markdown(content: str, buttons: List[Button]) -> List[Message]:
|
||||
return [
|
||||
Message(type='markdown', data=content),
|
||||
Message(type='buttons', data=msgspec.to_builtins(buttons)),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def at(user: str) -> Message:
|
||||
return Message(type='at', data=user)
|
||||
|
Loading…
x
Reference in New Issue
Block a user