diff options
| author | 2023-10-07 02:50:20 +0800 | |
|---|---|---|
| committer | 2023-10-07 02:50:20 +0800 | |
| commit | fd1f123d531e25ac066dae6a1ea8dc19fd1c0964 (patch) | |
| tree | a7b1247b880af207a4aff71ae377e8a46f155d93 /src/hydrorollcore/rule.py | |
| parent | 6a63a90d8e5831e97b02cec4b67a6fb72285a2bb (diff) | |
| download | infini-fd1f123d531e25ac066dae6a1ea8dc19fd1c0964.tar.gz infini-fd1f123d531e25ac066dae6a1ea8dc19fd1c0964.zip | |
feat: BREAKING CHANGES
Co-authored-by: 白咕咕 <baimianxiao@users.noreply.github.com>
Co-authored-by: kenichiLyon <kenichiLyon@users.noreply.github.com>
Diffstat (limited to 'src/hydrorollcore/rule.py')
| -rw-r--r-- | src/hydrorollcore/rule.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/hydrorollcore/rule.py b/src/hydrorollcore/rule.py new file mode 100644 index 00000000..54eaf48e --- /dev/null +++ b/src/hydrorollcore/rule.py @@ -0,0 +1,70 @@ +from abc import ABC, abstractmethod +import os, os.path +from enum import Enum +from iamai.config import ConfigModel +from iamai.utils import is_config_class +from typing import Generic, NoReturn, Optional, Type, TYPE_CHECKING +from HydroRoll.typing import T_Event, T_Config + +if TYPE_CHECKING: + from iamai.bot import Bot + +class RuleLoadType(Enum): + """插件加载类型。""" + + DIR = "dir" + NAME = "name" + FILE = "file" + CLASS = "class" + + +class Rule(ABC, Generic[T_Event, T_Config]): + """所有 iamai 插件的基类。 + + Attributes: + event: 当前正在被此插件处理的事件。 + priority: 插件的优先级,数字越小表示优先级越高,默认为 0。 + block: 插件执行结束后是否阻止事件的传播。True 表示阻止。 + __rule_load_type__: 插件加载类型,由 iamai 自动设置,反映了此插件是如何被加载的。 + __rule_file_path__: 当插件加载类型为 `RuleLoadType.CLASS` 时为 `None`, + 否则为定义插件在的 Python 模块的位置。 + """ + + event: T_Event + priority: int = 0 + Config: Type[ConfigModel] + + __rule_load_type__: RuleLoadType + __rule_file_path__: Optional[str] + + def __init__(self, event: T_Event): + self.event = event + + if not hasattr(self, "priority"): + self.priority = 0 + + self.get = self.bot.get + + self.__post_init__() + + def __post_init__(self): + """用于初始化后处理,被 `__init__()` 方法调用。""" + pass + + @property + def name(self) -> str: + """规则类名称。""" + return self.__class__.__name__ + + @property + def bot(self) -> "Bot": + """机器人对象。""" + return self.event.adapter.bot + + @property + def config(self) -> Optional[T_Config]: + """规则包配置。""" + config_class: ConfigModel = getattr(self, "Rule", None) + if is_config_class(config_class): + return getattr(self.config.rule, config_class.__config_name__, None) + return None |
