From 1dc82dcb9e962216123a42af9a63f98db4bbf1d2 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sat, 12 Mar 2022 19:49:03 +0800 Subject: [PATCH 1/3] feat(more notice): Group Admin Change and Member Restrict --- efb_qq_plugin_go_cqhttp/GoCQHttp.py | 56 ++++++++++++++++++++++++++++- efb_qq_plugin_go_cqhttp/Utils.py | 12 +++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/efb_qq_plugin_go_cqhttp/GoCQHttp.py b/efb_qq_plugin_go_cqhttp/GoCQHttp.py index 3c3997e..18605ba 100644 --- a/efb_qq_plugin_go_cqhttp/GoCQHttp.py +++ b/efb_qq_plugin_go_cqhttp/GoCQHttp.py @@ -50,6 +50,7 @@ from .Utils import ( download_user_avatar, process_quote_text, qq_emoji_list, + strf_time, ) @@ -304,6 +305,58 @@ class GoCQHttp(BaseClient): context["message"] = text self.send_efb_group_notice(context) + @self.coolq_bot.on_notice("group_admin") + def handle_group_admin_msg(context): + context["event_description"] = self._("\u2139 Group Admin Change Event") + if (context["sub_type"]) == "set": + text = self._("{nickname}({context[user_id]}) " + "has been appointed as the group({group_name}) administrator") + else: + text = self._("{nickname}({context[user_id]}) " + "has been de-appointed as the group({group_name}) administrator") + + original_group = self.get_group_info(context["group_id"], False) + group_name = context["group_id"] + if original_group is not None and "group_name" in original_group: + group_name = original_group["group_name"] + text = text.format( + nickname=self.get_stranger_info(context["user_id"])["nickname"], + context=context, + group_name=group_name, + ) + + context["message"] = text + self.send_efb_group_notice(context) + + @self.coolq_bot.on_notice("group_ban") + def handle_group_ban_msg(context): + context["event_description"] = self._("\u2139 Group Member Restrict Event") + if (context["sub_type"]) == "ban": + text = self._("{nickname}({context[user_id]}) " + "is restricted for speaking for {time} at the group({group_name}) by " + "{nickname_}({context[operator_id]})") + time_text = strf_time(context["duration"]) + else: + text = self._("{nickname}({context[user_id]}) " + "is lifted from restrictions at the group({group_name}) by " + "{nickname_}({context[operator_id]}){time}") + time_text = "" + + original_group = self.get_group_info(context["group_id"], False) + group_name = context["group_id"] + if original_group is not None and "group_name" in original_group: + group_name = original_group["group_name"] + text = text.format( + nickname=self.get_stranger_info(context["user_id"])["nickname"], + context=context, + time=time_text, + group_name=group_name, + nickname_=self.get_stranger_info(context["operator_id"])["nickname"], + ) + + context["message"] = text + self.send_efb_group_notice(context) + @self.coolq_bot.on_notice("offline_file") def handle_offline_file_upload_msg(context): context["event_description"] = "\u2139 Offline File Upload Event" @@ -922,12 +975,13 @@ class GoCQHttp(BaseClient): author = chat.get_member(SystemChatMember.SYSTEM_ID) except KeyError: author = chat.add_system_member() + event_description = context.get("event_description", "") msg = Message( uid="__group_notice__.%s" % int(time.time()), type=MsgType.Text, chat=chat, author=author, - text=context["message"], + text=(event_description + "\n\n" + context["message"]) if event_description else context["message"], deliver_to=coordinator.master, ) coordinator.send_message(msg) diff --git a/efb_qq_plugin_go_cqhttp/Utils.py b/efb_qq_plugin_go_cqhttp/Utils.py index 9de97d5..7776b92 100644 --- a/efb_qq_plugin_go_cqhttp/Utils.py +++ b/efb_qq_plugin_go_cqhttp/Utils.py @@ -784,3 +784,15 @@ def download_voice(voice_url: str): else: audio_file = origin_file return audio_file + + +def strf_time(seconds: int) -> str: + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + days, hours = divmod(hours, 24) + text = "" + text += f"{days}d" if days else "" + text += f"{hours}h" if hours else "" + text += f"{minutes}m" if minutes else "" + text += f"{seconds}s" if seconds else "" + return text From 7d4d3c4a0bc12f99b0326f035411c2d648d9bf9b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Mar 2022 11:52:26 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- efb_qq_plugin_go_cqhttp/GoCQHttp.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/efb_qq_plugin_go_cqhttp/GoCQHttp.py b/efb_qq_plugin_go_cqhttp/GoCQHttp.py index 18605ba..293c4d2 100644 --- a/efb_qq_plugin_go_cqhttp/GoCQHttp.py +++ b/efb_qq_plugin_go_cqhttp/GoCQHttp.py @@ -309,11 +309,13 @@ class GoCQHttp(BaseClient): def handle_group_admin_msg(context): context["event_description"] = self._("\u2139 Group Admin Change Event") if (context["sub_type"]) == "set": - text = self._("{nickname}({context[user_id]}) " - "has been appointed as the group({group_name}) administrator") + text = self._( + "{nickname}({context[user_id]}) " "has been appointed as the group({group_name}) administrator" + ) else: - text = self._("{nickname}({context[user_id]}) " - "has been de-appointed as the group({group_name}) administrator") + text = self._( + "{nickname}({context[user_id]}) " "has been de-appointed as the group({group_name}) administrator" + ) original_group = self.get_group_info(context["group_id"], False) group_name = context["group_id"] @@ -332,14 +334,18 @@ class GoCQHttp(BaseClient): def handle_group_ban_msg(context): context["event_description"] = self._("\u2139 Group Member Restrict Event") if (context["sub_type"]) == "ban": - text = self._("{nickname}({context[user_id]}) " - "is restricted for speaking for {time} at the group({group_name}) by " - "{nickname_}({context[operator_id]})") + text = self._( + "{nickname}({context[user_id]}) " + "is restricted for speaking for {time} at the group({group_name}) by " + "{nickname_}({context[operator_id]})" + ) time_text = strf_time(context["duration"]) else: - text = self._("{nickname}({context[user_id]}) " - "is lifted from restrictions at the group({group_name}) by " - "{nickname_}({context[operator_id]}){time}") + text = self._( + "{nickname}({context[user_id]}) " + "is lifted from restrictions at the group({group_name}) by " + "{nickname_}({context[operator_id]}){time}" + ) time_text = "" original_group = self.get_group_info(context["group_id"], False) From 3f89606ab66e0d84ffc40581b851ef1f2f8b2e1a Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Mon, 14 Mar 2022 13:13:26 +0800 Subject: [PATCH 3/3] refactor: clean up --- efb_qq_plugin_go_cqhttp/GoCQHttp.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/efb_qq_plugin_go_cqhttp/GoCQHttp.py b/efb_qq_plugin_go_cqhttp/GoCQHttp.py index 293c4d2..ea18b6e 100644 --- a/efb_qq_plugin_go_cqhttp/GoCQHttp.py +++ b/efb_qq_plugin_go_cqhttp/GoCQHttp.py @@ -307,15 +307,11 @@ class GoCQHttp(BaseClient): @self.coolq_bot.on_notice("group_admin") def handle_group_admin_msg(context): - context["event_description"] = self._("\u2139 Group Admin Change Event") + context["event_description"] = "\u2139 Group Admin Change Event" if (context["sub_type"]) == "set": - text = self._( - "{nickname}({context[user_id]}) " "has been appointed as the group({group_name}) administrator" - ) + text = "{nickname}({context[user_id]}) has been appointed as the group({group_name}) administrator" else: - text = self._( - "{nickname}({context[user_id]}) " "has been de-appointed as the group({group_name}) administrator" - ) + text = "{nickname}({context[user_id]}) has been de-appointed as the group({group_name}) administrator" original_group = self.get_group_info(context["group_id"], False) group_name = context["group_id"] @@ -332,16 +328,16 @@ class GoCQHttp(BaseClient): @self.coolq_bot.on_notice("group_ban") def handle_group_ban_msg(context): - context["event_description"] = self._("\u2139 Group Member Restrict Event") + context["event_description"] = "\u2139 Group Member Restrict Event" if (context["sub_type"]) == "ban": - text = self._( + text = ( "{nickname}({context[user_id]}) " "is restricted for speaking for {time} at the group({group_name}) by " "{nickname_}({context[operator_id]})" ) time_text = strf_time(context["duration"]) else: - text = self._( + text = ( "{nickname}({context[user_id]}) " "is lifted from restrictions at the group({group_name}) by " "{nickname_}({context[operator_id]}){time}"