新增Bot.receive_mutiply_resp()

This commit is contained in:
KimigaiiWuyi 2023-10-23 01:03:32 +08:00
parent 679de1fc5e
commit 158ae894f8
5 changed files with 80 additions and 12 deletions

View File

@ -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(

View File

@ -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,
)

View File

@ -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()

View File

@ -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('正在进行[开始游戏测试]')

View File

@ -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