mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 05:06:56 +08:00
♻️ change matcher run function
This commit is contained in:
parent
6963047af2
commit
a0d13822c0
@ -63,6 +63,7 @@ matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)
|
|||||||
current_bot: ContextVar[Bot] = ContextVar("current_bot")
|
current_bot: ContextVar[Bot] = ContextVar("current_bot")
|
||||||
current_event: ContextVar[Event] = ContextVar("current_event")
|
current_event: ContextVar[Event] = ContextVar("current_event")
|
||||||
current_state: ContextVar[T_State] = ContextVar("current_state")
|
current_state: ContextVar[T_State] = ContextVar("current_state")
|
||||||
|
current_handler: ContextVar[Handler] = ContextVar("current_handler")
|
||||||
|
|
||||||
|
|
||||||
class MatcherMeta(type):
|
class MatcherMeta(type):
|
||||||
@ -636,8 +637,19 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
"""
|
"""
|
||||||
self.block = True
|
self.block = True
|
||||||
|
|
||||||
# 运行handlers
|
async def update_type(self, bot: Bot, event: Event) -> str:
|
||||||
async def run(
|
updater = self.__class__._default_type_updater
|
||||||
|
if not updater:
|
||||||
|
return "message"
|
||||||
|
return await updater(bot, event, self.state, self.type)
|
||||||
|
|
||||||
|
async def update_permission(self, bot: Bot, event: Event) -> Permission:
|
||||||
|
updater = self.__class__._default_permission_updater
|
||||||
|
if not updater:
|
||||||
|
return USER(event.get_session_id(), perm=self.permission)
|
||||||
|
return await updater(bot, event, self.state, self.permission)
|
||||||
|
|
||||||
|
async def simple_run(
|
||||||
self,
|
self,
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
event: Event,
|
event: Event,
|
||||||
@ -659,6 +671,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
while self.handlers:
|
while self.handlers:
|
||||||
handler = self.handlers.pop(0)
|
handler = self.handlers.pop(0)
|
||||||
|
current_handler.set(handler)
|
||||||
logger.debug(f"Running handler {handler}")
|
logger.debug(f"Running handler {handler}")
|
||||||
try:
|
try:
|
||||||
await handler(
|
await handler(
|
||||||
@ -674,20 +687,32 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
f"Handler {handler} param {e.param.name} value {e.value} "
|
f"Handler {handler} param {e.param.name} value {e.value} "
|
||||||
f"mismatch type {e.param._type_display()}, skipped"
|
f"mismatch type {e.param._type_display()}, skipped"
|
||||||
)
|
)
|
||||||
|
except StopPropagation:
|
||||||
|
self.block = True
|
||||||
|
finally:
|
||||||
|
logger.info(f"Matcher {self} running complete")
|
||||||
|
current_bot.reset(b_t)
|
||||||
|
current_event.reset(e_t)
|
||||||
|
current_state.reset(s_t)
|
||||||
|
|
||||||
|
# 运行handlers
|
||||||
|
async def run(
|
||||||
|
self,
|
||||||
|
bot: Bot,
|
||||||
|
event: Event,
|
||||||
|
state: T_State,
|
||||||
|
stack: Optional[AsyncExitStack] = None,
|
||||||
|
dependency_cache: Optional[T_DependencyCache] = None,
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
await self.simple_run(bot, event, state, stack, dependency_cache)
|
||||||
|
|
||||||
except RejectedException:
|
except RejectedException:
|
||||||
self.handlers.insert(0, handler) # type: ignore
|
handler = current_handler.get()
|
||||||
updater = self.__class__._default_type_updater
|
self.handlers.insert(0, handler)
|
||||||
if updater:
|
|
||||||
type_ = await updater(bot, event, self.state, self.type) # type: ignore
|
|
||||||
else:
|
|
||||||
type_ = "message"
|
|
||||||
|
|
||||||
updater = self.__class__._default_permission_updater
|
type_ = await self.update_type(bot, event)
|
||||||
if updater:
|
permission = await self.update_permission(bot, event)
|
||||||
permission = await updater(bot, event, self.state, self.permission)
|
|
||||||
else:
|
|
||||||
permission = USER(event.get_session_id(), perm=self.permission)
|
|
||||||
|
|
||||||
Matcher.new(
|
Matcher.new(
|
||||||
type_,
|
type_,
|
||||||
@ -706,17 +731,8 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
default_permission_updater=self.__class__._default_permission_updater,
|
default_permission_updater=self.__class__._default_permission_updater,
|
||||||
)
|
)
|
||||||
except PausedException:
|
except PausedException:
|
||||||
updater = self.__class__._default_type_updater
|
type_ = await self.update_type(bot, event)
|
||||||
if updater:
|
permission = await self.update_permission(bot, event)
|
||||||
type_ = await updater(bot, event, self.state, self.type) # type: ignore
|
|
||||||
else:
|
|
||||||
type_ = "message"
|
|
||||||
|
|
||||||
updater = self.__class__._default_permission_updater
|
|
||||||
if updater:
|
|
||||||
permission = await updater(bot, event, self.state, self.permission)
|
|
||||||
else:
|
|
||||||
permission = USER(event.get_session_id(), perm=self.permission)
|
|
||||||
|
|
||||||
Matcher.new(
|
Matcher.new(
|
||||||
type_,
|
type_,
|
||||||
@ -736,10 +752,3 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
)
|
)
|
||||||
except FinishedException:
|
except FinishedException:
|
||||||
pass
|
pass
|
||||||
except StopPropagation:
|
|
||||||
self.block = True
|
|
||||||
finally:
|
|
||||||
logger.info(f"Matcher {self} running complete")
|
|
||||||
current_bot.reset(b_t)
|
|
||||||
current_event.reset(e_t)
|
|
||||||
current_state.reset(s_t)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user