diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 26769e4..a0ef855 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -18,76 +18,11 @@ from ..plugin import host as plugin_host from ..plugin import models as plugin_models import tips as tips_custom from ..qqbot import adapter as msadapter +from . import resprule from ..boot import app -# 检查消息是否符合泛响应匹配机制 -def check_response_rule(group_id:int, text: str): - config = context.get_config_manager().data - - rules = config['response_rules'] - - # 检查是否有特定规则 - if 'prefix' not in config['response_rules']: - if str(group_id) in config['response_rules']: - rules = config['response_rules'][str(group_id)] - else: - rules = config['response_rules']['default'] - - # 检查前缀匹配 - if 'prefix' in rules: - for rule in rules['prefix']: - if text.startswith(rule): - return True, text.replace(rule, "", 1) - - # 检查正则表达式匹配 - if 'regexp' in rules: - for rule in rules['regexp']: - import re - match = re.match(rule, text) - if match: - return True, text - - return False, "" - - -def response_at(group_id: int): - config = context.get_config_manager().data - - use_response_rule = config['response_rules'] - - # 检查是否有特定规则 - if 'prefix' not in config['response_rules']: - if str(group_id) in config['response_rules']: - use_response_rule = config['response_rules'][str(group_id)] - else: - use_response_rule = config['response_rules']['default'] - - if 'at' not in use_response_rule: - return True - - return use_response_rule['at'] - - -def random_responding(group_id): - config = context.get_config_manager().data - - use_response_rule = config['response_rules'] - - # 检查是否有特定规则 - if 'prefix' not in config['response_rules']: - if str(group_id) in config['response_rules']: - use_response_rule = config['response_rules'][str(group_id)] - else: - use_response_rule = config['response_rules']['default'] - - if 'random_rate' in use_response_rule: - import random - return random.random() < use_response_rule['random_rate'] - return False - - # 控制QQ消息输入输出的类 class QQBotManager: retry = 3 @@ -371,16 +306,16 @@ class QQBotManager: elif Image in event.message_chain: pass else: - if At(self.bot_account_id) in event.message_chain and response_at(event.group.id): + if At(self.bot_account_id) in event.message_chain and resprule.response_at(event.group.id): # 直接调用 reply = await process() else: - check, result = check_response_rule(event.group.id, str(event.message_chain).strip()) + check, result = resprule.check_response_rule(event.group.id, str(event.message_chain).strip()) if check: reply = await process(result.strip()) # 检查是否随机响应 - elif random_responding(event.group.id): + elif resprule.random_responding(event.group.id): logging.info("随机响应group_{}消息".format(event.group.id)) reply = await process() diff --git a/pkg/qqbot/resprule.py b/pkg/qqbot/resprule.py new file mode 100644 index 0000000..5c23702 --- /dev/null +++ b/pkg/qqbot/resprule.py @@ -0,0 +1,67 @@ +from ..utils import context + + +# 检查消息是否符合泛响应匹配机制 +def check_response_rule(group_id:int, text: str): + config = context.get_config_manager().data + + rules = config['response_rules'] + + # 检查是否有特定规则 + if 'prefix' not in config['response_rules']: + if str(group_id) in config['response_rules']: + rules = config['response_rules'][str(group_id)] + else: + rules = config['response_rules']['default'] + + # 检查前缀匹配 + if 'prefix' in rules: + for rule in rules['prefix']: + if text.startswith(rule): + return True, text.replace(rule, "", 1) + + # 检查正则表达式匹配 + if 'regexp' in rules: + for rule in rules['regexp']: + import re + match = re.match(rule, text) + if match: + return True, text + + return False, "" + + +def response_at(group_id: int): + config = context.get_config_manager().data + + use_response_rule = config['response_rules'] + + # 检查是否有特定规则 + if 'prefix' not in config['response_rules']: + if str(group_id) in config['response_rules']: + use_response_rule = config['response_rules'][str(group_id)] + else: + use_response_rule = config['response_rules']['default'] + + if 'at' not in use_response_rule: + return True + + return use_response_rule['at'] + + +def random_responding(group_id): + config = context.get_config_manager().data + + use_response_rule = config['response_rules'] + + # 检查是否有特定规则 + if 'prefix' not in config['response_rules']: + if str(group_id) in config['response_rules']: + use_response_rule = config['response_rules'][str(group_id)] + else: + use_response_rule = config['response_rules']['default'] + + if 'random_rate' in use_response_rule: + import random + return random.random() < use_response_rule['random_rate'] + return False