🎨 优化to_me的判定

This commit is contained in:
‘KimigaiiWuyi’ 2023-03-30 15:29:56 +08:00
parent 29e6ab5a17
commit 90f2e0c9ac
4 changed files with 16 additions and 22 deletions

View File

@ -1,12 +1,12 @@
import asyncio
from typing import Dict
from typing import Dict, List
from gsuid_core.sv import SL
from gsuid_core.bot import Bot, _Bot
from gsuid_core.logger import logger
from gsuid_core.trigger import Trigger
from gsuid_core.config import core_config
from gsuid_core.models import Event, MessageReceive
from gsuid_core.models import Event, Message, MessageReceive
config_masters = core_config.get_config('masters')
config_superusers = core_config.get_config('superusers')
@ -30,17 +30,23 @@ async def msg_process(msg: MessageReceive) -> Event:
msg.group_id,
msg.user_id,
msg.user_pm,
msg.content,
)
_content: List[Message] = []
for _msg in msg.content:
if _msg.type == 'text':
event.raw_text += _msg.data # type:ignore
event.raw_text += _msg.data.strip() # type:ignore
elif _msg.type == 'at':
if event.bot_self_id == _msg.data:
event.is_tome = True
continue
else:
event.at = _msg.data
event.at_list.append(_msg.data)
elif _msg.type == 'image':
event.image = _msg.data
event.image_list.append(_msg.data)
_content.append(_msg)
event.content = _content
return event
@ -78,20 +84,6 @@ async def handle_event(ws: _Bot, msg: MessageReceive):
sorted_event = sorted(valid_event.items(), key=lambda x: x[1])
for trigger, _ in sorted_event:
message = await trigger.get_command(event)
if event.bot_self_id in event.at_list:
event.at_list.remove(event.bot_self_id)
if event.bot_self_id == event.at:
if len(event.at_list) >= 1:
event.at = event.at_list[0]
else:
event.at = None
_content = []
for _c in event.content:
if _c.type == 'at' and _c.data == event.bot_self_id:
pass
else:
_content.append(_c)
event.content = _content
bot = Bot(ws, event)
logger.info(
f'↪ 消息 「{event.raw_text}」 触发'

View File

@ -27,6 +27,7 @@ class Event(MessageReceive):
at: Optional[str] = None
image_list: List[Any] = []
at_list: List[Any] = []
is_tome: bool = False
class MessageSend(Struct):

View File

@ -96,6 +96,7 @@ class SV:
type: Literal['prefix', 'suffix', 'keyword', 'fullmatch', 'command'],
keyword: Union[str, Tuple[str, ...]],
block: bool = False,
to_me: bool = False,
):
def deco(func: Callable) -> Callable:
keyword_list = keyword
@ -104,7 +105,7 @@ class SV:
for _k in keyword_list:
if _k not in self.TL:
logger.info(f'载入{type}触发器【{_k}】!')
self.TL[_k] = Trigger(type, _k, func, block)
self.TL[_k] = Trigger(type, _k, func, block, to_me)
@wraps(func)
async def wrapper(bot, msg) -> Optional[Callable]:

View File

@ -21,7 +21,7 @@ class Trigger:
def check_command(self, raw_msg: Event) -> bool:
msg = raw_msg.raw_text
if self.to_me:
if raw_msg.at_list and raw_msg.bot_self_id in raw_msg.at_list:
if raw_msg.is_tome:
pass
else:
return False