From b3a796e7260d1da6e738315a2078ee4d5f559e95 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 8 Sep 2020 14:36:21 +0800 Subject: [PATCH 1/2] :bug: fix: key erro when command not found --- nonebot/rule.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/nonebot/rule.py b/nonebot/rule.py index a992e488..15dc1e6a 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -60,9 +60,15 @@ class TrieRule: def get_value(cls, bot: Bot, event: Event, state: dict) -> Tuple[Dict[str, Any], Dict[str, Any]]: if event.type != "message": - state["_prefix"] = {} - state["_suffix"] = {} - return {}, {} + state["_prefix"] = {"raw_command": None, "command": None} + state["_suffix"] = {"raw_command": None, "command": None} + return { + "raw_command": None, + "command": None + }, { + "raw_command": None, + "command": None + } prefix = None suffix = None @@ -77,19 +83,31 @@ class TrieRule: state["_prefix"] = { "raw_command": prefix.key, "command": prefix.value - } if prefix else {} + } if prefix else { + "raw_command": None, + "command": None + } state["_suffix"] = { "raw_command": suffix.key, "command": suffix.value - } if suffix else {} + } if suffix else { + "raw_command": None, + "command": None + } return ({ "raw_command": prefix.key, "command": prefix.value - } if prefix else {}, { + } if prefix else { + "raw_command": None, + "command": None + }, { "raw_command": suffix.key, "command": suffix.value - } if suffix else {}) + } if suffix else { + "raw_command": None, + "command": None + }) def startswith(msg: str) -> Rule: From 6d37417d212e8d1c66c66c88028ab1b82ac69172 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 8 Sep 2020 15:55:53 +0800 Subject: [PATCH 2/2] :building_construction: make event handling in background --- nonebot/drivers/fastapi.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nonebot/drivers/fastapi.py b/nonebot/drivers/fastapi.py index 21f56016..663998d1 100644 --- a/nonebot/drivers/fastapi.py +++ b/nonebot/drivers/fastapi.py @@ -3,6 +3,7 @@ import hmac import json +import asyncio import logging import uvicorn @@ -151,7 +152,7 @@ class Driver(BaseDriver): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="adapter not found") - await bot.handle_message(data) + asyncio.create_task(bot.handle_message(data)) return Response("", 204) @overrides(BaseDriver) @@ -203,7 +204,7 @@ class Driver(BaseDriver): if not data: continue - await bot.handle_message(data) + asyncio.create_task(bot.handle_message(data)) finally: del self._clients[x_self_id]