From 158ae894f85fb1a590e1071cf78706dc879a7889 Mon Sep 17 00:00:00 2001 From: KimigaiiWuyi <444835641@qq.com> Date: Mon, 23 Oct 2023 01:03:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E`Bot.receive=5Fmu?= =?UTF-8?q?tiply=5Fresp()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gsuid_core/bot.py | 58 ++++++++++++++++++++++++++++++----- gsuid_core/client.py | 4 +-- gsuid_core/handler.py | 15 +++++++-- gsuid_core/plugins/gs_test.py | 12 ++++++++ gsuid_core/sv.py | 3 ++ 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/gsuid_core/bot.py b/gsuid_core/bot.py index c11a112..703ff8a 100644 --- a/gsuid_core/bot.py +++ b/gsuid_core/bot.py @@ -67,25 +67,35 @@ class _Bot: class Bot: instances: Dict[str, "Bot"] = {} + mutiply_instances: Dict[str, "Bot"] = {} + mutiply_map: Dict[str, str] = {} def __init__(self, bot: _Bot, ev: Event): - gid = ev.group_id if ev.group_id else 0 - uid = ev.user_id if ev.user_id else 0 - self.uuid = f'{uid}{gid}' - self.instances[self.uuid] = self + self.gid = ev.group_id if ev.group_id else '0' + self.uid = ev.user_id if ev.user_id else '0' + self.uuid = f'{self.gid}{self.uid}' self.bot = bot self.ev = ev self.logger = self.bot.logger self.bot_id = ev.bot_id self.bot_self_id = ev.bot_self_id - self.event = asyncio.Event() self.resp: List[Event] = [] + self.mutiply_tag = False + self.mutiply_resp: List[Event] = [] @classmethod def get_instances(cls): return cls.instances + @classmethod + def get_mutiply_instances(cls): + return cls.mutiply_instances + + @classmethod + def get_mutiply_map(cls): + return cls.mutiply_map + async def wait_for_key(self, timeout: float) -> Optional[Event]: await asyncio.wait_for(self.event.wait(), timeout=timeout) @@ -98,6 +108,24 @@ class Bot: def set_event(self): self.event.set() + def set_mutiply_event(self): + self.mutiply_event.set() + + async def receive_mutiply_resp( + self, + reply: Optional[ + Union[Message, List[Message], List[str], str, bytes] + ] = None, + option_list: Optional[ + Union[List[str], List[Button], List[List[str]], List[List[Button]]] + ] = None, + unsuported_platform: bool = False, + timeout: float = 60, + ): + return await self.receive_resp( + reply, option_list, unsuported_platform, True, True, timeout + ) + async def send_option( self, reply: Optional[ @@ -109,7 +137,7 @@ class Bot: unsuported_platform: bool = False, ): return await self.receive_resp( - reply, option_list, unsuported_platform, False + reply, option_list, unsuported_platform, False, False ) async def receive_resp( @@ -121,6 +149,7 @@ class Bot: Union[List[str], List[Button], List[List[str]], List[List[Button]]] ] = None, unsuported_platform: bool = False, + is_mutiply: bool = False, is_recive: bool = True, timeout: float = 60, ) -> Optional[Event]: @@ -173,7 +202,22 @@ class Bot: elif reply: await self.send(reply) - if is_recive: + if is_mutiply: + if self.uuid not in self.mutiply_instances: + self.mutiply_instances[self.uuid] = self + if self.gid not in self.mutiply_map: + self.mutiply_map[self.gid] = self.uuid + self.mutiply_tag = True + self.mutiply_event = asyncio.Event() + + while self.mutiply_resp == []: + await asyncio.wait_for(self.mutiply_event.wait(), timeout) + + self.mutiply_event.clear() + return self.mutiply_resp.pop(0) + elif is_recive: + self.instances[self.uuid] = self + self.event = asyncio.Event() return await self.wait_for_key(timeout) async def send( diff --git a/gsuid_core/client.py b/gsuid_core/client.py index 7e2e68c..d0b303e 100644 --- a/gsuid_core/client.py +++ b/gsuid_core/client.py @@ -45,9 +45,9 @@ class GsClient: content = [Message(type='text', data=intent)] msg = MessageReceive( bot_id='Nonebot222', - user_type='direct', + user_type='group', user_pm=1, - group_id=None, + group_id='9999', user_id='511', content=content, ) diff --git a/gsuid_core/handler.py b/gsuid_core/handler.py index dba50a7..b90481f 100644 --- a/gsuid_core/handler.py +++ b/gsuid_core/handler.py @@ -75,10 +75,19 @@ async def handle_event(ws: _Bot, msg: MessageReceive): event = await msg_process(msg) logger.info('[收到事件]', event=event) - gid = event.group_id if event.group_id else 0 - uid = event.user_id if event.user_id else 0 - uuid = f'{uid}{gid}' + gid = event.group_id if event.group_id else '0' + uid = event.user_id if event.user_id else '0' + uuid = f'{gid}{uid}' instances = Bot.get_instances() + mutiply_instances = Bot.get_mutiply_instances() + mutiply_map = Bot.get_mutiply_map() + + if gid in mutiply_map 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: + return + if uuid in instances: instances[uuid].resp.append(event) instances[uuid].set_event() diff --git a/gsuid_core/plugins/gs_test.py b/gsuid_core/plugins/gs_test.py index 0de4d07..67c7c41 100644 --- a/gsuid_core/plugins/gs_test.py +++ b/gsuid_core/plugins/gs_test.py @@ -33,6 +33,18 @@ 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('正在进行[测试多人事件]') + 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('超时了哦!') + + @sv_switch.on_fullmatch('开始游戏') async def get_resp_msg(bot: Bot, ev: Event): await bot.send('正在进行[开始游戏测试]') diff --git a/gsuid_core/sv.py b/gsuid_core/sv.py index 169745b..c873917 100644 --- a/gsuid_core/sv.py +++ b/gsuid_core/sv.py @@ -33,8 +33,11 @@ def modify_func(func): result = await func(bot, event) finally: instancess = Bot.get_instances() + mutiply_instances = Bot.get_mutiply_instances() if bot.uuid in instancess: instancess.pop(bot.uuid) + if bot.uuid in mutiply_instances and bot.mutiply_tag: + mutiply_instances.pop(bot.uuid) return result return wrapper