Skip to content

Commit

Permalink
刷新access token,添加频道支持自动初始化人设,修复入群事件bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nek0us committed Jul 16, 2024
1 parent 994010f commit 6d01d96
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 优化添加人设名称识别
Expand Down
31 changes: 21 additions & 10 deletions nonebot_plugin_gpt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions nonebot_plugin_gpt/api.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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')
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down

0 comments on commit 6d01d96

Please sign in to comment.