From cf07a5220016e91dade31aae8ba4d778a0c84190 Mon Sep 17 00:00:00 2001 From: oldpear <1453599706@qq.com> Date: Mon, 3 Nov 2025 06:33:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4xyit=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=A0=81=E5=9C=A8ncatbot?= =?UTF-8?q?=204.x=E6=97=B6=E6=8A=A5=E9=94=99=E7=9A=84bug=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.toml | 31 ++++++++-------- control/group.py | 9 +++-- control/notice.py | 19 +++------- control/private.py | 3 +- control/request.py | 17 ++------- main.py | 62 ++++++++++++++++++-------------- model/ai_model.py | 3 -- model/ai_models/xyit.py | 76 ---------------------------------------- ncatbot.yaml | 25 +++++++++++++ uuid.db | Bin 16384 -> 0 bytes 10 files changed, 90 insertions(+), 155 deletions(-) delete mode 100644 model/ai_models/xyit.py create mode 100644 ncatbot.yaml delete mode 100644 uuid.db diff --git a/config.toml b/config.toml index cae5c1e..292ab00 100644 --- a/config.toml +++ b/config.toml @@ -1,18 +1,25 @@ -#napcat配置 +# ncatbot 配置 + +# 此处仅提供ncatbot部分配置项,其余配置项请在ncatbot.yaml中修改 +# 此处配置优先级高于ncatbot.yaml +# 请务必保证bot_qq及root_qq配置正确 +# 在ws监听IP为0.0.0.0时要求ws_token为强密码,启用web_ui(enable_webui_interaction = True)检查时要求webui_token为强密码 +# 强密码要求: 1,至少 12 位字符 2,包含数字、字母和特殊符号(特殊符号包括:!@#$%^&*()_+-=[]{}|;:,.<>?) + bot_qq = 123456 #机器人q号 root_qq = 1234567 # 管理员q号 -ws_uri = "loacalhost:3001" # ws 地址, 可自定义端口, 默认 3001 -webui_uri = "loacalhost:6099" # webui 地址, 可自定义端口, 默认 6099 +ws_uri = "localhost:3001" # ws 地址, 可自定义端口, 默认 3001 +webui_uri = "localhost:6099" # webui 地址, 可自定义端口, 默认 6099 webui_token = "napcat" # webui 令牌, 默认 napcat ws_token = "" # ws_uri 令牌, 默认留空 ws_listen_ip = "localhost" # ws_uri 监听 ip, 默认 localhost 监听本机,监听全部则配置 0.0.0.0 -remote_mode = false # 是否远程模式, 即 NapCat 服务不在本机运行 ps:ncatbot官方已废弃该参数 +enable_webui_interaction = true # 是否启用webui交互,默认启用 # 功能配置 allowed_groups = "all" # 授权群聊,all为全部,eg:[123456789, 987654321] allowed_users = "all" # 授权用户,all为全部,eg:[123456789, 987654321] -ai_service = "xyit" # ai平台 支持 "dify" "mcunc" "xyit" "aliyun" -friend_auto = false # 好友自动同意 +ai_service = "" # ai平台 支持 "dify" "mcunc" "aliyun" +friend_auto = false # 好友自动同意,实验性功能 group_auto = true group_welcome = false # 入群欢迎 group_leave = false # 退群提醒 @@ -22,14 +29,14 @@ group_welcome_message = "!at 欢迎加入本群,使用@bot /help获取此bot group_leave_message = "用户{userid}退群了" # 退群消息 ,{userid}为退群用户id error_message = "服务器繁忙,请稍后再逝" permission_denied_message = "此bot未在该群启用" -status_message = "---猫娘 QBOT---\n Q bot 运行正常 \n 版本: 2.0 pre \n © 无尽创意MCUNC" +status_message = "---chatbot ncapcat---\n Q bot 运行正常 \n 版本: 2.0 pre \n © 无尽创意MCUNC" help_message = "直接输入聊天内容即可 \n /help -- 获取帮助 \n /clear [群号 / private:Q号] (all 为全部,不填为本 群/用户) \n /status -- 查看bot状态" command_isnone_message = "你似乎没有提供想和我聊的内容喵~ \n 直接输入聊天内容即可" command_notfound_message = "指令不存在,输入/help查看帮助" chatmessage_isnone_message = "你似乎没有提供想和我聊的内容喵~ \n 格式:/cat <提问内容>" clean_all_success_message = "✅ 已清空所有群组数据" -clean_one_success_message = "✅ 已成功删除 group_id = {group_id} 的数据" -clean_one_notfound_message = "⚠️ 没有找到 group_id = {group_id} 的数据" +clean_one_success_message = "✅ 已成功删除 group_id = {group_id} 的数据" # {group_id}为群组id +clean_one_notfound_message = "⚠️ 没有找到 group_id = {group_id} 的数据" # {group_id}为群组id clean_nopermissoin_message = "你不是管理员哦喵~" clean_fail_message = "❌ 数据库操作失败" @@ -51,12 +58,6 @@ mcunc_appID = "" # appID mcunc_appKEY = "" # appKEY mcunc_model = "maoniang" # 模型名称名称 -# xyit配置 ai_service选择xyit时需配置 -xyit_ip = "https://ai.openapi.xyit.net" # ip 此项一般不需用修改 -xyit_appID = "" # appID -xyit_appKEY = "" # appKEY -xyit_model = "maoniang" # 模型名称 - # 阿里云百炼配置 ai_service选择aliyun时需配置 aliyun_app_id = "" aliyun_api_key = "" \ No newline at end of file diff --git a/control/group.py b/control/group.py index 0347725..bdf1cc6 100644 --- a/control/group.py +++ b/control/group.py @@ -1,4 +1,4 @@ -import logging +from ncatbot.core.event.message_segment import At, Text from model import ai_model from model.Clear import Clear import toml @@ -7,6 +7,7 @@ from model.logger import setup_logger logger = setup_logger() class group: def __init__(self, msg): + self.self_id = msg.self_id self.user_id = msg.user_id self.group_id = msg.group_id self.message_id = msg.message_id @@ -14,8 +15,6 @@ class group: self.raw_message = msg.raw_message self.sender = msg.sender self.message = msg.message - self.self_id = msg.self_id - self.time = msg.time try: with open("config.toml", "r", encoding="utf-8") as f: config = toml.load(f) @@ -44,8 +43,8 @@ class group: def is_at(self): for seg in self.message: - if seg['type'] == 'at' and seg['data'].get('qq') == str(self.self_id): - texts = [s['data']['text'].strip() for s in self.message if s['type'] == 'text'] + if isinstance(seg, At) and seg.qq == str(self.self_id): + texts = [s.text.strip() for s in self.message if isinstance(s, Text)] full_text = ' '.join(texts).strip() return full_text diff --git a/control/notice.py b/control/notice.py index 200ad80..ac23d53 100644 --- a/control/notice.py +++ b/control/notice.py @@ -5,18 +5,11 @@ from model.logger import setup_logger logger = setup_logger() class notice: def __init__(self,msg): - print(msg) - if msg["notice_type"] == "group_increase" or msg["notice_type"] == "group_decrease": - self.time = msg["time"] - self.self_id = msg["self_id"] - self.post_type = msg["post_type"] - self.notice_type = msg["notice_type"] - self.sub_type = msg["sub_type"] - self.group_id = msg["group_id"] - self.operator_id = msg["operator_id"] - self.user_id = msg["user_id"] - else: - pass + self.time = msg.time + self.notice_type = msg.notice_type + self.group_id = msg.group_id + self.operator_id = msg.operator_id + self.user_id = msg.user_id def main(self): if self.notice_type == "group_increase": @@ -27,7 +20,6 @@ class notice: return None def group_increase(self): - print(1) try: with open("./config.toml", "r", encoding="utf-8") as f: config = toml.load(f) @@ -52,7 +44,6 @@ class notice: config = toml.load(f) group_leave = config.get("group_leave") group_leave_message = config.get("group_leave_message") - print(group_leave_message) if group_leave: if "{userid}" in group_leave_message: return group_leave_message.replace("{userid}", str(self.user_id)) diff --git a/control/private.py b/control/private.py index 55fead6..8b60a19 100644 --- a/control/private.py +++ b/control/private.py @@ -1,3 +1,4 @@ +from ncatbot.core.event.message_segment import Text from model.logger import setup_logger from model.Clear import Clear from model import ai_model @@ -30,7 +31,7 @@ class private: logger.error(f"读取配置文件出现错误{e}") def main(self): - texts = [seg['data']['text'].strip() for seg in self.message if seg['type'] == 'text'] + texts = [seg.text.strip() for seg in self.message if isinstance(seg, Text)] full_text = ' '.join(texts).strip() permission = self.check_permission() if permission is None: diff --git a/control/request.py b/control/request.py index 9dfa6c5..3cf4a55 100644 --- a/control/request.py +++ b/control/request.py @@ -5,12 +5,9 @@ import toml logger = setup_logger() class request: def __init__(self,msg): - self.time = msg.time self.self_id = msg.self_id self.request_type = msg.request_type - self.sub_type = msg.sub_type - self.group_id = msg.group_id - self.user_id = msg.user_id + self.post_type = msg.post_type self.comment = msg.comment self.flag = msg.flag @@ -31,14 +28,4 @@ class request: return friend_auto except Exception as e: logger.error(f"读取配置文件错误:{e}") - return False - - def get_allow_group(self): - try: - with open("./config.toml", "r", encoding="utf-8") as f: - config = toml.load(f) - allow_group = config.get("allowed_groups") - return allow_group - except Exception as e: - logger.error(f"读取配置文件错误:{e}") - return [] + return False \ No newline at end of file diff --git a/main.py b/main.py index fa85a0f..53aef4a 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,17 @@ -from ncatbot.core.notice import NoticeMessage -from ncatbot.core import BotClient, Request +import os +os.environ['NCATBOT_CONFIG_PATH'] = 'ncatbot.yaml' +import toml +from ncatbot.core.event import PrivateMessageEvent, GroupMessageEvent, NoticeEvent, RequestEvent +from ncatbot.utils import ncatbot_config +from ncatbot.core import BotClient from model.logger import setup_logger -import asyncio from control.group import group from control.private import private from control.request import request from control.notice import notice -import toml - logger = setup_logger() + with open("./config.toml", "r", encoding="utf-8") as f: config = toml.load(f) bt_uin = config.get("bot_qq") @@ -19,13 +21,21 @@ with open("./config.toml", "r", encoding="utf-8") as f: webui_token = config.get("webui_token") ws_token = config.get("ws_token") ws_listen_ip = config.get("ws_listen_ip") - remote_mode = config.get("remote_mode") + enable_webui_interaction = config.get("enable_webui_interaction") + + ncatbot_config.set_bot_uin(bt_uin) + ncatbot_config.set_root(root) + ncatbot_config.set_webui_uri(webui_uri) + ncatbot_config.set_webui_token(webui_token) + ncatbot_config.set_ws_uri(ws_uri) + ncatbot_config.set_ws_token(ws_token) + ncatbot_config.set_ws_listen_ip(ws_listen_ip) + ncatbot_config.enable_webui_interaction = enable_webui_interaction bot = BotClient() -api = bot.run_blocking(bt_uin=bt_uin, root=root, ws_uri=ws_uri, webui_uri=webui_uri, webui_token=webui_token, ws_token=ws_token, ws_listen_ip=ws_listen_ip, remote_mode=remote_mode) -@bot.group_event() -async def on_group_message(msg): +@bot.on_group_message() +async def on_group_message(msg: GroupMessageEvent): logger.info(f"收到消息:{msg.raw_message},来自{msg.group_id}群聊{msg.user_id}用户") if msg.user_id == 2854196310: # qq管家,防止刷屏。bot大战请看 pass @@ -35,11 +45,11 @@ async def on_group_message(msg): if return_message is None: return else: - logger.info(f"返回消息:{return_message}") - await bot.api.post_group_msg(group_id=msg.group_id, text=return_message, reply=msg.message_id) + # logger.info(f"返回消息:{return_message}") + await msg.reply(text=return_message) -@bot.private_event() -async def on_private_message(msg): +@bot.on_private_message() +async def on_private_message(msg: PrivateMessageEvent): logger.info(f"收到消息:{msg.raw_message},来自{msg.user_id}用户") if msg.user_id == 2854196310: # qq管家,防止刷屏。 pass @@ -49,36 +59,36 @@ async def on_private_message(msg): if return_message is None: return else: - logger.info(f"返回消息:{return_message}") - await bot.api.post_private_msg(user_id=msg.user_id, text=return_message, reply=msg.message_id) + # logger.info(f"返回消息:{return_message}") + await msg.reply(text=return_message) -@bot.request_event() -async def on_request_event(msg: Request): +@bot.on_request(filter='friend') +async def on_request_event(msg: RequestEvent): logger.info(f"收到request事件:{msg.request_type},来自{msg.group_id}群聊,{msg.user_id}用户,验证消息:{msg.comment}") + logger.info(f"收到request请求,来自{msg.self_id}") ctrl = request(msg) accept_friend_application = ctrl.main() if accept_friend_application is True: - await msg.reply(True, comment="请求已通过") - logger.info("请求已通过") + await msg.reply(True) + # logger.info("请求已通过") else: - await msg.reply(False, comment="请求被拒绝") - logger.info("请求被拒绝") + await msg.reply(False) + # logger.info("请求被拒绝") -@bot.notice_event() -async def on_notice_event(msg: NoticeMessage): +@bot.on_notice() +async def on_notice_event(msg: NoticeEvent): logger.info(f"收到notice事件:{msg['notice_type']},来自{msg['user_id']}用户") ctrl = notice(msg) return_message = ctrl.main() if return_message is None: return else: - logger.info(f"返回消息:{return_message}") + # logger.info(f"返回消息:{return_message}") await bot.api.post_group_msg(group_id=msg["group_id"], text=return_message) +bot.run_frontend() - -asyncio.get_event_loop().run_forever() \ No newline at end of file diff --git a/model/ai_model.py b/model/ai_model.py index 9c79fd7..fd28f70 100644 --- a/model/ai_model.py +++ b/model/ai_model.py @@ -24,9 +24,6 @@ def main(message,qid,group_openid): elif ai_service == "mcunc": from model.ai_models import mcunc return mcunc.main(message, group_openid) - elif ai_service == "xyit": - from model.ai_models import xyit - return xyit.main(message, group_openid) elif ai_service == "aliyun": from model.ai_models import aliyun return aliyun.main(message, group_openid) diff --git a/model/ai_models/xyit.py b/model/ai_models/xyit.py deleted file mode 100644 index 6888ae4..0000000 --- a/model/ai_models/xyit.py +++ /dev/null @@ -1,76 +0,0 @@ -import requests -import json -from model.logger import setup_logger -import toml -from model.sql_tools import init_db -from model.sql_tools import get_uuid -from model.sql_tools import add_uuid - - - -logger = setup_logger() -def main(message, group_openid): - with open('./config.toml', 'r', encoding='utf-8') as f: - config = toml.load(f) - - error_message = config.get("error_message") - - # xyit - xyit_ip = config.get("xyit_ip") - xyit_appID = config.get("xyit_appID") - xyit_appKEY = config.get("xyit_appKEY") - xyit_model = config.get("xyit_model") - - init_db() - - uuid = get_uuid(group_openid) - - if uuid == "": - logger.info("未找到 UUID") - else: - logger.info(f"找到 UUID: {uuid}") - - url = f"{xyit_ip}/models/{xyit_model}/" # 替换为实际的 API 地址 - - # 请求头 - headers = { - "Content-Type": "application/json", - "appID": xyit_appID, # 替换为你的 API 密钥 - "appKEY": xyit_appKEY - } - - # 请求体 - payload = { - "query": message, # 用户输入/提问内容 - "conversation_id": uuid, # (选填)会话 ID,继续对话时需要传入 - } - - logger.info("请求平台:xyit") - - - # 发送 POST 请求 - try: - response = requests.post(url, headers=headers, data=json.dumps(payload)) - - # 检查响应状态码 - if response.status_code == 200: - logger.info("请求成功!返回结果:") - response_data = response.json() - print(response_data) # test - # 存储uuid - if uuid == "": - uuid = response_data.get("conversation_id") - add_uuid(group_openid, uuid) - - # 提取 answer 值 - answer = response_data.get("answer", error_message) - return answer - else: - logger.info(f"请求失败!状态码: {response.status_code}") - logger.info(f"错误信息: {response.text}") # 打印错误信息 - return error_message - - except Exception as e: - logger.info(f"请求过程中出现异常: {e}") - return error_message - diff --git a/ncatbot.yaml b/ncatbot.yaml new file mode 100644 index 0000000..578bb0a --- /dev/null +++ b/ncatbot.yaml @@ -0,0 +1,25 @@ +root: '1453599706' +bt_uin: '3917995084' +enable_webui_interaction: null +debug: false +github_proxy: null +check_ncatbot_update: true +skip_ncatbot_install_check: false +websocket_timeout: 15 +napcat: + ws_uri: ws://sz.mcunc.site:13001 + ws_token: 79_t&_e@WJ#HgXXp + ws_listen_ip: 0.0.0.0 + webui_uri: http://sz.mcunc.site:26099 + webui_token: U16z8RnYNls]W[N| + enable_webui: true + check_napcat_update: false + stop_napcat: false + remote_mode: false + report_self_message: false + report_forward_message_detail: true +plugin: + plugins_dir: plugins + plugin_whitelist: [] + plugin_blacklist: [] + skip_plugin_load: false diff --git a/uuid.db b/uuid.db deleted file mode 100644 index 77665493e3cd64bc91be25ece043df5041f95306..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI(O>YuG7zgm#E~%zTJH}WDy>yOk=rl9C!|rS^ie_Vol(x8}_C%I_QIZyD3yoLf z$%_}iQ!l0;!NkvCJa}{{(!?rvWB*SWh8gC0*!}HZ7{begMsJj{d~kLWkC;my5X&M@ z7$byin^rJw7M6K&dpvt@UcXJYFFq{OAH-hyNLIem&!$5_00Izz00bZa0SG_<0uX?} zofp`*7fY3jbu@~*$JzPmvH5D+InPeVSwFcwEIez4wJ2m!?P()qx7Kd-R4h3OpISYmUcm$G^@3ZI89w%@An)>=pGMR>$&?P#yw zG^KXKX0$ao`?`E*FwFX$?=pntLmB}0e0uX=z1Rwwb2tWV= z5P$##AaI8T*}u5O7J-GW3IB;=Q>qB&sE?AJH-N#?crJPA|7Qs zj$75r@nls9H`aMjO?VpkK36Jrc-I%2J6gDzaMGmfIC`}}_)V@9p6B|$o4AS8A{RUl z63JDX2RzV$;wl#+2$VSn*OhCNBMdy{2US;!*z0Dgu6o=NsacpvkH?Pmxnrs+?WH1( z#ge(8HoYYD8~s8r>D3*+zBqLVKmY;|fB*y_009U<00Izzz`rW6Y_D5pN1|v;>(9-G z;v<{iOwaZaRwpSpTLf#9-~TQ8otOs#0uX=z1Rwwb2tWV=5P$##AOL~?NMOmb%hvRN H0Bb)1$*8;F