️ 修改Bot的创建时机

This commit is contained in:
Wuyi无疑 2023-03-05 19:39:14 +08:00
parent 1619311abe
commit 6a85ed297c
2 changed files with 10 additions and 5 deletions

View File

@ -48,7 +48,10 @@ class _Bot:
data = await self.queue.get()
task = asyncio.create_task(data)
self.bg_tasks.add(task)
task.add_done_callback(lambda _: self.bg_tasks.discard(task))
try:
task.add_done_callback(lambda _: self.bg_tasks.discard(task))
except Exception as e:
logger.error(e)
class Bot:

View File

@ -44,10 +44,9 @@ async def handle_event(ws: _Bot, msg: MessageReceive):
# 获取用户权限,越小越高
user_pm = await get_user_pml(msg)
event = await msg_process(msg)
bot = Bot(ws, event)
print(f'[收到消息] {msg}')
pending = [
_check_command(ws, bot, SL.lst[sv].TL[tr], event)
_check_command(ws, event, SL.lst[sv].TL[tr], event)
for sv in SL.lst
for tr in SL.lst[sv].TL
if (
@ -64,7 +63,10 @@ async def handle_event(ws: _Bot, msg: MessageReceive):
await asyncio.gather(*pending, return_exceptions=True)
async def _check_command(ws: _Bot, Bot: Bot, trigger: Trigger, message: Event):
async def _check_command(
ws: _Bot, ev: Event, trigger: Trigger, message: Event
):
if trigger.check_command(message):
bot = Bot(ws, ev)
message = await trigger.get_command(message)
ws.queue.put_nowait(trigger.func(Bot, message))
ws.queue.put_nowait(trigger.func(bot, message))