2023-01-04 17:09:57 +08:00
|
|
|
|
import importlib
|
2022-12-07 16:48:32 +08:00
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2022-12-08 12:06:04 +08:00
|
|
|
|
import threading
|
|
|
|
|
import time
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
2022-12-09 00:01:49 +08:00
|
|
|
|
import logging
|
2022-12-11 19:57:50 +08:00
|
|
|
|
import sys
|
2023-02-10 19:03:25 +08:00
|
|
|
|
|
2023-01-02 13:22:50 +08:00
|
|
|
|
try:
|
|
|
|
|
import colorlog
|
|
|
|
|
except ImportError:
|
|
|
|
|
print("未安装colorlog,请查看 https://github.com/RockChinQ/qcg-installer/issues/15")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
import colorlog
|
2022-12-11 19:57:50 +08:00
|
|
|
|
|
2023-01-06 13:50:45 +08:00
|
|
|
|
import requests
|
|
|
|
|
import websockets.exceptions
|
|
|
|
|
from urllib3.exceptions import InsecureRequestWarning
|
|
|
|
|
|
|
|
|
|
|
2022-12-15 17:52:41 +08:00
|
|
|
|
sys.path.append(".")
|
2022-12-11 19:57:50 +08:00
|
|
|
|
|
2022-12-09 00:01:49 +08:00
|
|
|
|
log_colors_config = {
|
|
|
|
|
'DEBUG': 'green', # cyan white
|
|
|
|
|
'INFO': 'white',
|
|
|
|
|
'WARNING': 'yellow',
|
|
|
|
|
'ERROR': 'red',
|
|
|
|
|
'CRITICAL': 'bold_red',
|
|
|
|
|
}
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
2022-12-09 15:44:49 +08:00
|
|
|
|
|
2022-12-08 00:41:35 +08:00
|
|
|
|
def init_db():
|
2022-12-09 15:24:47 +08:00
|
|
|
|
import pkg.database.manager
|
2022-12-11 13:58:47 +08:00
|
|
|
|
database = pkg.database.manager.DatabaseManager()
|
2022-12-08 00:41:35 +08:00
|
|
|
|
|
|
|
|
|
database.initialize_database()
|
2022-12-07 16:48:32 +08:00
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = False
|
2022-12-07 16:48:32 +08:00
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
|
2023-01-17 11:54:33 +08:00
|
|
|
|
def reset_logging():
|
|
|
|
|
assert os.path.exists('config.py')
|
|
|
|
|
|
|
|
|
|
config = importlib.import_module('config')
|
|
|
|
|
|
|
|
|
|
import pkg.utils.context
|
|
|
|
|
|
|
|
|
|
if pkg.utils.context.context['logger_handler'] is not None:
|
|
|
|
|
logging.getLogger().removeHandler(pkg.utils.context.context['logger_handler'])
|
|
|
|
|
|
|
|
|
|
for handler in logging.getLogger().handlers:
|
|
|
|
|
logging.getLogger().removeHandler(handler)
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=config.logging_level, # 设置日志输出格式
|
|
|
|
|
filename='qchatgpt.log', # log日志输出的文件位置和文件名
|
|
|
|
|
format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s",
|
|
|
|
|
# 日志输出的格式
|
|
|
|
|
# -8表示占位符,让输出左对齐,输出长度都为8位
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式
|
|
|
|
|
)
|
|
|
|
|
sh = logging.StreamHandler()
|
|
|
|
|
sh.setLevel(config.logging_level)
|
|
|
|
|
sh.setFormatter(colorlog.ColoredFormatter(
|
|
|
|
|
fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : "
|
|
|
|
|
"%(message)s",
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
|
log_colors=log_colors_config
|
|
|
|
|
))
|
|
|
|
|
logging.getLogger().addHandler(sh)
|
|
|
|
|
return sh
|
|
|
|
|
|
|
|
|
|
|
2023-01-02 00:35:36 +08:00
|
|
|
|
def main(first_time_init=False):
|
2023-01-04 12:01:26 +08:00
|
|
|
|
global known_exception_caught
|
2023-01-02 13:11:33 +08:00
|
|
|
|
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = False
|
|
|
|
|
try:
|
|
|
|
|
# 导入config.py
|
|
|
|
|
assert os.path.exists('config.py')
|
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
config = importlib.import_module('config')
|
2023-01-04 12:01:26 +08:00
|
|
|
|
|
|
|
|
|
import pkg.utils.context
|
2023-01-04 17:09:57 +08:00
|
|
|
|
pkg.utils.context.set_config(config)
|
|
|
|
|
|
2023-01-17 11:54:33 +08:00
|
|
|
|
sh = reset_logging()
|
2023-01-04 12:01:26 +08:00
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
# 检查是否设置了管理员
|
|
|
|
|
if not (hasattr(config, 'admin_qq') and config.admin_qq != 0):
|
2023-01-05 18:08:18 +08:00
|
|
|
|
# logging.warning("未设置管理员QQ,管理员权限指令及运行告警将无法使用,如需设置请修改config.py中的admin_qq字段")
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
config.admin_qq = int(input("未设置管理员QQ,管理员权限指令及运行告警将无法使用,请输入管理员QQ号: "))
|
|
|
|
|
# 写入到文件
|
|
|
|
|
|
|
|
|
|
# 读取文件
|
|
|
|
|
config_file_str = ""
|
|
|
|
|
with open("config.py", "r", encoding="utf-8") as f:
|
|
|
|
|
config_file_str = f.read()
|
|
|
|
|
# 替换
|
|
|
|
|
config_file_str = config_file_str.replace("admin_qq = 0", "admin_qq = " + str(config.admin_qq))
|
|
|
|
|
# 写入
|
|
|
|
|
with open("config.py", "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(config_file_str)
|
|
|
|
|
|
|
|
|
|
print("管理员QQ已设置,如需修改请修改config.py中的admin_qq字段")
|
|
|
|
|
time.sleep(4)
|
|
|
|
|
break
|
|
|
|
|
except ValueError:
|
|
|
|
|
print("请输入数字")
|
2023-01-04 17:09:57 +08:00
|
|
|
|
|
2023-01-04 12:01:26 +08:00
|
|
|
|
import pkg.openai.manager
|
|
|
|
|
import pkg.database.manager
|
|
|
|
|
import pkg.openai.session
|
|
|
|
|
import pkg.qqbot.manager
|
|
|
|
|
|
|
|
|
|
pkg.utils.context.context['logger_handler'] = sh
|
|
|
|
|
# 主启动流程
|
|
|
|
|
database = pkg.database.manager.DatabaseManager()
|
|
|
|
|
|
|
|
|
|
database.initialize_database()
|
|
|
|
|
|
|
|
|
|
openai_interact = pkg.openai.manager.OpenAIInteract(config.openai_config['api_key'])
|
|
|
|
|
|
|
|
|
|
# 加载所有未超时的session
|
|
|
|
|
pkg.openai.session.load_sessions()
|
|
|
|
|
|
|
|
|
|
# 初始化qq机器人
|
|
|
|
|
qqbot = pkg.qqbot.manager.QQBotManager(mirai_http_api_config=config.mirai_http_api_config,
|
|
|
|
|
timeout=config.process_message_timeout, retry=config.retry_times,
|
|
|
|
|
first_time_init=first_time_init)
|
|
|
|
|
|
2023-01-13 16:49:56 +08:00
|
|
|
|
# 加载插件
|
|
|
|
|
import pkg.plugin.host
|
|
|
|
|
pkg.plugin.host.load_plugins()
|
|
|
|
|
|
|
|
|
|
pkg.plugin.host.initialize_plugins()
|
|
|
|
|
|
2023-01-04 12:01:26 +08:00
|
|
|
|
if first_time_init: # 不是热重载之后的启动,则不启动新的bot线程
|
|
|
|
|
|
2023-01-06 13:50:45 +08:00
|
|
|
|
import mirai.exceptions
|
2023-01-13 16:49:56 +08:00
|
|
|
|
|
2023-01-04 12:01:26 +08:00
|
|
|
|
def run_bot_wrapper():
|
|
|
|
|
global known_exception_caught
|
|
|
|
|
try:
|
|
|
|
|
qqbot.bot.run()
|
|
|
|
|
except TypeError as e:
|
|
|
|
|
if str(e).__contains__("argument 'debug'"):
|
2023-01-04 17:09:57 +08:00
|
|
|
|
logging.error(
|
2023-01-18 20:42:31 +08:00
|
|
|
|
"连接bot失败:{}, 解决方案: https://github.com/RockChinQ/QChatGPT/issues/82".format(e))
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = True
|
|
|
|
|
elif str(e).__contains__("As of 3.10, the *loop*"):
|
2023-01-04 17:09:57 +08:00
|
|
|
|
logging.error(
|
2023-01-18 20:42:31 +08:00
|
|
|
|
"Websockets版本过低:{}, 解决方案: https://github.com/RockChinQ/QChatGPT/issues/5".format(e))
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = True
|
|
|
|
|
|
|
|
|
|
except websockets.exceptions.InvalidStatus as e:
|
2023-01-04 17:09:57 +08:00
|
|
|
|
logging.error(
|
2023-01-18 20:42:31 +08:00
|
|
|
|
"mirai-api-http端口无法使用:{}, 解决方案: https://github.com/RockChinQ/QChatGPT/issues/22".format(
|
2023-01-04 17:09:57 +08:00
|
|
|
|
e))
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = True
|
|
|
|
|
except mirai.exceptions.NetworkError as e:
|
|
|
|
|
logging.error("连接mirai-api-http失败:{}, 请检查是否已按照文档启动mirai".format(e))
|
|
|
|
|
known_exception_caught = True
|
|
|
|
|
except Exception as e:
|
2023-01-04 17:09:57 +08:00
|
|
|
|
if str(e).__contains__("404"):
|
|
|
|
|
logging.error(
|
2023-01-18 20:42:31 +08:00
|
|
|
|
"mirai-api-http端口无法使用:{}, 解决方案: https://github.com/RockChinQ/QChatGPT/issues/22".format(
|
2023-01-04 17:09:57 +08:00
|
|
|
|
e))
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = True
|
2023-01-05 16:15:46 +08:00
|
|
|
|
elif str(e).__contains__("signal only works in main thread"):
|
|
|
|
|
logging.error(
|
2023-01-18 20:42:31 +08:00
|
|
|
|
"hypercorn异常:{}, 解决方案: https://github.com/RockChinQ/QChatGPT/issues/86".format(
|
2023-01-05 16:15:46 +08:00
|
|
|
|
e))
|
|
|
|
|
known_exception_caught = True
|
2023-01-17 17:34:20 +08:00
|
|
|
|
elif str(e).__contains__("did not receive a valid HTTP"):
|
|
|
|
|
logging.error(
|
2023-01-18 20:42:31 +08:00
|
|
|
|
"mirai-api-http端口无法使用:{}, 解决方案: https://github.com/RockChinQ/QChatGPT/issues/22".format(
|
2023-01-17 17:34:20 +08:00
|
|
|
|
e))
|
2023-01-04 12:01:26 +08:00
|
|
|
|
else:
|
2023-01-04 17:09:57 +08:00
|
|
|
|
logging.error(
|
2023-01-16 16:57:01 +08:00
|
|
|
|
"捕捉到未知异常:{}, 请前往 https://github.com/RockChinQ/QChatGPT/issues 查找或提issue".format(e))
|
2023-01-04 12:01:26 +08:00
|
|
|
|
known_exception_caught = True
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
qq_bot_thread = threading.Thread(target=run_bot_wrapper, args=(), daemon=True)
|
|
|
|
|
qq_bot_thread.start()
|
|
|
|
|
finally:
|
2023-01-04 17:09:57 +08:00
|
|
|
|
time.sleep(12)
|
2023-01-04 12:01:26 +08:00
|
|
|
|
if first_time_init:
|
|
|
|
|
if not known_exception_caught:
|
2023-01-04 17:09:57 +08:00
|
|
|
|
logging.info('程序启动完成,如长时间未显示 ”成功登录到账号xxxxx“ ,并且不回复消息,请查看 '
|
|
|
|
|
'https://github.com/RockChinQ/QChatGPT/issues/37')
|
2023-01-04 12:01:26 +08:00
|
|
|
|
else:
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
else:
|
|
|
|
|
logging.info('热重载完成')
|
2022-12-09 00:01:49 +08:00
|
|
|
|
|
2023-02-10 19:03:25 +08:00
|
|
|
|
# 发送赞赏码
|
|
|
|
|
if hasattr(config, 'encourage_sponsor_at_start') \
|
|
|
|
|
and config.encourage_sponsor_at_start \
|
|
|
|
|
and pkg.utils.context.get_openai_manager().audit_mgr.get_total_text_length() >= 2048:
|
|
|
|
|
|
|
|
|
|
logging.info("发送赞赏码")
|
|
|
|
|
from mirai import MessageChain, Plain, Image
|
|
|
|
|
import pkg.utils.constants
|
|
|
|
|
message_chain = MessageChain([
|
|
|
|
|
Plain("自2022年12月初以来,开发者已经花费了大量时间和精力来维护本项目,如果您觉得本项目对您有帮助,欢迎赞赏开发者,"
|
|
|
|
|
"以支持项目稳定运行😘"),
|
|
|
|
|
Image(base64=pkg.utils.constants.alipay_qr_b64),
|
|
|
|
|
Image(base64=pkg.utils.constants.wechat_qr_b64),
|
2023-02-14 23:07:16 +08:00
|
|
|
|
Plain("BTC: 3N4Azee63vbBB9boGv9Rjf4N5SocMe5eCq\nXMR: 89LS21EKQuDGkyQoe2nDupiuWXk4TVD6FALvSKv5owfmeJEPFpHeMsZLYtLiJ6GxLrhsRe5gMs6MyMSDn4GNQAse2Mae4KE\n\n"),
|
2023-02-10 19:03:25 +08:00
|
|
|
|
Plain("(本消息仅在启动时发送至管理员,如果您不想再看到此消息,请在config.py中将encourage_sponsor_at_start设置为False)")
|
|
|
|
|
])
|
|
|
|
|
pkg.utils.context.get_qqbot_manager().notify_admin_message_chain(message_chain)
|
|
|
|
|
|
2023-01-05 20:52:59 +08:00
|
|
|
|
time.sleep(5)
|
|
|
|
|
import pkg.utils.updater
|
|
|
|
|
try:
|
|
|
|
|
if pkg.utils.updater.is_new_version_available():
|
|
|
|
|
pkg.utils.context.get_qqbot_manager().notify_admin("新版本可用,请发送 !update 进行自动更新")
|
|
|
|
|
else:
|
|
|
|
|
logging.info("当前已是最新版本")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2023-01-11 12:22:04 +08:00
|
|
|
|
logging.warning("检查更新失败:{}".format(e))
|
2023-01-05 20:52:59 +08:00
|
|
|
|
|
2022-12-08 12:06:04 +08:00
|
|
|
|
while True:
|
|
|
|
|
try:
|
2023-01-03 21:08:15 +08:00
|
|
|
|
time.sleep(10)
|
2023-01-02 00:35:36 +08:00
|
|
|
|
if qqbot != pkg.utils.context.get_qqbot_manager(): # 已经reload了
|
2023-01-02 12:00:10 +08:00
|
|
|
|
logging.info("以前的main流程由于reload退出")
|
2023-01-02 00:35:36 +08:00
|
|
|
|
break
|
2022-12-08 12:06:04 +08:00
|
|
|
|
except KeyboardInterrupt:
|
2023-01-02 00:35:36 +08:00
|
|
|
|
stop()
|
|
|
|
|
|
2022-12-08 12:06:04 +08:00
|
|
|
|
print("程序退出")
|
2022-12-15 17:52:41 +08:00
|
|
|
|
sys.exit(0)
|
2022-12-09 15:44:49 +08:00
|
|
|
|
|
|
|
|
|
|
2023-01-02 00:35:36 +08:00
|
|
|
|
def stop():
|
|
|
|
|
import pkg.utils.context
|
|
|
|
|
import pkg.qqbot.manager
|
|
|
|
|
import pkg.openai.session
|
|
|
|
|
try:
|
2023-01-13 16:49:56 +08:00
|
|
|
|
import pkg.plugin.host
|
|
|
|
|
pkg.plugin.host.unload_plugins()
|
|
|
|
|
|
2023-01-02 00:35:36 +08:00
|
|
|
|
qqbot_inst = pkg.utils.context.get_qqbot_manager()
|
|
|
|
|
assert isinstance(qqbot_inst, pkg.qqbot.manager.QQBotManager)
|
|
|
|
|
|
|
|
|
|
for session in pkg.openai.session.sessions:
|
|
|
|
|
logging.info('持久化session: %s', session)
|
|
|
|
|
pkg.openai.session.sessions[session].persistence()
|
2023-01-04 12:01:26 +08:00
|
|
|
|
pkg.utils.context.get_database_manager().close()
|
2023-01-02 00:35:36 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
if not isinstance(e, KeyboardInterrupt):
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
2022-12-09 15:44:49 +08:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# 检查是否有config.py,如果没有就把config-template.py复制一份,并退出程序
|
|
|
|
|
if not os.path.exists('config.py'):
|
|
|
|
|
shutil.copy('config-template.py', 'config.py')
|
|
|
|
|
print('请先在config.py中填写配置')
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2023-01-07 16:50:34 +08:00
|
|
|
|
# 检查是否有banlist.py,如果没有就把banlist-template.py复制一份
|
|
|
|
|
if not os.path.exists('banlist.py'):
|
|
|
|
|
shutil.copy('banlist-template.py', 'banlist.py')
|
|
|
|
|
|
2022-12-09 15:44:49 +08:00
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'init_db':
|
|
|
|
|
init_db()
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2023-01-01 20:46:25 +08:00
|
|
|
|
elif len(sys.argv) > 1 and sys.argv[1] == 'update':
|
|
|
|
|
try:
|
2023-01-16 21:34:35 +08:00
|
|
|
|
try:
|
|
|
|
|
import pkg.utils.pkgmgr
|
|
|
|
|
pkg.utils.pkgmgr.ensure_dulwich()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2023-01-01 20:46:25 +08:00
|
|
|
|
from dulwich import porcelain
|
2023-01-04 17:09:57 +08:00
|
|
|
|
|
2023-01-01 21:01:09 +08:00
|
|
|
|
repo = porcelain.open_repo('.')
|
2023-01-01 20:46:25 +08:00
|
|
|
|
porcelain.pull(repo)
|
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
|
print("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2023-01-04 17:09:57 +08:00
|
|
|
|
# import pkg.utils.configmgr
|
|
|
|
|
#
|
|
|
|
|
# pkg.utils.configmgr.set_config_and_reload("quote_origin", False)
|
2023-01-05 20:52:59 +08:00
|
|
|
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
2023-01-02 00:35:36 +08:00
|
|
|
|
main(True)
|