mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-12 06:55:49 +08:00
✨ 新增Bot.receive_mutiply_resp()
This commit is contained in:
parent
679de1fc5e
commit
158ae894f8
@ -67,25 +67,35 @@ class _Bot:
|
|||||||
|
|
||||||
class Bot:
|
class Bot:
|
||||||
instances: Dict[str, "Bot"] = {}
|
instances: Dict[str, "Bot"] = {}
|
||||||
|
mutiply_instances: Dict[str, "Bot"] = {}
|
||||||
|
mutiply_map: Dict[str, str] = {}
|
||||||
|
|
||||||
def __init__(self, bot: _Bot, ev: Event):
|
def __init__(self, bot: _Bot, ev: Event):
|
||||||
gid = ev.group_id if ev.group_id else 0
|
self.gid = ev.group_id if ev.group_id else '0'
|
||||||
uid = ev.user_id if ev.user_id else 0
|
self.uid = ev.user_id if ev.user_id else '0'
|
||||||
self.uuid = f'{uid}{gid}'
|
self.uuid = f'{self.gid}{self.uid}'
|
||||||
self.instances[self.uuid] = self
|
|
||||||
|
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.ev = ev
|
self.ev = ev
|
||||||
self.logger = self.bot.logger
|
self.logger = self.bot.logger
|
||||||
self.bot_id = ev.bot_id
|
self.bot_id = ev.bot_id
|
||||||
self.bot_self_id = ev.bot_self_id
|
self.bot_self_id = ev.bot_self_id
|
||||||
self.event = asyncio.Event()
|
|
||||||
self.resp: List[Event] = []
|
self.resp: List[Event] = []
|
||||||
|
self.mutiply_tag = False
|
||||||
|
self.mutiply_resp: List[Event] = []
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_instances(cls):
|
def get_instances(cls):
|
||||||
return cls.instances
|
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]:
|
async def wait_for_key(self, timeout: float) -> Optional[Event]:
|
||||||
await asyncio.wait_for(self.event.wait(), timeout=timeout)
|
await asyncio.wait_for(self.event.wait(), timeout=timeout)
|
||||||
|
|
||||||
@ -98,6 +108,24 @@ class Bot:
|
|||||||
def set_event(self):
|
def set_event(self):
|
||||||
self.event.set()
|
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(
|
async def send_option(
|
||||||
self,
|
self,
|
||||||
reply: Optional[
|
reply: Optional[
|
||||||
@ -109,7 +137,7 @@ class Bot:
|
|||||||
unsuported_platform: bool = False,
|
unsuported_platform: bool = False,
|
||||||
):
|
):
|
||||||
return await self.receive_resp(
|
return await self.receive_resp(
|
||||||
reply, option_list, unsuported_platform, False
|
reply, option_list, unsuported_platform, False, False
|
||||||
)
|
)
|
||||||
|
|
||||||
async def receive_resp(
|
async def receive_resp(
|
||||||
@ -121,6 +149,7 @@ class Bot:
|
|||||||
Union[List[str], List[Button], List[List[str]], List[List[Button]]]
|
Union[List[str], List[Button], List[List[str]], List[List[Button]]]
|
||||||
] = None,
|
] = None,
|
||||||
unsuported_platform: bool = False,
|
unsuported_platform: bool = False,
|
||||||
|
is_mutiply: bool = False,
|
||||||
is_recive: bool = True,
|
is_recive: bool = True,
|
||||||
timeout: float = 60,
|
timeout: float = 60,
|
||||||
) -> Optional[Event]:
|
) -> Optional[Event]:
|
||||||
@ -173,7 +202,22 @@ class Bot:
|
|||||||
elif reply:
|
elif reply:
|
||||||
await self.send(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)
|
return await self.wait_for_key(timeout)
|
||||||
|
|
||||||
async def send(
|
async def send(
|
||||||
|
@ -45,9 +45,9 @@ class GsClient:
|
|||||||
content = [Message(type='text', data=intent)]
|
content = [Message(type='text', data=intent)]
|
||||||
msg = MessageReceive(
|
msg = MessageReceive(
|
||||||
bot_id='Nonebot222',
|
bot_id='Nonebot222',
|
||||||
user_type='direct',
|
user_type='group',
|
||||||
user_pm=1,
|
user_pm=1,
|
||||||
group_id=None,
|
group_id='9999',
|
||||||
user_id='511',
|
user_id='511',
|
||||||
content=content,
|
content=content,
|
||||||
)
|
)
|
||||||
|
@ -75,10 +75,19 @@ async def handle_event(ws: _Bot, msg: MessageReceive):
|
|||||||
event = await msg_process(msg)
|
event = await msg_process(msg)
|
||||||
logger.info('[收到事件]', event=event)
|
logger.info('[收到事件]', event=event)
|
||||||
|
|
||||||
gid = event.group_id if event.group_id else 0
|
gid = event.group_id if event.group_id else '0'
|
||||||
uid = event.user_id if event.user_id else 0
|
uid = event.user_id if event.user_id else '0'
|
||||||
uuid = f'{uid}{gid}'
|
uuid = f'{gid}{uid}'
|
||||||
instances = Bot.get_instances()
|
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:
|
if uuid in instances:
|
||||||
instances[uuid].resp.append(event)
|
instances[uuid].resp.append(event)
|
||||||
instances[uuid].set_event()
|
instances[uuid].set_event()
|
||||||
|
@ -33,6 +33,18 @@ async def get_fullmatch_msg(bot: Bot, ev: Event):
|
|||||||
await bot.send('[全匹配测试]校验成功!')
|
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('开始游戏')
|
@sv_switch.on_fullmatch('开始游戏')
|
||||||
async def get_resp_msg(bot: Bot, ev: Event):
|
async def get_resp_msg(bot: Bot, ev: Event):
|
||||||
await bot.send('正在进行[开始游戏测试]')
|
await bot.send('正在进行[开始游戏测试]')
|
||||||
|
@ -33,8 +33,11 @@ def modify_func(func):
|
|||||||
result = await func(bot, event)
|
result = await func(bot, event)
|
||||||
finally:
|
finally:
|
||||||
instancess = Bot.get_instances()
|
instancess = Bot.get_instances()
|
||||||
|
mutiply_instances = Bot.get_mutiply_instances()
|
||||||
if bot.uuid in instancess:
|
if bot.uuid in instancess:
|
||||||
instancess.pop(bot.uuid)
|
instancess.pop(bot.uuid)
|
||||||
|
if bot.uuid in mutiply_instances and bot.mutiply_tag:
|
||||||
|
mutiply_instances.pop(bot.uuid)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
Loading…
x
Reference in New Issue
Block a user