From 8c6ce1f030ac7d1376d22fb04c9ea7fa586ed943 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Tue, 12 Mar 2024 23:34:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BE=A4=E5=93=8D=E5=BA=94=E8=A7=84?= =?UTF-8?q?=E5=88=99=E7=9A=84=E6=89=A9=E5=B1=95=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/pipeline/resprule/resprule.py | 14 ++++++-------- pkg/pipeline/resprule/rule.py | 12 ++++++++++++ pkg/pipeline/resprule/rules/atbot.py | 1 + pkg/pipeline/resprule/rules/prefix.py | 1 + pkg/pipeline/resprule/rules/random.py | 1 + pkg/pipeline/resprule/rules/regexp.py | 1 + 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pkg/pipeline/resprule/resprule.py b/pkg/pipeline/resprule/resprule.py index 8f41872..d795d05 100644 --- a/pkg/pipeline/resprule/resprule.py +++ b/pkg/pipeline/resprule/resprule.py @@ -21,15 +21,13 @@ class GroupRespondRuleCheckStage(stage.PipelineStage): async def initialize(self): """初始化检查器 """ - self.rule_matchers = [ - atbot.AtBotRule(self.ap), - prefix.PrefixRule(self.ap), - regexp.RegExpRule(self.ap), - random.RandomRespRule(self.ap), - ] - for rule_matcher in self.rule_matchers: - await rule_matcher.initialize() + self.rule_matchers = [] + + for rule_matcher in rule.preregisetered_rules: + rule_inst = rule_matcher(self.ap) + await rule_inst.initialize() + self.rule_matchers.append(rule_inst) async def process(self, query: core_entities.Query, stage_inst_name: str) -> entities.StageProcessResult: diff --git a/pkg/pipeline/resprule/rule.py b/pkg/pipeline/resprule/rule.py index cde9ec3..bfab415 100644 --- a/pkg/pipeline/resprule/rule.py +++ b/pkg/pipeline/resprule/rule.py @@ -1,5 +1,6 @@ from __future__ import annotations import abc +import typing import mirai @@ -7,9 +8,20 @@ from ...core import app, entities as core_entities from . import entities +preregisetered_rules: list[typing.Type[GroupRespondRule]] = [] + +def rule_class(name: str): + def decorator(cls: typing.Type[GroupRespondRule]) -> typing.Type[GroupRespondRule]: + cls.name = name + preregisetered_rules.append(cls) + return cls + return decorator + + class GroupRespondRule(metaclass=abc.ABCMeta): """群组响应规则的抽象类 """ + name: str ap: app.Application diff --git a/pkg/pipeline/resprule/rules/atbot.py b/pkg/pipeline/resprule/rules/atbot.py index 692bee7..293cfd9 100644 --- a/pkg/pipeline/resprule/rules/atbot.py +++ b/pkg/pipeline/resprule/rules/atbot.py @@ -7,6 +7,7 @@ from .. import entities from ....core import entities as core_entities +@rule_model.rule_class("at-bot") class AtBotRule(rule_model.GroupRespondRule): async def match( diff --git a/pkg/pipeline/resprule/rules/prefix.py b/pkg/pipeline/resprule/rules/prefix.py index 1b61c13..99dcd4f 100644 --- a/pkg/pipeline/resprule/rules/prefix.py +++ b/pkg/pipeline/resprule/rules/prefix.py @@ -5,6 +5,7 @@ from .. import entities from ....core import entities as core_entities +@rule_model.rule_class("prefix") class PrefixRule(rule_model.GroupRespondRule): async def match( diff --git a/pkg/pipeline/resprule/rules/random.py b/pkg/pipeline/resprule/rules/random.py index 185e03e..80acf6a 100644 --- a/pkg/pipeline/resprule/rules/random.py +++ b/pkg/pipeline/resprule/rules/random.py @@ -7,6 +7,7 @@ from .. import entities from ....core import entities as core_entities +@rule_model.rule_class("random") class RandomRespRule(rule_model.GroupRespondRule): async def match( diff --git a/pkg/pipeline/resprule/rules/regexp.py b/pkg/pipeline/resprule/rules/regexp.py index 4e39d43..aaa4644 100644 --- a/pkg/pipeline/resprule/rules/regexp.py +++ b/pkg/pipeline/resprule/rules/regexp.py @@ -7,6 +7,7 @@ from .. import entities from ....core import entities as core_entities +@rule_model.rule_class("regexp") class RegExpRule(rule_model.GroupRespondRule): async def match(