Merge pull request #423 from mobyw/Bot

🐛 Bug fix
This commit is contained in:
惜月 2023-04-16 16:28:18 +08:00 committed by GitHub
commit 5c35e06065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -10,7 +10,8 @@ from nonebot.params import CommandArg, ArgPlainText
from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent, PrivateMessageEvent, FriendRequestEvent, \ from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent, PrivateMessageEvent, FriendRequestEvent, \
GroupRequestEvent, \ GroupRequestEvent, \
RequestEvent, NoticeEvent, \ RequestEvent, NoticeEvent, \
GroupIncreaseNoticeEvent, FriendAddNoticeEvent, GroupMessageEvent GroupIncreaseNoticeEvent, FriendAddNoticeEvent, GroupMessageEvent, \
ActionFailed
from nonebot.typing import T_State from nonebot.typing import T_State
from LittlePaimon.config import config as bot_config from LittlePaimon.config import config as bot_config
@ -105,7 +106,10 @@ async def _(event: PrivateMessageEvent, bot: Bot, state: T_State, id_: str = Arg
@requests.handle() @requests.handle()
async def _(bot: Bot, event: FriendRequestEvent): async def _(bot: Bot, event: FriendRequestEvent):
done[f'add_friend_{event.user_id}'] = datetime.datetime.now() done[f'add_friend_{event.user_id}'] = datetime.datetime.now()
user_info = await bot.get_stranger_info(user_id=event.user_id) try:
user_info = await bot.get_stranger_info(user_id=event.user_id)
except ActionFailed:
user_info = {'nickname': '未知'}
base_msg = f'{user_info["nickname"]}({event.user_id})请求添加好友,验证信息为"{event.comment or ""}"' base_msg = f'{user_info["nickname"]}({event.user_id})请求添加好友,验证信息为"{event.comment or ""}"'
if bot_config.auto_add_friend: if bot_config.auto_add_friend:
await asyncio.sleep(random.randint(10, 20)) await asyncio.sleep(random.randint(10, 20))
@ -121,8 +125,14 @@ async def _(bot: Bot, event: FriendRequestEvent):
@requests.handle() @requests.handle()
async def _(bot: Bot, event: GroupRequestEvent): async def _(bot: Bot, event: GroupRequestEvent):
done[f'add_group_{event.group_id}'] = datetime.datetime.now() done[f'add_group_{event.group_id}'] = datetime.datetime.now()
user_info = await bot.get_stranger_info(user_id=event.user_id) try:
group_info = await bot.get_group_info(group_id=event.group_id) user_info = await bot.get_stranger_info(user_id=event.user_id)
except ActionFailed:
user_info = {'nickname': '未知'}
try:
group_info = await bot.get_group_info(group_id=event.group_id)
except ActionFailed:
group_info = {'group_name': '未知'}
base_msg = f'{user_info["nickname"]}({event.user_id})邀请{NICKNAME}加入群{group_info["group_name"]}({event.group_id})' base_msg = f'{user_info["nickname"]}({event.user_id})邀请{NICKNAME}加入群{group_info["group_name"]}({event.group_id})'
if bot_config.auto_add_group or event.user_id in SUPERUSERS: if bot_config.auto_add_group or event.user_id in SUPERUSERS:
await asyncio.sleep(random.randint(10, 20)) await asyncio.sleep(random.randint(10, 20))

View File

@ -11,13 +11,13 @@ class logger:
@staticmethod @staticmethod
def info(command: str, info: str = '', param: Dict[str, any] = None, result: str = '', result_type: bool = True): def info(command: str, info: str = '', param: Dict[str, any] = None, result: str = '', result_type: bool = True):
param_str = ' '.join([f'{k}<m>{escape_tag(v)}</m>' for k, v in param.items()]) if param else '' param_str = ' '.join([f'{k}<m>{escape_tag(str(v))}</m>' for k, v in param.items()]) if param else ''
result_str = f'<g>{escape_tag(result)}</g>' if result_type else f'<r>{escape_tag(result)}</r>' if result else '' result_str = f'<g>{escape_tag(result)}</g>' if result_type else f'<r>{escape_tag(result)}</r>' if result else ''
nb_logger.opt(colors=True).info(f'<u><y>[{command}]</y></u>{info}{param_str}{result_str}') nb_logger.opt(colors=True).info(f'<u><y>[{command}]</y></u>{info}{param_str}{result_str}')
@staticmethod @staticmethod
def success(command: str, info: str = '', param: Dict[str, any] = None, result: str = ''): def success(command: str, info: str = '', param: Dict[str, any] = None, result: str = ''):
param_str = ' '.join([f'{k}<m>{escape_tag(v)}</m>' for k, v in param.items()]) if param else '' param_str = ' '.join([f'{k}<m>{escape_tag(str(v))}</m>' for k, v in param.items()]) if param else ''
result_str = f'<g>{escape_tag(result)}</g>' if result else '' result_str = f'<g>{escape_tag(result)}</g>' if result else ''
nb_logger.opt(colors=True).success(f'<u><y>[{command}]</y></u>{info}{param_str}{result_str}') nb_logger.opt(colors=True).success(f'<u><y>[{command}]</y></u>{info}{param_str}{result_str}')