🐛 修复Bot.receive_mutiply_resp()可能失效的问题

This commit is contained in:
KimigaiiWuyi 2023-12-03 01:34:13 +08:00
parent 9d131b2089
commit 35a5841d33
5 changed files with 58 additions and 34 deletions

View File

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

View File

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

View File

@ -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('开始游戏')

View File

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

View File

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