diff --git a/none/command.py b/none/command.py index 2c36ee87..4681321f 100644 --- a/none/command.py +++ b/none/command.py @@ -247,7 +247,8 @@ class CommandSession(BaseSession): @property def is_valid(self) -> bool: """Check if the session is expired or not.""" - if self._last_interaction and \ + if self.bot.config.SESSION_EXPIRE_TIMEOUT and \ + self._last_interaction and \ datetime.now() - self._last_interaction > \ self.bot.config.SESSION_EXPIRE_TIMEOUT: return False diff --git a/none/default_config.py b/none/default_config.py index 575667aa..2549c399 100644 --- a/none/default_config.py +++ b/none/default_config.py @@ -15,21 +15,25 @@ For example: """ from datetime import timedelta +from typing import Container, Union, Iterable, Pattern, Optional -API_ROOT = '' -SECRET = '' -ACCESS_TOKEN = '' -HOST = '127.0.0.1' -PORT = 8080 -DEBUG = True +from .expression import Expression_T -SUPERUSERS = set() -NICKNAME = '' -COMMAND_START = {'/', '!', '/', '!'} -COMMAND_SEP = {'/', '.'} -SESSION_EXPIRE_TIMEOUT = timedelta(minutes=5) -SESSION_RUNNING_EXPRESSION = '您有命令正在执行,请稍后再试' -SHORT_MESSAGE_MAX_LENGTH = 50 +API_ROOT: str = '' +SECRET: str = '' +ACCESS_TOKEN: str = '' +HOST: str = '127.0.0.1' +PORT: int = 8080 +DEBUG: bool = True + +SUPERUSERS: Container[int] = set() +NICKNAME: Union[str, Iterable[str]] = '' +COMMAND_START: Iterable[Union[str, Pattern]] = {'/', '!', '/', '!'} +COMMAND_SEP: Iterable[Union[str, Pattern]] = {'/', '.'} +SESSION_EXPIRE_TIMEOUT: Optional[timedelta] = timedelta(minutes=5) +SESSION_RUN_TIMEOUT: Optional[timedelta] = None +SESSION_RUNNING_EXPRESSION: Expression_T = '您有命令正在执行,请稍后再试' +SHORT_MESSAGE_MAX_LENGTH: int = 50 APSCHEDULER_CONFIG = { 'apscheduler.timezone': 'Asia/Shanghai' diff --git a/none/expression.py b/none/expression.py index 85ea46b9..037a41eb 100644 --- a/none/expression.py +++ b/none/expression.py @@ -3,8 +3,10 @@ from typing import Union, Sequence, Callable from aiocqhttp import message +Expression_T = Union[str, Sequence[str], Callable] -def render(expr: Union[str, Sequence[str], Callable], *, escape_args=True, + +def render(expr: Expression_T, *, escape_args=True, **kwargs) -> str: """ Render an expression to message string. diff --git a/none/natural_language.py b/none/natural_language.py index 4a7caf6a..b5f768ae 100644 --- a/none/natural_language.py +++ b/none/natural_language.py @@ -86,11 +86,10 @@ async def handle_natural_language(bot: NoneBot, ctx: Dict[str, Any]) -> bool: msg = str(ctx['message']) if bot.config.NICKNAME: # check if the user is calling me with my nickname - if not isinstance(bot.config.NICKNAME, Iterable): - # noinspection PyUnusedLocal + if isinstance(bot.config.NICKNAME, str) or \ + not isinstance(bot.config.NICKNAME, Iterable): nicknames = (bot.config.NICKNAME,) else: - # noinspection PyUnusedLocal nicknames = filter(lambda n: n, bot.config.NICKNAME) m = re.search(rf'^({"|".join(nicknames)})[\s,,]+', msg, re.IGNORECASE) if m: