From 35a5841d332d1a3ac6575d07ff61a19ad5b75db3 Mon Sep 17 00:00:00 2001 From: KimigaiiWuyi <444835641@qq.com> Date: Sun, 3 Dec 2023 01:34:13 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D`Bot.receive?= =?UTF-8?q?=5Fmutiply=5Fresp()`=E5=8F=AF=E8=83=BD=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gsuid_core/client.py | 53 +++++++++++++++++---------- gsuid_core/handler.py | 6 ++- gsuid_core/plugins/gs_test.py | 21 ++++++----- gsuid_core/sv.py | 4 +- gsuid_core/utils/image/image_tools.py | 8 ++-- 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/gsuid_core/client.py b/gsuid_core/client.py index 64a0dd1..9d84b6a 100644 --- a/gsuid_core/client.py +++ b/gsuid_core/client.py @@ -1,3 +1,4 @@ +import random import asyncio from typing import Union @@ -39,25 +40,39 @@ class GsClient: async def send_msg(self): while True: intent = await self._input() - if intent == '图片测试': - content = [ - Message( - type='file', - data='xxx.json|XAclpWfLF5d66dtrHx8cqq8E+', - ) - ] - else: - content = [Message(type='text', data=intent)] - msg = MessageReceive( - bot_id='console', - user_type='direct', - user_pm=1, - group_id=None, - user_id='511', - content=content, - ) - msg_send = msgjson.encode(msg) - await self.ws.send(msg_send) + for content in [ + Message(type='text', data=intent), + Message(type='text', data='开始一场60秒的游戏'), + Message(type='text', data=intent), + Message(type='text', data='233'), + Message(type='text', data='233'), + Message(type='text', data='233'), + Message(type='text', data='233'), + Message(type='text', data='233'), + Message(type='text', data=intent), + Message(type='text', data=intent), + Message(type='text', data=intent), + Message(type='text', data=intent), + Message(type='text', data=intent), + Message(type='text', data=intent), + Message(type='text', data=intent), + ]: + user_type = random.choice(['direct', 'group']) + group_id = random.choice(['555', '666', '777']) + user_id = random.choice(['1', '2', '3', '4', '5']) + if content.data == '开始一场60秒的游戏': + user_type = 'group' + group_id = '555' + msg = MessageReceive( + bot_id='console', + user_type=user_type, # type: ignore + user_pm=1, + group_id=group_id, + user_id=user_id, + content=[content], + ) + msg_send = msgjson.encode(msg) + await self.ws.send(msg_send) async def start(self): recv_task = asyncio.create_task(self.recv_msg()) diff --git a/gsuid_core/handler.py b/gsuid_core/handler.py index 628a3ba..c218ae4 100644 --- a/gsuid_core/handler.py +++ b/gsuid_core/handler.py @@ -83,7 +83,11 @@ async def handle_event(ws: _Bot, msg: MessageReceive): mutiply_instances = Bot.get_mutiply_instances() mutiply_map = Bot.get_mutiply_map() - if gid in mutiply_map and mutiply_map[gid] in mutiply_instances: + if ( + gid in mutiply_map + and event.user_type != 'direct' + and mutiply_map[gid] in mutiply_instances + ): mutiply_instances[mutiply_map[gid]].mutiply_resp.append(event) mutiply_instances[mutiply_map[gid]].set_mutiply_event() if uuid == mutiply_instances[mutiply_map[gid]].uuid: diff --git a/gsuid_core/plugins/gs_test.py b/gsuid_core/plugins/gs_test.py index 1590e24..b4b380a 100644 --- a/gsuid_core/plugins/gs_test.py +++ b/gsuid_core/plugins/gs_test.py @@ -1,5 +1,7 @@ import asyncio +from async_timeout import timeout + from gsuid_core.bot import Bot from gsuid_core.sv import SL, SV from gsuid_core.models import Event @@ -33,16 +35,17 @@ async def get_fullmatch_msg(bot: Bot, ev: Event): await bot.send('[全匹配测试]校验成功!') -@sv_switch.on_fullmatch('测试多人事件') -async def get_event_msg(bot: Bot, ev: Event): - await bot.send('正在进行[测试多人事件]') +@sv_switch.on_fullmatch('开始一场60秒的游戏') +async def get_time_limit_resp_msg(bot: Bot, ev: Event): + await bot.send('接下来开始60秒的游戏!?') try: - while True: - resp = await bot.receive_mutiply_resp() - if resp is not None: - await bot.send(f'{resp.user_id}:发送了 - {resp.text}') - except TimeoutError: - await bot.send('超时了哦!') + async with timeout(60): # 限制时长60秒 + while True: + resp = await bot.receive_mutiply_resp() + if resp is not None: + await bot.send(f'你说的是 {resp.text} 吧?') + except asyncio.TimeoutError: + await bot.send('时间到!!现在开始计算每个人的分数...') @sv_switch.on_fullmatch('开始游戏') diff --git a/gsuid_core/sv.py b/gsuid_core/sv.py index c873917..74cf65b 100644 --- a/gsuid_core/sv.py +++ b/gsuid_core/sv.py @@ -34,10 +34,12 @@ def modify_func(func): finally: instancess = Bot.get_instances() mutiply_instances = Bot.get_mutiply_instances() + mutiply_map = Bot.get_mutiply_map() if bot.uuid in instancess: instancess.pop(bot.uuid) if bot.uuid in mutiply_instances and bot.mutiply_tag: mutiply_instances.pop(bot.uuid) + mutiply_map.pop(bot.gid) return result return wrapper @@ -126,7 +128,7 @@ class SV: ) if name == '测试开关': - self.pm = 0 + self.pm = 6 self.enabled = False def set(self, **kwargs): diff --git a/gsuid_core/utils/image/image_tools.py b/gsuid_core/utils/image/image_tools.py index e3f728f..7a3e2dd 100644 --- a/gsuid_core/utils/image/image_tools.py +++ b/gsuid_core/utils/image/image_tools.py @@ -20,14 +20,14 @@ async def get_event_avatar( ev: Event, avatar_path: Optional[Path] = None ) -> Image.Image: img = None - if 'avatar' in ev.sender and ev.sender['avatar']: + if ev.bot_id == 'onebot' and ev.at: + return await get_qq_avatar(ev.at) + elif 'avatar' in ev.sender and ev.sender['avatar']: avatar_url = ev.sender['avatar'] content = (await sget(avatar_url)).content return Image.open(BytesIO(content)).convert('RGBA') elif ev.bot_id == 'onebot' and not ev.sender: - avatar_url = f'http://q1.qlogo.cn/g?b=qq&nk={ev.user_id}&s=640' - content = (await sget(avatar_url)).content - return Image.open(BytesIO(content)).convert('RGBA') + return await get_qq_avatar(ev.user_id) elif avatar_path: pic_path_list = list(avatar_path.iterdir()) if pic_path_list: