From 6d01d96e7359299dd2103ae7604835859660db39 Mon Sep 17 00:00:00 2001 From: nek0us <1130131059@qq.com> Date: Tue, 16 Jul 2024 22:58:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B7=E6=96=B0access=20token=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A2=91=E9=81=93=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BA=BA=E8=AE=BE=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=85=A5=E7=BE=A4=E4=BA=8B=E4=BB=B6bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ nonebot_plugin_gpt/__init__.py | 31 +++++++++++++++++++++---------- nonebot_plugin_gpt/api.py | 14 ++++++++++---- pyproject.toml | 4 ++-- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 965cdd3..8ee0c06 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,12 @@ C:\Users\UserName\AppData\Local\nonebot2\nonebot_plugin_gpt\\{bot_name\} > 还是由于时间关系,暂时没写与白名单相关适配,自动初始化人设若开启,优先级会比白名单高,例如非白名单群,入群也会在gpt账户上创建一个会话(3.5的会话),当然没白名单该群后续触发不了这个会话 ### 更新日志 +2024.07.16 0.0.36 +1. 修复0.0.35版本中,未正确捕获自身入群事件的问题 +2. 自动初始化人设添加频道相关支持 +3. 尝试修复长时间运行时,access_token过期未自动刷新的问题 + + 2024.07.15 0.0.35 1. 修复0.0.34造成的gpt plus账户会话失败的问题 2. 优化添加人设名称识别 diff --git a/nonebot_plugin_gpt/__init__.py b/nonebot_plugin_gpt/__init__.py index 2cba25b..fe98ef9 100644 --- a/nonebot_plugin_gpt/__init__.py +++ b/nonebot_plugin_gpt/__init__.py @@ -3,9 +3,10 @@ from nonebot.log import logger from nonebot import on_command,on_message,on_notice from nonebot.adapters.onebot.v11 import Message,MessageEvent,GroupIncreaseNoticeEvent,FriendAddNoticeEvent -from nonebot.adapters.qq.event import MessageEvent as QQMessageEvent,GroupAddRobotEvent,FriendAddEvent +from nonebot.adapters.qq.event import MessageEvent as QQMessageEvent,GroupAddRobotEvent,FriendAddEvent,GuildMemberUpdateEvent from nonebot.adapters.qq.message import Message as QQMessage -from nonebot.matcher import Matcher +from nonebot.adapters.qq import Bot +from nonebot.matcher import Matcher,current_bot from nonebot.params import Arg, CommandArg,EventMessage from nonebot.plugin import PluginMetadata from nonebot.typing import T_State @@ -282,7 +283,7 @@ async def del_checker_handle(event: QQMessageEvent): init_personal = on_notice(block=False,priority=config_gpt.gpt_chat_priority) @init_personal.handle() - async def init_personal_handle(event: GroupAddRobotEvent|FriendAddNoticeEvent|GroupIncreaseNoticeEvent|FriendAddEvent): + async def init_personal_handle(event: GroupAddRobotEvent|FriendAddNoticeEvent|GroupIncreaseNoticeEvent|FriendAddEvent|GuildMemberUpdateEvent): if isinstance(event,GroupAddRobotEvent): # QQ群 if config_gpt.gpt_auto_init_group: @@ -301,12 +302,13 @@ async def init_personal_handle(event: GroupAddRobotEvent|FriendAddNoticeEvent|Gr await init_personal_api(chatbot,id=event.get_user_id(),personal_name=config_gpt.gpt_init_friend_pernal_name,type_from='private') elif isinstance(event,GroupIncreaseNoticeEvent): # onebot 群 - if config_gpt.gpt_auto_init_group: - if not config_gpt.gpt_init_group_pernal_name: - logger.warning(f"检测到已开启入群初始化人设,但未配置具体人设名,类型 GroupIncreaseNoticeEvent, id: {str(event.group_id)} 入群初始化人设失败") - else: - logger.info(f"检测到已开启入群初始化人设,类型 GroupIncreaseNoticeEvent, id: {str(event.group_id)} 即将入群初始化人设 {config_gpt.gpt_init_group_pernal_name}") - await init_personal_api(chatbot,id=str(event.group_id),personal_name=config_gpt.gpt_init_group_pernal_name,type_from='group') + if event.get_user_id() == str(event.self_id): + if config_gpt.gpt_auto_init_group: + if not config_gpt.gpt_init_group_pernal_name: + logger.warning(f"检测到已开启入群初始化人设,但未配置具体人设名,类型 GroupIncreaseNoticeEvent, id: {str(event.group_id)} 入群初始化人设失败") + else: + logger.info(f"检测到已开启入群初始化人设,类型 GroupIncreaseNoticeEvent, id: {str(event.group_id)} 即将入群初始化人设 {config_gpt.gpt_init_group_pernal_name}") + await init_personal_api(chatbot,id=str(event.group_id),personal_name=config_gpt.gpt_init_group_pernal_name,type_from='group') elif isinstance(event,FriendAddEvent): # QQ好友 if config_gpt.gpt_auto_init_friend: @@ -315,7 +317,16 @@ async def init_personal_handle(event: GroupAddRobotEvent|FriendAddNoticeEvent|Gr else: logger.info(f"检测到已开启好友初始化人设,类型 FriendAddEvent, id: {event.get_user_id()} 即将好友初始化人设 {config_gpt.gpt_init_friend_pernal_name}") await init_personal_api(chatbot,id=event.get_user_id(),personal_name=config_gpt.gpt_init_friend_pernal_name,type_from='QQprivate') - + elif isinstance(event,GuildMemberUpdateEvent): + # QQ频道 + bot: Bot = current_bot.get() # type: ignore + if bot.self_info.id == event.op_user_id: + if config_gpt.gpt_auto_init_group: + if not config_gpt.gpt_init_group_pernal_name: + logger.warning(f"检测到已开启频道初始化人设,但未配置具体人设名,类型 GuildMemberUpdateEvent, id: {event.guild_id} 频道初始化人设失败") + else: + logger.info(f"检测到已开启频道初始化人设,类型 GuildMemberUpdateEvent, id: {event.guild_id} 即将频道初始化人设 {config_gpt.gpt_init_group_pernal_name}") + await init_personal_api(chatbot,id=event.guild_id,personal_name=config_gpt.gpt_init_group_pernal_name,type_from='QQguild') else: diff --git a/nonebot_plugin_gpt/api.py b/nonebot_plugin_gpt/api.py index 8a37e1c..2869acb 100644 --- a/nonebot_plugin_gpt/api.py +++ b/nonebot_plugin_gpt/api.py @@ -1,8 +1,6 @@ from nonebot.adapters.onebot.v11 import Message,MessageSegment,MessageEvent,GroupMessageEvent,PrivateMessageEvent -from nonebot.adapters.onebot.v11 import GroupIncreaseNoticeEvent,FriendAddNoticeEvent -from nonebot.adapters.qq.event import GroupAddRobotEvent,FriendAddEvent from nonebot.matcher import Matcher,current_matcher,current_event from nonebot.params import EventMessage from ChatGPTWeb import chatgpt @@ -915,7 +913,15 @@ async def init_personal_api(chatbot: chatgpt,id: str,personal_name: str,type_fro logger.warning(f"默认初始化人格名: {personal_name} 不存在") return data = MsgData() - data = get_c_id(id=id,data=data,c_type='group' if 'group' in type_from else 'private') + if type_from == "QQguild": + data = get_c_id(id=id,data=data,c_type='group') + if data.conversation_id == 'pass': + logger.info(f"默认人设初始化类型:{type_from},id:{id},检测到疑似重复提示消息,不进行初始化") + return + data_tmp = MsgData(conversation_id='pass') + set_c_id(id,data_tmp,'group') + else: + data = get_c_id(id=id,data=data,c_type='group' if 'group' in type_from else 'private') if data.conversation_id: logger.info(f"默认人设初始化类型:{type_from},id:{id},存在默认会话id:{data.conversation_id},不进行新的初始化") return @@ -924,7 +930,7 @@ async def init_personal_api(chatbot: chatgpt,id: str,personal_name: str,type_fro if not data.msg_recv: logger.warning( f"默认人设初始化失败,类型:{type_from},id:{id},错误为:\n{data.error_info}") - if 'group' in type_from: + if 'group' in type_from or 'guild' in type_from: set_c_id(id,data,'group') else: set_c_id(id,data,'private') \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 6a3ac84..158c0de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,12 +1,12 @@ [project] name = "nonebot-plugin-gpt" -version = "0.0.35" +version = "0.0.36" description = "Nonebot2's plugin of ChatGPT " authors = [ {name = "nek0us", email = "nekouss@mail.com"}, ] license = {text = "GPL3"} -dependencies = ["ChatGPTWeb>=0.2.38","nonebot_adapter_onebot>=2.3.1","nonebot-plugin-sendmsg-by-bots>=0.1.6","nonebot_adapter_qq>=1.3.5","nonebot2>=2.0.0","more_itertools","nonebot_plugin_htmlrender","nonebot_plugin_localstore"] +dependencies = ["ChatGPTWeb>=0.2.39","nonebot_adapter_onebot>=2.3.1","nonebot-plugin-sendmsg-by-bots>=0.1.6","nonebot_adapter_qq>=1.3.5","nonebot2>=2.0.0","more_itertools","nonebot_plugin_htmlrender","nonebot_plugin_localstore"] requires-python = ">=3.10" readme = "README.md"