mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
refactor: 独立resprule模块
This commit is contained in:
parent
2f877965cf
commit
a236089785
|
@ -18,76 +18,11 @@ from ..plugin import host as plugin_host
|
||||||
from ..plugin import models as plugin_models
|
from ..plugin import models as plugin_models
|
||||||
import tips as tips_custom
|
import tips as tips_custom
|
||||||
from ..qqbot import adapter as msadapter
|
from ..qqbot import adapter as msadapter
|
||||||
|
from . import resprule
|
||||||
|
|
||||||
from ..boot import app
|
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消息输入输出的类
|
# 控制QQ消息输入输出的类
|
||||||
class QQBotManager:
|
class QQBotManager:
|
||||||
retry = 3
|
retry = 3
|
||||||
|
@ -371,16 +306,16 @@ class QQBotManager:
|
||||||
elif Image in event.message_chain:
|
elif Image in event.message_chain:
|
||||||
pass
|
pass
|
||||||
else:
|
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()
|
reply = await process()
|
||||||
else:
|
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:
|
if check:
|
||||||
reply = await process(result.strip())
|
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))
|
logging.info("随机响应group_{}消息".format(event.group.id))
|
||||||
reply = await process()
|
reply = await process()
|
||||||
|
|
||||||
|
|
67
pkg/qqbot/resprule.py
Normal file
67
pkg/qqbot/resprule.py
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user