We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Bug: 错误删除未执行过的`temp matcher`
**描述问题：**

处理事件时错误地删除了未执行过的 `temp matcher`

**环境信息：**

 - OS: any
 - Python Version: any
 - Nonebot Version: 2.0.0a8


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `nonebot/message.py`
Content:
```
1 """
2 事件处理
3 ========
4 
5 NoneBot 内部处理并按优先级分发事件给所有事件响应器，提供了多个插槽以进行事件的预处理等。
6 """
7 
8 import asyncio
9 from datetime import datetime
10 from typing import Set, Type, Optional, Iterable, TYPE_CHECKING
11 
12 from nonebot.log import logger
13 from nonebot.rule import TrieRule
14 from nonebot.matcher import matchers, Matcher
15 from nonebot.exception import IgnoredException, StopPropagation, NoLogException
16 from nonebot.typing import T_State, T_EventPreProcessor, T_RunPreProcessor, T_EventPostProcessor, T_RunPostProcessor
17 
18 if TYPE_CHECKING:
19     from nonebot.adapters import Bot, Event
20 
21 _event_preprocessors: Set[T_EventPreProcessor] = set()
22 _event_postprocessors: Set[T_EventPostProcessor] = set()
23 _run_preprocessors: Set[T_RunPreProcessor] = set()
24 _run_postprocessors: Set[T_RunPostProcessor] = set()
25 
26 
27 def event_preprocessor(func: T_EventPreProcessor) -> T_EventPreProcessor:
28     """
29     :说明:
30 
31       事件预处理。装饰一个函数，使它在每次接收到事件并分发给各响应器之前执行。
32 
33     :参数:
34 
35       事件预处理函数接收三个参数。
36 
37       * ``bot: Bot``: Bot 对象
38       * ``event: Event``: Event 对象
39       * ``state: T_State``: 当前 State
40     """
41     _event_preprocessors.add(func)
42     return func
43 
44 
45 def event_postprocessor(func: T_EventPostProcessor) -> T_EventPostProcessor:
46     """
47     :说明:
48 
49       事件后处理。装饰一个函数，使它在每次接收到事件并分发给各响应器之后执行。
50 
51     :参数:
52 
53       事件后处理函数接收三个参数。
54 
55       * ``bot: Bot``: Bot 对象
56       * ``event: Event``: Event 对象
57       * ``state: T_State``: 当前事件运行前 State
58     """
59     _event_postprocessors.add(func)
60     return func
61 
62 
63 def run_preprocessor(func: T_RunPreProcessor) -> T_RunPreProcessor:
64     """
65     :说明:
66 
67       运行预处理。装饰一个函数，使它在每次事件响应器运行前执行。
68 
69     :参数:
70 
71       运行预处理函数接收四个参数。
72 
73       * ``matcher: Matcher``: 当前要运行的事件响应器
74       * ``bot: Bot``: Bot 对象
75       * ``event: Event``: Event 对象
76       * ``state: T_State``: 当前 State
77     """
78     _run_preprocessors.add(func)
79     return func
80 
81 
82 def run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:
83     """
84     :说明:
85 
86       运行后处理。装饰一个函数，使它在每次事件响应器运行后执行。
87 
88     :参数:
89 
90       运行后处理函数接收五个参数。
91 
92       * ``matcher: Matcher``: 运行完毕的事件响应器
93       * ``exception: Optional[Exception]``: 事件响应器运行错误（如果存在）
94       * ``bot: Bot``: Bot 对象
95       * ``event: Event``: Event 对象
96       * ``state: T_State``: 当前 State
97     """
98     _run_postprocessors.add(func)
99     return func
100 
101 
102 async def _check_matcher(priority: int, bot: "Bot", event: "Event",
103                          state: T_State) -> Iterable[Type[Matcher]]:
104     current_matchers = matchers[priority].copy()
105 
106     async def _check(Matcher: Type[Matcher], bot: "Bot", event: "Event",
107                      state: T_State) -> Optional[Type[Matcher]]:
108         try:
109             if (not Matcher.expire_time or datetime.now() <= Matcher.expire_time
110                ) and await Matcher.check_perm(
111                    bot, event) and await Matcher.check_rule(bot, event, state):
112                 return Matcher
113         except Exception as e:
114             logger.opt(colors=True, exception=e).error(
115                 f"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>"
116             )
117         return None
118 
119     async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]:
120         if Matcher.temp or (Matcher.expire_time and
121                             datetime.now() > Matcher.expire_time):
122             return Matcher
123         return None
124 
125     checking_tasks = [
126         _check(Matcher, bot, event, state) for Matcher in current_matchers
127     ]
128     checking_expire_tasks = [
129         _check_expire(Matcher) for Matcher in current_matchers
130     ]
131     results = await asyncio.gather(*checking_tasks, return_exceptions=True)
132     expired = await asyncio.gather(*checking_expire_tasks)
133     for expired_matcher in filter(lambda x: x, expired):
134         try:
135             matchers[priority].remove(expired_matcher)  # type: ignore
136         except Exception:
137             pass
138     return filter(lambda x: x, results)
139 
140 
141 async def _run_matcher(Matcher: Type[Matcher], bot: "Bot", event: "Event",
142                        state: T_State) -> None:
143     logger.info(f"Event will be handled by {Matcher}")
144 
145     matcher = Matcher()
146 
147     coros = list(
148         map(lambda x: x(matcher, bot, event, state), _run_preprocessors))
149     if coros:
150         try:
151             await asyncio.gather(*coros)
152         except IgnoredException:
153             logger.opt(colors=True).info(
154                 f"Matcher {matcher} running is <b>cancelled</b>")
155             return
156         except Exception as e:
157             logger.opt(colors=True, exception=e).error(
158                 "<r><bg #f8bbd0>Error when running RunPreProcessors. "
159                 "Running cancelled!</bg #f8bbd0></r>")
160             return
161 
162     exception = None
163 
164     try:
165         logger.debug(f"Running matcher {matcher}")
166         await matcher.run(bot, event, state)
167     except Exception as e:
168         logger.opt(colors=True, exception=e).error(
169             f"<r><bg #f8bbd0>Running matcher {matcher} failed.</bg #f8bbd0></r>"
170         )
171         exception = e
172 
173     coros = list(
174         map(lambda x: x(matcher, exception, bot, event, state),
175             _run_postprocessors))
176     if coros:
177         try:
178             await asyncio.gather(*coros)
179         except Exception as e:
180             logger.opt(colors=True, exception=e).error(
181                 "<r><bg #f8bbd0>Error when running RunPostProcessors</bg #f8bbd0></r>"
182             )
183 
184     if matcher.block:
185         raise StopPropagation
186     return
187 
188 
189 async def handle_event(bot: "Bot", event: "Event"):
190     """
191     :说明:
192 
193        处理一个事件。调用该函数以实现分发事件。
194 
195     :参数:
196 
197       * ``bot: Bot``: Bot 对象
198       * ``event: Event``: Event 对象
199 
200     :示例:
201 
202     .. code-block:: python
203 
204         import asyncio
205         asyncio.create_task(handle_event(bot, event))
206     """
207     show_log = True
208     log_msg = f"<m>{bot.type.upper()} {bot.self_id}</m> | "
209     try:
210         log_msg += event.get_log_string()
211     except NoLogException:
212         show_log = False
213     if show_log:
214         logger.opt(colors=True).info(log_msg)
215 
216     state = {}
217     coros = list(map(lambda x: x(bot, event, state), _event_preprocessors))
218     if coros:
219         try:
220             logger.debug("Running PreProcessors...")
221             await asyncio.gather(*coros)
222         except IgnoredException:
223             logger.opt(colors=True).info(
224                 f"Event {event.get_event_name()} is <b>ignored</b>")
225             return
226         except Exception as e:
227             logger.opt(colors=True, exception=e).error(
228                 "<r><bg #f8bbd0>Error when running EventPreProcessors. "
229                 "Event ignored!</bg #f8bbd0></r>")
230             return
231 
232     # Trie Match
233     _, _ = TrieRule.get_value(bot, event, state)
234 
235     break_flag = False
236     for priority in sorted(matchers.keys()):
237         if break_flag:
238             break
239 
240         if show_log:
241             logger.debug(f"Checking for matchers in priority {priority}...")
242 
243         run_matchers = await _check_matcher(priority, bot, event, state)
244 
245         pending_tasks = [
246             _run_matcher(matcher, bot, event, state.copy())
247             for matcher in run_matchers
248         ]
249 
250         results = await asyncio.gather(*pending_tasks, return_exceptions=True)
251 
252         for result in results:
253             if isinstance(result, StopPropagation):
254                 if not break_flag:
255                     break_flag = True
256                     logger.debug("Stop event propagation")
257 
258     coros = list(map(lambda x: x(bot, event, state), _event_postprocessors))
259     if coros:
260         try:
261             logger.debug("Running PostProcessors...")
262             await asyncio.gather(*coros)
263         except Exception as e:
264             logger.opt(colors=True, exception=e).error(
265                 "<r><bg #f8bbd0>Error when running EventPostProcessors</bg #f8bbd0></r>"
266             )
267
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/nonebot/message.py b/nonebot/message.py
--- a/nonebot/message.py
+++ b/nonebot/message.py
@@ -117,8 +117,7 @@
         return None
 
     async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]:
-        if Matcher.temp or (Matcher.expire_time and
-                            datetime.now() > Matcher.expire_time):
+        if Matcher.expire_time and datetime.now() > Matcher.expire_time:
             return Matcher
         return None
 
@@ -128,14 +127,19 @@
     checking_expire_tasks = [
         _check_expire(Matcher) for Matcher in current_matchers
     ]
-    results = await asyncio.gather(*checking_tasks, return_exceptions=True)
+    results = await asyncio.gather(*checking_tasks)
     expired = await asyncio.gather(*checking_expire_tasks)
     for expired_matcher in filter(lambda x: x, expired):
         try:
             matchers[priority].remove(expired_matcher)  # type: ignore
         except Exception:
             pass
-    return filter(lambda x: x, results)
+    for temp_matcher in filter(lambda x: x and x.temp, results):
+        try:
+            matchers[priority].remove(temp_matcher)  # type: ignore
+        except Exception:
+            pass
+    return filter(lambda x: x, results)  # type: ignore
 
 
 async def _run_matcher(Matcher: Type[Matcher], bot: "Bot", event: "Event",

{"golden_diff": "diff --git a/nonebot/message.py b/nonebot/message.py\n--- a/nonebot/message.py\n+++ b/nonebot/message.py\n@@ -117,8 +117,7 @@\n         return None\n \n     async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]:\n-        if Matcher.temp or (Matcher.expire_time and\n-                            datetime.now() > Matcher.expire_time):\n+        if Matcher.expire_time and datetime.now() > Matcher.expire_time:\n             return Matcher\n         return None\n \n@@ -128,14 +127,19 @@\n     checking_expire_tasks = [\n         _check_expire(Matcher) for Matcher in current_matchers\n     ]\n-    results = await asyncio.gather(*checking_tasks, return_exceptions=True)\n+    results = await asyncio.gather(*checking_tasks)\n     expired = await asyncio.gather(*checking_expire_tasks)\n     for expired_matcher in filter(lambda x: x, expired):\n         try:\n             matchers[priority].remove(expired_matcher)  # type: ignore\n         except Exception:\n             pass\n-    return filter(lambda x: x, results)\n+    for temp_matcher in filter(lambda x: x and x.temp, results):\n+        try:\n+            matchers[priority].remove(temp_matcher)  # type: ignore\n+        except Exception:\n+            pass\n+    return filter(lambda x: x, results)  # type: ignore\n \n \n async def _run_matcher(Matcher: Type[Matcher], bot: \"Bot\", event: \"Event\",\n", "issue": "Bug: \u9519\u8bef\u5220\u9664\u672a\u6267\u884c\u8fc7\u7684`temp matcher`\n**\u63cf\u8ff0\u95ee\u9898\uff1a**\r\n\r\n\u5904\u7406\u4e8b\u4ef6\u65f6\u9519\u8bef\u5730\u5220\u9664\u4e86\u672a\u6267\u884c\u8fc7\u7684 `temp matcher`\r\n\r\n**\u73af\u5883\u4fe1\u606f\uff1a**\r\n\r\n - OS: any\r\n - Python Version: any\r\n - Nonebot Version: 2.0.0a8\r\n\n", "before_files": [{"content": "\"\"\"\n\u4e8b\u4ef6\u5904\u7406\n========\n\nNoneBot \u5185\u90e8\u5904\u7406\u5e76\u6309\u4f18\u5148\u7ea7\u5206\u53d1\u4e8b\u4ef6\u7ed9\u6240\u6709\u4e8b\u4ef6\u54cd\u5e94\u5668\uff0c\u63d0\u4f9b\u4e86\u591a\u4e2a\u63d2\u69fd\u4ee5\u8fdb\u884c\u4e8b\u4ef6\u7684\u9884\u5904\u7406\u7b49\u3002\n\"\"\"\n\nimport asyncio\nfrom datetime import datetime\nfrom typing import Set, Type, Optional, Iterable, TYPE_CHECKING\n\nfrom nonebot.log import logger\nfrom nonebot.rule import TrieRule\nfrom nonebot.matcher import matchers, Matcher\nfrom nonebot.exception import IgnoredException, StopPropagation, NoLogException\nfrom nonebot.typing import T_State, T_EventPreProcessor, T_RunPreProcessor, T_EventPostProcessor, T_RunPostProcessor\n\nif TYPE_CHECKING:\n    from nonebot.adapters import Bot, Event\n\n_event_preprocessors: Set[T_EventPreProcessor] = set()\n_event_postprocessors: Set[T_EventPostProcessor] = set()\n_run_preprocessors: Set[T_RunPreProcessor] = set()\n_run_postprocessors: Set[T_RunPostProcessor] = set()\n\n\ndef event_preprocessor(func: T_EventPreProcessor) -> T_EventPreProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u4e8b\u4ef6\u9884\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u63a5\u6536\u5230\u4e8b\u4ef6\u5e76\u5206\u53d1\u7ed9\u5404\u54cd\u5e94\u5668\u4e4b\u524d\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u4e8b\u4ef6\u9884\u5904\u7406\u51fd\u6570\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\u3002\n\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d State\n    \"\"\"\n    _event_preprocessors.add(func)\n    return func\n\n\ndef event_postprocessor(func: T_EventPostProcessor) -> T_EventPostProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u4e8b\u4ef6\u540e\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u63a5\u6536\u5230\u4e8b\u4ef6\u5e76\u5206\u53d1\u7ed9\u5404\u54cd\u5e94\u5668\u4e4b\u540e\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u4e8b\u4ef6\u540e\u5904\u7406\u51fd\u6570\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\u3002\n\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d\u4e8b\u4ef6\u8fd0\u884c\u524d State\n    \"\"\"\n    _event_postprocessors.add(func)\n    return func\n\n\ndef run_preprocessor(func: T_RunPreProcessor) -> T_RunPreProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u8fd0\u884c\u9884\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u4e8b\u4ef6\u54cd\u5e94\u5668\u8fd0\u884c\u524d\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u8fd0\u884c\u9884\u5904\u7406\u51fd\u6570\u63a5\u6536\u56db\u4e2a\u53c2\u6570\u3002\n\n      * ``matcher: Matcher``: \u5f53\u524d\u8981\u8fd0\u884c\u7684\u4e8b\u4ef6\u54cd\u5e94\u5668\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d State\n    \"\"\"\n    _run_preprocessors.add(func)\n    return func\n\n\ndef run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u8fd0\u884c\u540e\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u4e8b\u4ef6\u54cd\u5e94\u5668\u8fd0\u884c\u540e\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u8fd0\u884c\u540e\u5904\u7406\u51fd\u6570\u63a5\u6536\u4e94\u4e2a\u53c2\u6570\u3002\n\n      * ``matcher: Matcher``: \u8fd0\u884c\u5b8c\u6bd5\u7684\u4e8b\u4ef6\u54cd\u5e94\u5668\n      * ``exception: Optional[Exception]``: \u4e8b\u4ef6\u54cd\u5e94\u5668\u8fd0\u884c\u9519\u8bef\uff08\u5982\u679c\u5b58\u5728\uff09\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d State\n    \"\"\"\n    _run_postprocessors.add(func)\n    return func\n\n\nasync def _check_matcher(priority: int, bot: \"Bot\", event: \"Event\",\n                         state: T_State) -> Iterable[Type[Matcher]]:\n    current_matchers = matchers[priority].copy()\n\n    async def _check(Matcher: Type[Matcher], bot: \"Bot\", event: \"Event\",\n                     state: T_State) -> Optional[Type[Matcher]]:\n        try:\n            if (not Matcher.expire_time or datetime.now() <= Matcher.expire_time\n               ) and await Matcher.check_perm(\n                   bot, event) and await Matcher.check_rule(bot, event, state):\n                return Matcher\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                f\"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>\"\n            )\n        return None\n\n    async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]:\n        if Matcher.temp or (Matcher.expire_time and\n                            datetime.now() > Matcher.expire_time):\n            return Matcher\n        return None\n\n    checking_tasks = [\n        _check(Matcher, bot, event, state) for Matcher in current_matchers\n    ]\n    checking_expire_tasks = [\n        _check_expire(Matcher) for Matcher in current_matchers\n    ]\n    results = await asyncio.gather(*checking_tasks, return_exceptions=True)\n    expired = await asyncio.gather(*checking_expire_tasks)\n    for expired_matcher in filter(lambda x: x, expired):\n        try:\n            matchers[priority].remove(expired_matcher)  # type: ignore\n        except Exception:\n            pass\n    return filter(lambda x: x, results)\n\n\nasync def _run_matcher(Matcher: Type[Matcher], bot: \"Bot\", event: \"Event\",\n                       state: T_State) -> None:\n    logger.info(f\"Event will be handled by {Matcher}\")\n\n    matcher = Matcher()\n\n    coros = list(\n        map(lambda x: x(matcher, bot, event, state), _run_preprocessors))\n    if coros:\n        try:\n            await asyncio.gather(*coros)\n        except IgnoredException:\n            logger.opt(colors=True).info(\n                f\"Matcher {matcher} running is <b>cancelled</b>\")\n            return\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running RunPreProcessors. \"\n                \"Running cancelled!</bg #f8bbd0></r>\")\n            return\n\n    exception = None\n\n    try:\n        logger.debug(f\"Running matcher {matcher}\")\n        await matcher.run(bot, event, state)\n    except Exception as e:\n        logger.opt(colors=True, exception=e).error(\n            f\"<r><bg #f8bbd0>Running matcher {matcher} failed.</bg #f8bbd0></r>\"\n        )\n        exception = e\n\n    coros = list(\n        map(lambda x: x(matcher, exception, bot, event, state),\n            _run_postprocessors))\n    if coros:\n        try:\n            await asyncio.gather(*coros)\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running RunPostProcessors</bg #f8bbd0></r>\"\n            )\n\n    if matcher.block:\n        raise StopPropagation\n    return\n\n\nasync def handle_event(bot: \"Bot\", event: \"Event\"):\n    \"\"\"\n    :\u8bf4\u660e:\n\n       \u5904\u7406\u4e00\u4e2a\u4e8b\u4ef6\u3002\u8c03\u7528\u8be5\u51fd\u6570\u4ee5\u5b9e\u73b0\u5206\u53d1\u4e8b\u4ef6\u3002\n\n    :\u53c2\u6570:\n\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n\n    :\u793a\u4f8b:\n\n    .. code-block:: python\n\n        import asyncio\n        asyncio.create_task(handle_event(bot, event))\n    \"\"\"\n    show_log = True\n    log_msg = f\"<m>{bot.type.upper()} {bot.self_id}</m> | \"\n    try:\n        log_msg += event.get_log_string()\n    except NoLogException:\n        show_log = False\n    if show_log:\n        logger.opt(colors=True).info(log_msg)\n\n    state = {}\n    coros = list(map(lambda x: x(bot, event, state), _event_preprocessors))\n    if coros:\n        try:\n            logger.debug(\"Running PreProcessors...\")\n            await asyncio.gather(*coros)\n        except IgnoredException:\n            logger.opt(colors=True).info(\n                f\"Event {event.get_event_name()} is <b>ignored</b>\")\n            return\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running EventPreProcessors. \"\n                \"Event ignored!</bg #f8bbd0></r>\")\n            return\n\n    # Trie Match\n    _, _ = TrieRule.get_value(bot, event, state)\n\n    break_flag = False\n    for priority in sorted(matchers.keys()):\n        if break_flag:\n            break\n\n        if show_log:\n            logger.debug(f\"Checking for matchers in priority {priority}...\")\n\n        run_matchers = await _check_matcher(priority, bot, event, state)\n\n        pending_tasks = [\n            _run_matcher(matcher, bot, event, state.copy())\n            for matcher in run_matchers\n        ]\n\n        results = await asyncio.gather(*pending_tasks, return_exceptions=True)\n\n        for result in results:\n            if isinstance(result, StopPropagation):\n                if not break_flag:\n                    break_flag = True\n                    logger.debug(\"Stop event propagation\")\n\n    coros = list(map(lambda x: x(bot, event, state), _event_postprocessors))\n    if coros:\n        try:\n            logger.debug(\"Running PostProcessors...\")\n            await asyncio.gather(*coros)\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running EventPostProcessors</bg #f8bbd0></r>\"\n            )\n", "path": "nonebot/message.py"}], "after_files": [{"content": "\"\"\"\n\u4e8b\u4ef6\u5904\u7406\n========\n\nNoneBot \u5185\u90e8\u5904\u7406\u5e76\u6309\u4f18\u5148\u7ea7\u5206\u53d1\u4e8b\u4ef6\u7ed9\u6240\u6709\u4e8b\u4ef6\u54cd\u5e94\u5668\uff0c\u63d0\u4f9b\u4e86\u591a\u4e2a\u63d2\u69fd\u4ee5\u8fdb\u884c\u4e8b\u4ef6\u7684\u9884\u5904\u7406\u7b49\u3002\n\"\"\"\n\nimport asyncio\nfrom datetime import datetime\nfrom typing import Set, Type, Optional, Iterable, TYPE_CHECKING\n\nfrom nonebot.log import logger\nfrom nonebot.rule import TrieRule\nfrom nonebot.matcher import matchers, Matcher\nfrom nonebot.exception import IgnoredException, StopPropagation, NoLogException\nfrom nonebot.typing import T_State, T_EventPreProcessor, T_RunPreProcessor, T_EventPostProcessor, T_RunPostProcessor\n\nif TYPE_CHECKING:\n    from nonebot.adapters import Bot, Event\n\n_event_preprocessors: Set[T_EventPreProcessor] = set()\n_event_postprocessors: Set[T_EventPostProcessor] = set()\n_run_preprocessors: Set[T_RunPreProcessor] = set()\n_run_postprocessors: Set[T_RunPostProcessor] = set()\n\n\ndef event_preprocessor(func: T_EventPreProcessor) -> T_EventPreProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u4e8b\u4ef6\u9884\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u63a5\u6536\u5230\u4e8b\u4ef6\u5e76\u5206\u53d1\u7ed9\u5404\u54cd\u5e94\u5668\u4e4b\u524d\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u4e8b\u4ef6\u9884\u5904\u7406\u51fd\u6570\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\u3002\n\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d State\n    \"\"\"\n    _event_preprocessors.add(func)\n    return func\n\n\ndef event_postprocessor(func: T_EventPostProcessor) -> T_EventPostProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u4e8b\u4ef6\u540e\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u63a5\u6536\u5230\u4e8b\u4ef6\u5e76\u5206\u53d1\u7ed9\u5404\u54cd\u5e94\u5668\u4e4b\u540e\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u4e8b\u4ef6\u540e\u5904\u7406\u51fd\u6570\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\u3002\n\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d\u4e8b\u4ef6\u8fd0\u884c\u524d State\n    \"\"\"\n    _event_postprocessors.add(func)\n    return func\n\n\ndef run_preprocessor(func: T_RunPreProcessor) -> T_RunPreProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u8fd0\u884c\u9884\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u4e8b\u4ef6\u54cd\u5e94\u5668\u8fd0\u884c\u524d\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u8fd0\u884c\u9884\u5904\u7406\u51fd\u6570\u63a5\u6536\u56db\u4e2a\u53c2\u6570\u3002\n\n      * ``matcher: Matcher``: \u5f53\u524d\u8981\u8fd0\u884c\u7684\u4e8b\u4ef6\u54cd\u5e94\u5668\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d State\n    \"\"\"\n    _run_preprocessors.add(func)\n    return func\n\n\ndef run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:\n    \"\"\"\n    :\u8bf4\u660e:\n\n      \u8fd0\u884c\u540e\u5904\u7406\u3002\u88c5\u9970\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u5b83\u5728\u6bcf\u6b21\u4e8b\u4ef6\u54cd\u5e94\u5668\u8fd0\u884c\u540e\u6267\u884c\u3002\n\n    :\u53c2\u6570:\n\n      \u8fd0\u884c\u540e\u5904\u7406\u51fd\u6570\u63a5\u6536\u4e94\u4e2a\u53c2\u6570\u3002\n\n      * ``matcher: Matcher``: \u8fd0\u884c\u5b8c\u6bd5\u7684\u4e8b\u4ef6\u54cd\u5e94\u5668\n      * ``exception: Optional[Exception]``: \u4e8b\u4ef6\u54cd\u5e94\u5668\u8fd0\u884c\u9519\u8bef\uff08\u5982\u679c\u5b58\u5728\uff09\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n      * ``state: T_State``: \u5f53\u524d State\n    \"\"\"\n    _run_postprocessors.add(func)\n    return func\n\n\nasync def _check_matcher(priority: int, bot: \"Bot\", event: \"Event\",\n                         state: T_State) -> Iterable[Type[Matcher]]:\n    current_matchers = matchers[priority].copy()\n\n    async def _check(Matcher: Type[Matcher], bot: \"Bot\", event: \"Event\",\n                     state: T_State) -> Optional[Type[Matcher]]:\n        try:\n            if (not Matcher.expire_time or datetime.now() <= Matcher.expire_time\n               ) and await Matcher.check_perm(\n                   bot, event) and await Matcher.check_rule(bot, event, state):\n                return Matcher\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                f\"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>\"\n            )\n        return None\n\n    async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]:\n        if Matcher.expire_time and datetime.now() > Matcher.expire_time:\n            return Matcher\n        return None\n\n    checking_tasks = [\n        _check(Matcher, bot, event, state) for Matcher in current_matchers\n    ]\n    checking_expire_tasks = [\n        _check_expire(Matcher) for Matcher in current_matchers\n    ]\n    results = await asyncio.gather(*checking_tasks)\n    expired = await asyncio.gather(*checking_expire_tasks)\n    for expired_matcher in filter(lambda x: x, expired):\n        try:\n            matchers[priority].remove(expired_matcher)  # type: ignore\n        except Exception:\n            pass\n    for temp_matcher in filter(lambda x: x and x.temp, results):\n        try:\n            matchers[priority].remove(temp_matcher)  # type: ignore\n        except Exception:\n            pass\n    return filter(lambda x: x, results)  # type: ignore\n\n\nasync def _run_matcher(Matcher: Type[Matcher], bot: \"Bot\", event: \"Event\",\n                       state: T_State) -> None:\n    logger.info(f\"Event will be handled by {Matcher}\")\n\n    matcher = Matcher()\n\n    coros = list(\n        map(lambda x: x(matcher, bot, event, state), _run_preprocessors))\n    if coros:\n        try:\n            await asyncio.gather(*coros)\n        except IgnoredException:\n            logger.opt(colors=True).info(\n                f\"Matcher {matcher} running is <b>cancelled</b>\")\n            return\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running RunPreProcessors. \"\n                \"Running cancelled!</bg #f8bbd0></r>\")\n            return\n\n    exception = None\n\n    try:\n        logger.debug(f\"Running matcher {matcher}\")\n        await matcher.run(bot, event, state)\n    except Exception as e:\n        logger.opt(colors=True, exception=e).error(\n            f\"<r><bg #f8bbd0>Running matcher {matcher} failed.</bg #f8bbd0></r>\"\n        )\n        exception = e\n\n    coros = list(\n        map(lambda x: x(matcher, exception, bot, event, state),\n            _run_postprocessors))\n    if coros:\n        try:\n            await asyncio.gather(*coros)\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running RunPostProcessors</bg #f8bbd0></r>\"\n            )\n\n    if matcher.block:\n        raise StopPropagation\n    return\n\n\nasync def handle_event(bot: \"Bot\", event: \"Event\"):\n    \"\"\"\n    :\u8bf4\u660e:\n\n       \u5904\u7406\u4e00\u4e2a\u4e8b\u4ef6\u3002\u8c03\u7528\u8be5\u51fd\u6570\u4ee5\u5b9e\u73b0\u5206\u53d1\u4e8b\u4ef6\u3002\n\n    :\u53c2\u6570:\n\n      * ``bot: Bot``: Bot \u5bf9\u8c61\n      * ``event: Event``: Event \u5bf9\u8c61\n\n    :\u793a\u4f8b:\n\n    .. code-block:: python\n\n        import asyncio\n        asyncio.create_task(handle_event(bot, event))\n    \"\"\"\n    show_log = True\n    log_msg = f\"<m>{bot.type.upper()} {bot.self_id}</m> | \"\n    try:\n        log_msg += event.get_log_string()\n    except NoLogException:\n        show_log = False\n    if show_log:\n        logger.opt(colors=True).info(log_msg)\n\n    state = {}\n    coros = list(map(lambda x: x(bot, event, state), _event_preprocessors))\n    if coros:\n        try:\n            logger.debug(\"Running PreProcessors...\")\n            await asyncio.gather(*coros)\n        except IgnoredException:\n            logger.opt(colors=True).info(\n                f\"Event {event.get_event_name()} is <b>ignored</b>\")\n            return\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running EventPreProcessors. \"\n                \"Event ignored!</bg #f8bbd0></r>\")\n            return\n\n    # Trie Match\n    _, _ = TrieRule.get_value(bot, event, state)\n\n    break_flag = False\n    for priority in sorted(matchers.keys()):\n        if break_flag:\n            break\n\n        if show_log:\n            logger.debug(f\"Checking for matchers in priority {priority}...\")\n\n        run_matchers = await _check_matcher(priority, bot, event, state)\n\n        pending_tasks = [\n            _run_matcher(matcher, bot, event, state.copy())\n            for matcher in run_matchers\n        ]\n\n        results = await asyncio.gather(*pending_tasks, return_exceptions=True)\n\n        for result in results:\n            if isinstance(result, StopPropagation):\n                if not break_flag:\n                    break_flag = True\n                    logger.debug(\"Stop event propagation\")\n\n    coros = list(map(lambda x: x(bot, event, state), _event_postprocessors))\n    if coros:\n        try:\n            logger.debug(\"Running PostProcessors...\")\n            await asyncio.gather(*coros)\n        except Exception as e:\n            logger.opt(colors=True, exception=e).error(\n                \"<r><bg #f8bbd0>Error when running EventPostProcessors</bg #f8bbd0></r>\"\n            )\n", "path": "nonebot/message.py"}]}