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