Skip to content

Commit

Permalink
改改
Browse files Browse the repository at this point in the history
  • Loading branch information
eya46 committed Jul 27, 2023
1 parent 3af025e commit 3ee9d86
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions nonebot/internal/matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Matcher(metaclass=MatcherMeta):
def __init__(self):
self.handlers = self.handlers.copy()
self.state = self._default_state.copy()
self._m_t = current_matcher.set(self)

def __repr__(self) -> str:
return (
Expand Down Expand Up @@ -620,53 +621,19 @@ def get_arg(cls, key: str, default: T) -> Union[Message, T]:
...

@classmethod
@overload
def get_arg(
cls, key: str, default: Optional[T] = None
) -> Optional[Union[Message, T]]:
...

@overload
def get_arg(self, key: str) -> Union[Message, None]:
...

@overload
def get_arg(self, key: str, default: T) -> Union[Message, T]:
...

@overload
def get_arg(
self, key: str, default: Optional[T] = None
) -> Optional[Union[Message, T]]:
...

def get_arg(
self, key: str = None, default: Optional[T] = None
) -> Optional[Union[Message, T]]:
"""获取一个 `got` 消息
如果没有找到对应的消息,返回 `default` 值
"""
if isinstance(self, Matcher):
return self.state.get(ARG_KEY.format(key=key), default)
else:
return current_matcher.get().state.get(ARG_KEY.format(key=self), key)
return current_matcher.get().state.get(ARG_KEY.format(key=key), default)

@classmethod
@overload
def set_arg(cls, key: str, message: Message) -> None:
...

@overload
def set_arg(self, key: str, message: Message) -> None:
...

def set_arg(self, key: str, message: Message = None) -> None:
"""设置一个 `got` 消息"""
if isinstance(self, Matcher):
self.state[ARG_KEY.format(key=key)] = message
else:
current_matcher.get().state[ARG_KEY.format(key=self)] = key
current_matcher.get().state[ARG_KEY.format(key=key)] = message

def set_target(self, target: str, cache: bool = True) -> None:
if cache:
Expand Down Expand Up @@ -738,13 +705,13 @@ async def resolve_reject(self):
def ensure_context(self, bot: Bot, event: Event):
b_t = current_bot.set(bot)
e_t = current_event.set(event)
m_t = current_matcher.set(self)
self._m_t = current_matcher.set(self)
try:
yield
finally:
current_bot.reset(b_t)
current_event.reset(e_t)
current_matcher.reset(m_t)
current_matcher.reset(self._m_t)

async def simple_run(
self,
Expand Down

0 comments on commit 3ee9d86

Please sign in to comment.