From 5a3ac8dac0d5481c53bc8d66252fad5afbdaa926 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 9 Apr 2023 10:09:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=B5=8B=E8=AF=95=E8=BF=87?= =?UTF-8?q?=E7=9A=84=E4=BA=A7=E7=89=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_dialectlist/__init__.py | 10 +++++--- nonebot_plugin_dialectlist/function.py | 33 +++++++++++--------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/nonebot_plugin_dialectlist/__init__.py b/nonebot_plugin_dialectlist/__init__.py index 69e8cb5..1cacc02 100644 --- a/nonebot_plugin_dialectlist/__init__.py +++ b/nonebot_plugin_dialectlist/__init__.py @@ -119,7 +119,7 @@ async def _group_message( @ranks.handle() async def _private_message( matcher: Matcher, - event: Union[V11Event.GroupMessageEvent, V12Event.GroupMessageEvent], + event: Union[V11Event.PrivateMessageEvent, V12Event.PrivateMessageEvent], state: T_State, commands: Tuple[str, ...] = Command(), args: Union[V11Message, V12Message] = CommandArg(), @@ -156,7 +156,9 @@ async def handle_message( plugin_config.dialectlist_excluded_people.append(bot_id["user_id"]) msg_list = await get_message_records( bot_ids=[str(bot.self_id)], - platforms=[str(bot.platform)], + platforms=['qq'] + if isinstance(event, V11Event.GroupMessageEvent) + else [str(bot.platform)], group_ids=[str(event.group_id)] if isinstance(event, (V11Event.GroupMessageEvent, V12Event.GroupMessageEvent)) else None, @@ -167,13 +169,15 @@ async def handle_message( time_start=start.astimezone(ZoneInfo("UTC")), time_stop=stop.astimezone(ZoneInfo("UTC")), ) + for i in msg_list: + logger.debug(i.plain_text) if isinstance(event, V11Event.GroupMessageEvent): processer = V11GroupMsgProcesser(bot=bot, gid=str(event.group_id), msg_list=msg_list) # type: ignore elif isinstance(event, V12Event.GroupMessageEvent): processer = V12GroupMsgProcesser(bot=bot, gid=str(event.group_id), msg_list=msg_list) # type: ignore elif isinstance(event, V12Event.ChannelMessageEvent): - pass + processer = V12GuildMsgProcesser(bot=bot, gid=str(event.guild_id), msg_list=msg_list) # type: ignore else: raise NotImplementedError("没支持呢(())") diff --git a/nonebot_plugin_dialectlist/function.py b/nonebot_plugin_dialectlist/function.py index f73e6c3..09b69c1 100644 --- a/nonebot_plugin_dialectlist/function.py +++ b/nonebot_plugin_dialectlist/function.py @@ -21,16 +21,6 @@ try: except ImportError: from backports.zoneinfo import ZoneInfo # type: ignore -require("nonebot_plugin_htmlrender") -from nonebot_plugin_htmlrender import ( - md_to_pic, - html_to_pic, - text_to_pic, - capture_element, - template_to_pic, - template_to_html, -) - require("nonebot_plugin_chatrecorder") from nonebot_plugin_chatrecorder import get_message_records from nonebot_plugin_chatrecorder.model import MessageRecord @@ -129,6 +119,7 @@ def got_rank(msg_dict: Dict[str, int]) -> List[List[Union[str, int]]]: try: max_key = max(msg_dict.items(), key=lambda x: x[1]) rank.append(list(max_key)) + msg_dict.pop(max_key[0]) except ValueError: rank.append(["null", 0]) continue @@ -167,7 +158,7 @@ class MsgProcesser(abc.ABC): pic_msg = None if plugin_config.dialectlist_visualization: try: - pic_msg = self.render_template_pic() + pic_msg = await self.render_template_pic() except OSError: plugin_config.dialectlist_visualization = False str_msg += "\n\n无法发送可视化图片,请检查是否安装GTK+,详细安装教程可见github\nhttps://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer \n若不想安装这个软件,再次使用这个指令不会显示这个提示" @@ -180,7 +171,7 @@ class MsgProcesser(abc.ABC): nicknames: List = await self.get_nickname_list() for i in range(len(rank)): index = i + 1 - nickname, chatdatanum = nicknames[i], rank[i] + nickname, chatdatanum = nicknames[i], rank[i][1] str_example = plugin_config.dialectlist_string_format.format( index=index, nickname=nickname, chatdatanum=chatdatanum ) @@ -188,7 +179,7 @@ class MsgProcesser(abc.ABC): return string - def render_template_pic(self) -> bytes: + async def render_template_pic(self) -> bytes: if plugin_config.dialectlist_visualization_type == "圆环图": view = pygal.Pie(inner_radius=0.6, style=style) elif plugin_config.dialectlist_visualization_type == "饼图": @@ -197,7 +188,7 @@ class MsgProcesser(abc.ABC): view = pygal.Bar(style=style) view.title = "消息可视化" - for i, j in zip(self.rank, self.get_nickname_list()): # type: ignore + for i, j in zip(self.rank, await self.get_nickname_list()): # type: ignore view.add(str(j), int(i[1])) png: bytes = view.render_to_png() # type: ignore @@ -217,13 +208,15 @@ class V11GroupMsgProcesser(MsgProcesser): member_info = await self.bot.get_group_member_info( group_id=int(self.gid), user_id=int(self.rank[i][0]), no_cache=True ) - nicknames.append( + nickname=( member_info["nickname"] if not member_info["card"] else member_info["card"] ) - except ActionFailed as e: + nicknames.append(remove_control_characters(nickname)) + except (ActionFailed,ValueError) as e: nicknames.append("{}这家伙不在群里了".format(self.rank[i][0])) + return nicknames def get_head_portrait_urls(self) -> List: @@ -237,7 +230,7 @@ class V11GroupMsgProcesser(MsgProcesser): msgs: List = await self.get_msg() msg = V11Message() msg += V11MessageSegment.text(msgs[0]) # type: ignore - msg += V12MessageSegment.image(msgs[1]) # type: ignore + msg += V11MessageSegment.image(msgs[1]) # type: ignore return msg @@ -268,11 +261,12 @@ class V12GroupMsgProcesser(V12MsgProcesser): member_info = await self.bot.get_group_member_info( group_id=str(self.gid), user_id=str(self.rank[i][0]), no_cache=True ) - nicknames.append( + nickname=( member_info["user_displayname"] if member_info["user_displayname"] else member_info["user_name"] ) + nicknames.append(remove_control_characters(nickname)) except ActionFailed as e: nicknames.append("{}这家伙不在群里了".format(self.rank[i][0])) return nicknames @@ -289,11 +283,12 @@ class V12GuildMsgProcesser(V12MsgProcesser): member_info = await self.bot.get_guild_member_info( guild_id=str(self.gid), user_id=str(self.rank[i][0]), no_cache=True ) - nicknames.append( + nickname=( member_info["user_displayname"] if member_info["user_displayname"] else member_info["user_name"] ) + nicknames.append(remove_control_characters(nickname)) except ActionFailed as e: nicknames.append("{}这家伙不在群里了".format(self.rank[i][0])) return nicknames