2022-12-10 16:40:05 +08:00
|
|
|
|
import asyncio
|
2022-12-11 17:17:33 +08:00
|
|
|
|
import json
|
|
|
|
|
import os
|
2022-12-10 16:40:05 +08:00
|
|
|
|
import threading
|
|
|
|
|
|
2023-01-02 11:33:26 +08:00
|
|
|
|
import mirai.models.bus
|
2022-12-10 16:40:05 +08:00
|
|
|
|
import openai.error
|
2022-12-21 14:17:57 +08:00
|
|
|
|
from mirai import At, GroupMessage, MessageEvent, Mirai, Plain, StrangerMessage, WebSocketAdapter, HTTPAdapter, \
|
|
|
|
|
FriendMessage, Image
|
2022-12-09 16:18:25 +08:00
|
|
|
|
|
2023-01-02 11:33:26 +08:00
|
|
|
|
from mirai.models.bus import ModelEventBus
|
|
|
|
|
|
2023-01-01 17:20:54 +08:00
|
|
|
|
from mirai.models.message import Quote
|
|
|
|
|
|
2022-12-09 16:18:25 +08:00
|
|
|
|
import config
|
2022-12-07 23:37:52 +08:00
|
|
|
|
import pkg.openai.session
|
2022-12-15 18:50:15 +08:00
|
|
|
|
import pkg.openai.manager
|
2022-12-26 19:37:25 +08:00
|
|
|
|
from func_timeout import FunctionTimedOut
|
2022-12-08 21:58:02 +08:00
|
|
|
|
import logging
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
import pkg.qqbot.filter
|
2022-12-26 19:37:25 +08:00
|
|
|
|
import pkg.qqbot.process as processor
|
2023-01-01 23:18:32 +08:00
|
|
|
|
import pkg.utils.context
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-08 12:06:04 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
# 并行运行
|
|
|
|
|
def go(func, args=()):
|
|
|
|
|
thread = threading.Thread(target=func, args=args, daemon=True)
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
|
2022-12-19 17:07:31 +08:00
|
|
|
|
# 检查消息是否符合泛响应匹配机制
|
|
|
|
|
def check_response_rule(text: str) -> (bool, str):
|
2022-12-19 17:15:17 +08:00
|
|
|
|
if not hasattr(config, 'response_rules'):
|
|
|
|
|
return False, ''
|
|
|
|
|
|
2022-12-19 17:07:31 +08:00
|
|
|
|
rules = config.response_rules
|
|
|
|
|
# 检查前缀匹配
|
2022-12-19 17:15:17 +08:00
|
|
|
|
if 'prefix' in rules:
|
|
|
|
|
for rule in rules['prefix']:
|
|
|
|
|
if text.startswith(rule):
|
|
|
|
|
return True, text.replace(rule, "", 1)
|
2022-12-19 17:07:31 +08:00
|
|
|
|
|
|
|
|
|
# 检查正则表达式匹配
|
2022-12-19 17:15:17 +08:00
|
|
|
|
if 'regexp' in rules:
|
|
|
|
|
for rule in rules['regexp']:
|
|
|
|
|
import re
|
|
|
|
|
match = re.match(rule, text)
|
|
|
|
|
if match:
|
2022-12-19 17:17:49 +08:00
|
|
|
|
return True, text
|
2022-12-19 17:07:31 +08:00
|
|
|
|
|
|
|
|
|
return False, ""
|
|
|
|
|
|
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 控制QQ消息输入输出的类
|
2022-12-07 23:37:52 +08:00
|
|
|
|
class QQBotManager:
|
|
|
|
|
retry = 3
|
|
|
|
|
|
|
|
|
|
bot = None
|
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
reply_filter = None
|
|
|
|
|
|
2023-01-02 00:35:36 +08:00
|
|
|
|
def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, first_time_init=True):
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
self.timeout = timeout
|
|
|
|
|
self.retry = retry
|
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
if os.path.exists("sensitive.json") \
|
|
|
|
|
and config.sensitive_word_filter is not None \
|
|
|
|
|
and config.sensitive_word_filter:
|
|
|
|
|
with open("sensitive.json", "r", encoding="utf-8") as f:
|
|
|
|
|
self.reply_filter = pkg.qqbot.filter.ReplyFilter(json.load(f)['words'])
|
|
|
|
|
else:
|
|
|
|
|
self.reply_filter = pkg.qqbot.filter.ReplyFilter([])
|
|
|
|
|
|
2023-01-02 00:35:36 +08:00
|
|
|
|
if first_time_init:
|
|
|
|
|
self.first_time_init(mirai_http_api_config)
|
|
|
|
|
else:
|
|
|
|
|
self.bot = pkg.utils.context.get_qqbot_manager().bot
|
|
|
|
|
|
|
|
|
|
pkg.utils.context.set_qqbot_manager(self)
|
|
|
|
|
|
2023-01-02 11:33:26 +08:00
|
|
|
|
# Caution: 注册新的事件处理器之后,请务必在unsubscribe_all中编写相应的取消订阅代码
|
|
|
|
|
@self.bot.on(FriendMessage)
|
|
|
|
|
async def on_friend_message(event: FriendMessage):
|
|
|
|
|
go(self.on_person_message, (event,))
|
|
|
|
|
|
|
|
|
|
@self.bot.on(StrangerMessage)
|
|
|
|
|
async def on_stranger_message(event: StrangerMessage):
|
|
|
|
|
go(self.on_person_message, (event,))
|
|
|
|
|
|
|
|
|
|
@self.bot.on(GroupMessage)
|
|
|
|
|
async def on_group_message(event: GroupMessage):
|
|
|
|
|
go(self.on_group_message, (event,))
|
|
|
|
|
|
|
|
|
|
def unsubscribe_all():
|
|
|
|
|
assert isinstance(self.bot, Mirai)
|
|
|
|
|
bus = self.bot.bus
|
|
|
|
|
assert isinstance(bus, mirai.models.bus.ModelEventBus)
|
|
|
|
|
|
|
|
|
|
bus.unsubscribe(FriendMessage, on_friend_message)
|
|
|
|
|
bus.unsubscribe(StrangerMessage, on_stranger_message)
|
|
|
|
|
bus.unsubscribe(GroupMessage, on_group_message)
|
|
|
|
|
|
|
|
|
|
self.unsubscribe_all = unsubscribe_all
|
|
|
|
|
|
2023-01-02 00:35:36 +08:00
|
|
|
|
def first_time_init(self, mirai_http_api_config: dict):
|
2023-01-02 11:33:26 +08:00
|
|
|
|
"""热重载后不再运行此函数"""
|
2023-01-02 00:35:36 +08:00
|
|
|
|
|
2022-12-21 14:17:57 +08:00
|
|
|
|
if 'adapter' not in mirai_http_api_config or mirai_http_api_config['adapter'] == "WebSocketAdapter":
|
2022-12-20 22:29:24 +08:00
|
|
|
|
bot = Mirai(
|
2022-12-21 14:17:57 +08:00
|
|
|
|
qq=mirai_http_api_config['qq'],
|
|
|
|
|
adapter=WebSocketAdapter(
|
|
|
|
|
verify_key=mirai_http_api_config['verifyKey'],
|
|
|
|
|
host=mirai_http_api_config['host'],
|
|
|
|
|
port=mirai_http_api_config['port']
|
|
|
|
|
)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
)
|
2022-12-20 22:29:24 +08:00
|
|
|
|
elif mirai_http_api_config['adapter'] == "HTTPAdapter":
|
|
|
|
|
bot = Mirai(
|
2022-12-21 14:17:57 +08:00
|
|
|
|
qq=mirai_http_api_config['qq'],
|
|
|
|
|
adapter=HTTPAdapter(
|
|
|
|
|
verify_key=mirai_http_api_config['verifyKey'],
|
|
|
|
|
host=mirai_http_api_config['host'],
|
|
|
|
|
port=mirai_http_api_config['port']
|
|
|
|
|
)
|
2022-12-20 22:29:24 +08:00
|
|
|
|
)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-26 19:37:25 +08:00
|
|
|
|
else:
|
|
|
|
|
raise Exception("未知的适配器类型")
|
|
|
|
|
|
2022-12-07 23:37:52 +08:00
|
|
|
|
self.bot = bot
|
|
|
|
|
|
2023-01-01 17:20:54 +08:00
|
|
|
|
def send(self, event, msg, check_quote=True):
|
|
|
|
|
asyncio.run(
|
2023-01-01 18:27:34 +08:00
|
|
|
|
self.bot.send(event, msg, quote=True if hasattr(config,
|
|
|
|
|
"quote_origin") and config.quote_origin and check_quote else False))
|
2022-12-12 22:04:38 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 私聊消息处理
|
2022-12-12 22:04:38 +08:00
|
|
|
|
def on_person_message(self, event: MessageEvent):
|
2022-12-07 23:37:52 +08:00
|
|
|
|
reply = ''
|
|
|
|
|
|
|
|
|
|
if event.sender.id == self.bot.qq:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
if Image in event.message_chain:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2022-12-12 22:04:38 +08:00
|
|
|
|
# 超时则重试,重试超过次数则放弃
|
|
|
|
|
failed = 0
|
|
|
|
|
for i in range(self.retry):
|
|
|
|
|
try:
|
2023-01-01 17:20:54 +08:00
|
|
|
|
reply = processor.process_message('person', event.sender.id, str(event.message_chain),
|
|
|
|
|
event.message_chain,
|
|
|
|
|
event.sender.id)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
break
|
|
|
|
|
except FunctionTimedOut:
|
2022-12-13 16:04:51 +08:00
|
|
|
|
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
|
2022-12-12 22:04:38 +08:00
|
|
|
|
failed += 1
|
|
|
|
|
continue
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
if failed == self.retry:
|
2022-12-13 16:04:51 +08:00
|
|
|
|
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
|
2022-12-13 00:45:56 +08:00
|
|
|
|
self.notify_admin("{} 请求超时".format("person_{}".format(event.sender.id)))
|
2022-12-26 23:53:56 +08:00
|
|
|
|
reply = ["[bot]err:请求超时"]
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-26 23:53:56 +08:00
|
|
|
|
if reply:
|
2023-01-01 17:20:54 +08:00
|
|
|
|
return self.send(event, reply, check_quote=False)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 群消息处理
|
2022-12-12 22:04:38 +08:00
|
|
|
|
def on_group_message(self, event: GroupMessage):
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
reply = ''
|
|
|
|
|
|
2022-12-21 14:17:57 +08:00
|
|
|
|
def process(text=None) -> str:
|
2022-12-19 17:07:31 +08:00
|
|
|
|
replys = ""
|
2022-12-19 17:19:09 +08:00
|
|
|
|
if At(self.bot.qq) in event.message_chain:
|
|
|
|
|
event.message_chain.remove(At(self.bot.qq))
|
2022-12-08 12:06:04 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
# 超时则重试,重试超过次数则放弃
|
|
|
|
|
failed = 0
|
|
|
|
|
for i in range(self.retry):
|
|
|
|
|
try:
|
2022-12-26 19:37:25 +08:00
|
|
|
|
replys = processor.process_message('group', event.group.id,
|
2023-01-01 17:20:54 +08:00
|
|
|
|
str(event.message_chain).strip() if text is None else text,
|
|
|
|
|
event.message_chain,
|
|
|
|
|
event.sender.id)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
break
|
|
|
|
|
except FunctionTimedOut:
|
2023-01-02 00:11:10 +08:00
|
|
|
|
pkg.openai.session.get_session('group_{}'.format(event.group.id)).release_response_lock()
|
2022-12-12 22:04:38 +08:00
|
|
|
|
failed += 1
|
|
|
|
|
continue
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
if failed == self.retry:
|
2023-01-02 00:11:10 +08:00
|
|
|
|
pkg.openai.session.get_session('group_{}'.format(event.group.id)).release_response_lock()
|
|
|
|
|
self.notify_admin("{} 请求超时".format("group_{}".format(event.group.id)))
|
2022-12-26 23:53:56 +08:00
|
|
|
|
replys = ["[bot]err:请求超时"]
|
2022-12-19 17:07:31 +08:00
|
|
|
|
|
|
|
|
|
return replys
|
|
|
|
|
|
|
|
|
|
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:
|
2022-12-19 17:25:09 +08:00
|
|
|
|
reply = process(result.strip())
|
2022-12-19 17:07:31 +08:00
|
|
|
|
else:
|
|
|
|
|
# 直接调用
|
|
|
|
|
reply = process()
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-26 23:53:56 +08:00
|
|
|
|
if reply:
|
2022-12-12 22:04:38 +08:00
|
|
|
|
return self.send(event, reply)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 通知系统管理员
|
2022-12-10 16:40:05 +08:00
|
|
|
|
def notify_admin(self, message: str):
|
2022-12-12 22:11:32 +08:00
|
|
|
|
if hasattr(config, "admin_qq") and config.admin_qq != 0:
|
2022-12-13 13:36:16 +08:00
|
|
|
|
logging.info("通知管理员:{}".format(message))
|
2022-12-10 16:40:05 +08:00
|
|
|
|
send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message))
|
|
|
|
|
threading.Thread(target=asyncio.run, args=(send_task,)).start()
|