From d0801c431955deece67efe2eb73bea0b63bb5067 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 19 Mar 2023 09:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E8=BF=87=E6=97=B6=E7=9A=84bug,=E6=9A=82=E6=97=B6=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AF=B9=E9=A2=91=E9=81=93=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_dialectlist/__init__.py | 70 ++++++++++++------------ nonebot_plugin_dialectlist/config.py | 4 +- nonebot_plugin_dialectlist/function.py | 73 ++------------------------ 3 files changed, 42 insertions(+), 105 deletions(-) diff --git a/nonebot_plugin_dialectlist/__init__.py b/nonebot_plugin_dialectlist/__init__.py index 213e39c..3084fa6 100644 --- a/nonebot_plugin_dialectlist/__init__.py +++ b/nonebot_plugin_dialectlist/__init__.py @@ -1,7 +1,7 @@ import re import time import asyncio -from typing import List, Tuple, Union +from typing import Tuple, Union from datetime import datetime, timedelta try: @@ -17,13 +17,13 @@ from nonebot.matcher import Matcher from nonebot.adapters import Bot from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent, Message -require("nonebot_plugin_chatrecorder_guild_patch") -from nonebot_plugin_chatrecorder_guild_patch import get_guild_all_channel + require("nonebot_plugin_chatrecorder") +from nonebot_plugin_chatrecorder import get_message_records require("nonebot_plugin_guild_patch") from nonebot_plugin_guild_patch import GuildMessageEvent -from .function import get_message_records,msg_counter, msg_list2msg +from .function import msg_counter, msg_list2msg from .config import plugin_config def parse_datetime(key: str): @@ -104,6 +104,9 @@ async def _group_message( elif command == "昨日群话痨排行榜": state["stop"] = dt.replace(hour=0, minute=0, second=0, microsecond=0) state["start"] = state["stop"] - timedelta(days=1) + elif command == "前日群话痨排行榜": + state["stop"] = dt.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=1) + state["start"] = state["stop"] - timedelta(days=1) elif command == "本周群话痨排行榜": state["start"] = dt.replace( hour=0, minute=0, second=0, microsecond=0 @@ -159,45 +162,46 @@ async def _private_message( ) async def handle_message( bot: Bot, - event: Union[GroupMessageEvent, GuildMessageEvent], + event: GroupMessageEvent, #Union[GroupMessageEvent,GuildMessageEvent], stop: datetime = Arg(), start: datetime = Arg() ): st = time.time() - if isinstance(event,GroupMessageEvent): - if plugin_config.dialectlist_excluded_self: - bot_id = await bot.call_api('get_login_info') - plugin_config.dialectlist_excluded_people.append(str(bot_id["user_id"])) - gids:List[str] = [str(event.group_id)] - msg_list = await get_message_records( - group_ids=gids, - exclude_user_ids=plugin_config.dialectlist_excluded_people, - message_type='group', - time_start=start.astimezone(ZoneInfo("UTC")), - time_stop=stop.astimezone(ZoneInfo("UTC")) - ) + #if isinstance(event,GroupMessageEvent): + if plugin_config.dialectlist_excluded_self: + bot_id:dict = await bot.call_api('get_login_info') + plugin_config.dialectlist_excluded_people.append(bot_id['user_id']) + print(event.self_id) + msg_list = await get_message_records( + bot_ids=[str(bot.self_id)], + platforms=[str('qq')], + group_ids=[str(event.group_id)], + exclude_user_ids=plugin_config.dialectlist_excluded_people, + time_start=start.astimezone(ZoneInfo("UTC")), + time_stop=stop.astimezone(ZoneInfo("UTC")) + ) - elif isinstance(event, GuildMessageEvent): - if plugin_config.dialectlist_excluded_self: - bot_id = await bot.call_api('get_guild_service_profile') - plugin_config.dialectlist_excluded_people.append(str(bot_id["user_id"])) - guild_ids:List[str] = await get_guild_all_channel(event.guild_id,bot=bot) - msg_list = await get_message_records( - group_ids=guild_ids, - exclude_user_ids=plugin_config.dialectlist_excluded_people, - message_type='group', - time_start=start.astimezone(ZoneInfo("UTC")), - time_stop=stop.astimezone(ZoneInfo("UTC")) - ) + # elif isinstance(event, GuildMessageEvent): + # if plugin_config.dialectlist_excluded_self: + # bot_id = await bot.call_api('get_guild_service_profile') + # guild_ids:List[str] = [str(event.guild_id)] + # msg_list = await get_message_records( + # bot_ids=[str(bot.self_id)], + # platforms=['qqguild'], + # guild_ids=guild_ids, + # exclude_user_ids=plugin_config.dialectlist_excluded_people, + # time_start=start.astimezone(ZoneInfo("UTC")), + # time_stop=stop.astimezone(ZoneInfo("UTC")) + # ) msg_dict = await msg_counter(msg_list=msg_list) - if isinstance(event,GroupMessageEvent): - msg = await msg_list2msg(msg_list=msg_dict,gid=event.group_id,platform='qq',bot=bot,got_num=plugin_config.dialectlist_get_num) - elif isinstance(event, GuildMessageEvent): - msg = await msg_list2msg(msg_list=msg_dict,gid=event.guild_id,platform='guild',bot=bot,got_num=plugin_config.dialectlist_get_num) + # if isinstance(event,GroupMessageEvent): + msg = await msg_list2msg(msg_list=msg_dict,gid=event.group_id,platform='qq',bot=bot,got_num=plugin_config.dialectlist_get_num) + # elif isinstance(event, GuildMessageEvent): + # msg = await msg_list2msg(msg_list=msg_dict,gid=event.guild_id,platform='guild',bot=bot,got_num=plugin_config.dialectlist_get_num) await rankings.send(msg) await asyncio.sleep(1) #让图片先发出来 diff --git a/nonebot_plugin_dialectlist/config.py b/nonebot_plugin_dialectlist/config.py index d90bb98..584e236 100644 --- a/nonebot_plugin_dialectlist/config.py +++ b/nonebot_plugin_dialectlist/config.py @@ -7,8 +7,8 @@ class Config(BaseModel, extra=Extra.ignore): timezone: Optional[str] dialectlist_string_format: str = '第{index}名:\n{nickname},{chatdatanum}条消息\n' #消息格式 - dialectlist_string_suffix_format: Optional[str] = '你们的职业是水群吗?————MYX\n计算花费时间:{timecost}秒' #消息后缀格式 - dialectlist_get_num:int = 10 #获取人数数量 + dialectlist_string_suffix_format: Optional[str] = '数你们聊天记录都要花{timecost}秒,你看看你们多能聊!' #消息后缀格式 + dialectlist_get_num:int = 5 #获取人数数量 dialectlist_visualization:bool = True #是否可视化 dialectlist_visualization_type:Literal['饼图','圆环图','柱状图'] = '圆环图' #可视化方案 dialectlist_font:str = 'SimHei'#字体格式 diff --git a/nonebot_plugin_dialectlist/function.py b/nonebot_plugin_dialectlist/function.py index 38bf8a9..74a0c1d 100644 --- a/nonebot_plugin_dialectlist/function.py +++ b/nonebot_plugin_dialectlist/function.py @@ -1,10 +1,8 @@ import pygal import unicodedata -from datetime import datetime -from sqlmodel import select, or_ from typing_extensions import Literal -from typing import Iterable, List, Optional, Dict +from typing import List, Optional, Dict from pygal.style import Style from nonebot.log import logger @@ -12,7 +10,6 @@ from nonebot.adapters import Bot from nonebot.adapters.onebot.v11 import Message,MessageSegment from nonebot.exception import ActionFailed -from nonebot_plugin_datastore import create_session from nonebot_plugin_chatrecorder.model import MessageRecord from .config import plugin_config @@ -31,70 +28,6 @@ def remove_control_characters(string:str) -> str: return "".join(ch for ch in string if unicodedata.category(ch)[0]!="C") - -async def get_message_records( - user_ids: Optional[Iterable[str]] = None, - group_ids: Optional[Iterable[str]] = None, - platforms: Optional[Iterable[str]] = None, - exclude_user_ids: Optional[Iterable[str]] = None, - exclude_group_ids: Optional[Iterable[str]] = None, - message_type: Optional[Literal['private', 'group']] = None, - time_start: Optional[datetime] = None, - time_stop: Optional[datetime] = None, -)->List[MessageRecord]: - """ - :说明: - - 获取消息记录 - - :参数: - - * ``user_ids: Optional[Iterable[str]]``: 用户列表,为空表示所有用户 - * ``group_ids: Optional[Iterable[str]]``: 群组列表,为空表示所有群组 - * ``platform: OPtional[Iterable[str]]``: 消息来源列表,为空表示所有来源 - * ``exclude_user_ids: Optional[Iterable[str]]``: 不包含的用户列表,为空表示不限制 - * ``exclude_group_ids: Optional[Iterable[str]]``: 不包含的群组列表,为空表示不限制 - * ``message_type: Optional[Literal['private', 'group']]``: 消息类型,可选值:'private' 和 'group',为空表示所有类型 - * ``time_start: Optional[datetime]``: 起始时间,UTC 时间,为空表示不限制起始时间 - * ``time_stop: Optional[datetime]``: 结束时间,UTC 时间,为空表示不限制结束时间 - - :返回值: - * ``List[MessageRecord]``:返回信息 - """ - - whereclause = [] - if user_ids: - whereclause.append( - or_(*[MessageRecord.user_id == user_id for user_id in user_ids]) # type: ignore - ) - if group_ids: - whereclause.append( - or_(*[MessageRecord.group_id == group_id for group_id in group_ids]) # type: ignore - ) - if platforms: - whereclause.append( - or_(*[MessageRecord.platform == platform for platform in platforms]) # type: ignore - ) - if exclude_user_ids: - for user_id in exclude_user_ids: - whereclause.append(MessageRecord.user_id != user_id) - if exclude_group_ids: - for group_id in exclude_group_ids: - whereclause.append(MessageRecord.group_id != group_id) - if message_type: - whereclause.append(MessageRecord.detail_type == message_type) - if time_start: - whereclause.append(MessageRecord.time >= time_start) - if time_stop: - whereclause.append(MessageRecord.time <= time_stop) - - statement = select(MessageRecord).where(*whereclause) - async with create_session() as session: - records: List[MessageRecord] = (await session.exec(statement)).all() # type: ignore - return records - - - async def msg_counter(msg_list:List[MessageRecord])->Dict[str,int]: ''' 计算出话最多的几个人的id并返回 @@ -106,9 +39,9 @@ async def msg_counter(msg_list:List[MessageRecord])->Dict[str,int]: for i in msg_list: try: - lst[i.user_id] +=1 + lst[i.user_id] += 1 except KeyError: - lst[i.user_id] =1 + lst[i.user_id] = 1 logger.debug(lst)