mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 01:18:19 +08:00
add matcher expire support
This commit is contained in:
parent
f1e62feb26
commit
10221520d9
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from datetime import timedelta
|
||||
from ipaddress import IPv4Address
|
||||
from typing import Set, Dict, Union, Optional
|
||||
|
||||
@ -29,6 +30,7 @@ class Config(BaseSettings):
|
||||
# bot runtime configs
|
||||
superusers: Set[int] = set()
|
||||
nickname: Union[str, Set[str]] = ""
|
||||
session_expire_timeout: timedelta = timedelta(minutes=2)
|
||||
|
||||
# custom configs
|
||||
custom_config: dict = {}
|
||||
|
@ -2,6 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from functools import wraps
|
||||
from datetime import datetime
|
||||
from collections import defaultdict
|
||||
from typing import Type, List, Dict, Optional, Callable
|
||||
|
||||
@ -20,6 +21,7 @@ class Matcher:
|
||||
rule: Rule = Rule()
|
||||
handlers: List[Handler] = []
|
||||
temp: bool = False
|
||||
expire_time: Optional[datetime] = None
|
||||
priority: int = 1
|
||||
|
||||
_default_state: dict = {}
|
||||
@ -41,7 +43,8 @@ class Matcher:
|
||||
temp: bool = False,
|
||||
priority: int = 1,
|
||||
*,
|
||||
default_state: dict = {}) -> Type["Matcher"]:
|
||||
default_state: dict = {},
|
||||
expire_time: Optional[datetime] = None) -> Type["Matcher"]:
|
||||
"""创建新的 Matcher
|
||||
|
||||
Returns:
|
||||
@ -53,6 +56,7 @@ class Matcher:
|
||||
"rule": rule,
|
||||
"handlers": handlers,
|
||||
"temp": temp,
|
||||
"expire_time": expire_time,
|
||||
"priority": priority,
|
||||
"_default_state": default_state
|
||||
})
|
||||
@ -154,7 +158,9 @@ class Matcher:
|
||||
self.handlers,
|
||||
temp=True,
|
||||
priority=0,
|
||||
default_state=self.state)
|
||||
default_state=self.state,
|
||||
expire_time=datetime.now() +
|
||||
bot.config.session_expire_timeout)
|
||||
matchers[0].append(matcher)
|
||||
return
|
||||
except PausedException:
|
||||
@ -162,7 +168,9 @@ class Matcher:
|
||||
self.handlers,
|
||||
temp=True,
|
||||
priority=0,
|
||||
default_state=self.state)
|
||||
default_state=self.state,
|
||||
expire_time=datetime.now() +
|
||||
bot.config.session_expire_timeout)
|
||||
matchers[0].append(matcher)
|
||||
return
|
||||
except FinishedException:
|
||||
|
@ -2,6 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from typing import Set, Callable
|
||||
|
||||
from nonebot.log import logger
|
||||
@ -30,10 +31,19 @@ async def handle_event(bot, event: Event):
|
||||
return
|
||||
|
||||
for priority in sorted(matchers.keys()):
|
||||
for index in range(len(matchers[priority])):
|
||||
index = 0
|
||||
while index <= len(matchers[priority]):
|
||||
Matcher = matchers[priority][index]
|
||||
|
||||
# Delete expired Matcher
|
||||
if datetime.now() > Matcher.expire_time:
|
||||
del matchers[priority][index]
|
||||
continue
|
||||
|
||||
# Check rule
|
||||
try:
|
||||
if not Matcher.check_rule(bot, event):
|
||||
index += 1
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
@ -42,6 +52,7 @@ async def handle_event(bot, event: Event):
|
||||
continue
|
||||
|
||||
matcher = Matcher()
|
||||
# TODO: BeforeMatcherRun
|
||||
if Matcher.temp:
|
||||
del matchers[priority][index]
|
||||
|
||||
|
@ -2,3 +2,4 @@ DRIVER=nonebot.drivers.fastapi
|
||||
HOST=0.0.0.0
|
||||
PORT=2333
|
||||
DEBUG=true
|
||||
CUSTOM_CONFIG={"custom": 1}
|
||||
|
@ -4,7 +4,7 @@
|
||||
from nonebot.rule import Rule
|
||||
from nonebot.event import Event
|
||||
from nonebot.plugin import on_message
|
||||
from nonebot.adapters.coolq import Message
|
||||
from nonebot.adapters.cqhttp import Message
|
||||
|
||||
print(repr(Message("asdfasdf[CQ:at,qq=123][CQ:at,qq=all]")))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user