diff --git a/config-template.py b/config-template.py index a8e843e..fbb3eea 100644 --- a/config-template.py +++ b/config-template.py @@ -82,12 +82,15 @@ default_prompt = { # 群内响应规则 # 符合此消息的群内消息即使不包含at机器人也会响应 # 支持消息前缀匹配及正则表达式匹配 +# 支持设置是否响应at消息、随机响应概率 # 注意:由消息前缀(prefix)匹配的消息中将会删除此前缀,正则表达式(regexp)匹配的消息不会删除匹配的部分 # 前缀匹配优先级高于正则表达式匹配 # 正则表达式简明教程:https://www.runoob.com/regexp/regexp-tutorial.html response_rules = { + "at": True, # 是否响应at机器人的消息 "prefix": ["/ai", "!ai", "!ai", "ai"], - "regexp": [] # "为什么.*", "怎么?样.*", "怎么.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ".*怎么办", ".*咋办" + "regexp": [], # "为什么.*", "怎么?样.*", "怎么.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ".*怎么办", ".*咋办" + "random_rate": 0.0, # 随机响应概率,0.0-1.0,0.0为不随机响应,1.0为响应所有消息, 仅在前几项判断不通过时生效 } # 消息忽略规则 diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index b973399..5d817ee 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -21,6 +21,7 @@ import pkg.utils.context import pkg.plugin.host as plugin_host import pkg.plugin.models as plugin_models + # 检查消息是否符合泛响应匹配机制 def check_response_rule(text: str): config = pkg.utils.context.get_config() @@ -45,6 +46,22 @@ def check_response_rule(text: str): return False, "" +def response_at(): + config = pkg.utils.context.get_config() + if 'at' not in config.response_rules: + return True + + return config.response_rules['at'] + + +def random_responding(): + config = pkg.utils.context.get_config() + if 'random_rate' in config.response_rules: + import random + return random.random() < config.response_rules['random_rate'] + return False + + # 控制QQ消息输入输出的类 class QQBotManager: retry = 3 @@ -296,14 +313,19 @@ class QQBotManager: if Image in event.message_chain: pass - elif At(self.bot.qq) not in event.message_chain: - check, result = check_response_rule(str(event.message_chain).strip()) - - if check: - reply = process(result.strip()) else: - # 直接调用 - reply = process() + if At(self.bot.qq) in event.message_chain and response_at(): + # 直接调用 + reply = process() + else: + check, result = check_response_rule(str(event.message_chain).strip()) + + if check: + reply = process(result.strip()) + # 检查是否随机响应 + elif random_responding(): + logging.info("随机响应group_{}消息".format(event.group.id)) + reply = process() if reply: return self.send(event, reply)