fix: 修复插件启用忽略前缀之后可能的逻辑冲突

This commit is contained in:
Soulter
2024-08-10 03:24:53 -04:00
parent 1df83addfc
commit f02731055e
4 changed files with 34 additions and 19 deletions
+7 -2
View File
@@ -134,8 +134,8 @@ class MessageHandler():
self.persist_manager.record_message(message.platform.platform_name, message.session_id)
# TODO: this should be configurable
if not message.message_str:
return MessageResult("Hi~")
# if not message.message_str:
# return MessageResult("Hi~")
# check the rate limit
if not self.rate_limit_helper.check_frequency(message.message_obj.sender.user_id):
@@ -158,6 +158,11 @@ class MessageHandler():
use_t2i=cmd_res.is_use_t2i
)
# next is the LLM part
if message.only_command:
return
# check if the message is a llm-wake-up command
if self.llm_wake_prefix and not msg_plain.startswith(self.llm_wake_prefix):
logger.debug(f"消息 `{msg_plain}` 没有以 LLM 唤醒前缀 `{self.llm_wake_prefix}` 开头,忽略。")
+10 -7
View File
@@ -106,23 +106,24 @@ class AIOCQHTTP(Platform):
# if message chain contains Plain components or
# At components which points to self_id, return True
if message.type == MessageType.FRIEND_MESSAGE:
return True
return True, "friend"
for comp in message.message:
if isinstance(comp, At) and str(comp.qq) == message.self_id:
return True
return True, "at"
# check commands which ignore prefix
if self.context.command_manager.check_command_ignore_prefix(message.message_str):
return True
return True, "command"
# check nicks
if self.check_nick(message.message_str):
return True
return False
return True, "nick"
return False, "none"
async def handle_msg(self, message: AstrBotMessage):
logger.info(
f"{message.sender.nickname}/{message.sender.user_id} -> {self.parse_message_outline(message)}")
if not self.pre_check(message):
ok, reason = self.pre_check(message)
if not ok:
return
# 解析 role
@@ -148,7 +149,9 @@ class AIOCQHTTP(Platform):
self.context,
"aiocqhttp",
message.session_id,
role, unified_msg_origin)
role,
unified_msg_origin,
reason == "command") # only_command
# transfer control to message handler
message_result = await self.message_handler.handle(ame)
+9 -7
View File
@@ -74,17 +74,17 @@ class QQGOCQ(Platform):
def pre_check(self, message: AstrBotMessage) -> bool:
# if message chain contains Plain components or At components which points to self_id, return True
if message.type == MessageType.FRIEND_MESSAGE:
return True
return True, "friend"
for comp in message.message:
if isinstance(comp, At) and str(comp.qq) == message.self_id:
return True
return True, "at"
# check commands which ignore prefix
if self.context.command_manager.check_command_ignore_prefix(message.message_str):
return True
return True, "command"
# check nicks
if self.check_nick(message.message_str):
return True
return False
return True, "nick"
return False, "none"
def run(self):
coro = self.client._run()
@@ -98,7 +98,8 @@ class QQGOCQ(Platform):
(GroupMessage, FriendMessage, GuildMessage))
# 判断是否响应消息
if not self.pre_check(message):
ok, reason = self.pre_check(message)
if not ok:
return
# 解析 session_id
@@ -142,7 +143,8 @@ class QQGOCQ(Platform):
"nakuru",
session_id,
role,
unified_msg_origin)
unified_msg_origin,
reason == 'command') # only_command
# transfer control to message handler
message_result = await self.message_handler.handle(ame)
+8 -3
View File
@@ -20,7 +20,8 @@ class AstrMessageEvent():
role: str,
context: Context,
session_id: str = None,
unified_msg_origin: str = None):
unified_msg_origin: str = None,
only_command: bool = False):
'''
AstrBot 消息事件。
@@ -31,6 +32,7 @@ class AstrMessageEvent():
`context`: 全局对象
`session_id`: 会话id
`unified_msg_origin`: 统一消息来源
`only_command`: 是否只处理指令,而不使用 LLM 回复
'''
self.context = context
self.message_str = message_str
@@ -39,13 +41,15 @@ class AstrMessageEvent():
self.role = role
self.session_id = session_id
self.unified_msg_origin = unified_msg_origin
self.only_command = only_command
def from_astrbot_message(message: AstrBotMessage,
context: Context,
platform_name: str,
session_id: str,
role: str = "member",
unified_msg_origin: str = None):
unified_msg_origin: str = None,
only_command: bool = False):
ame = AstrMessageEvent(message.message_str,
message,
@@ -53,6 +57,7 @@ class AstrMessageEvent():
role,
context,
session_id,
unified_msg_origin)
unified_msg_origin,
only_command=only_command)
return ame