From 2a52da8c6162740785e0b7694c728a6f08b2c135 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sat, 12 Mar 2022 18:15:41 +0800 Subject: [PATCH] Support Group Admin Change Event and Member Restrict Event --- efb_qq_plugin_go_cqhttp/GoCQHttp.py | 62 ++++++++++++++++++++++++++++- efb_qq_plugin_go_cqhttp/Utils.py | 12 ++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/efb_qq_plugin_go_cqhttp/GoCQHttp.py b/efb_qq_plugin_go_cqhttp/GoCQHttp.py index bf2187c..3c3b78d 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, ) all_group_list = [] @@ -322,6 +323,64 @@ 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): + if len(all_group_list) > 0 and context["group_id"] not in all_group_list: + print(f"Filter 1 group_admin from {context['group_id']}.") + return + 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): + if len(all_group_list) > 0 and context["group_id"] not in all_group_list: + print(f"Filter 1 group_ban from {context['group_id']}.") + return + 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"] = self._("\u2139 Offline File Upload Event") @@ -949,12 +1008,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 0b88cf6..30f9b5a 100644 --- a/efb_qq_plugin_go_cqhttp/Utils.py +++ b/efb_qq_plugin_go_cqhttp/Utils.py @@ -783,3 +783,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