From 0e6473bb2c370d3575720567e561edea8b5d34eb Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Wed, 11 Sep 2024 21:33:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0cookies=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E7=BE=A4=E5=86=85=E8=BF=9B=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- campux/imbot/nbmod.py | 72 ++++++++++++++++++++------------------ campux/mq/redis.py | 6 ++-- campux/social/mgr.py | 6 ++-- campux/social/qzone/api.py | 5 --- 4 files changed, 44 insertions(+), 45 deletions(-) diff --git a/campux/imbot/nbmod.py b/campux/imbot/nbmod.py index 9aea242..4ee7a42 100644 --- a/campux/imbot/nbmod.py +++ b/campux/imbot/nbmod.py @@ -40,8 +40,6 @@ async def fdelay(seconds: float=3): sign_up = on_command("注册账号", rule=to_me() & is_private & not_bot_self, priority=10, block=True) reset_password = on_command("重置密码", rule=to_me() & is_private & not_bot_self, priority=10, block=True) -relogin_qzone = on_command("更新cookies", rule=to_me() & is_private & not_bot_self, priority=10, block=True) - any_message = on_regex(r".*", rule=to_me() & is_private, priority=100, block=True) @sign_up.handle() @@ -73,34 +71,6 @@ async def reset_password_func(event: Event): traceback.print_exc() await reset_password.finish(str(e)) -@relogin_qzone.handle() -async def relogin_qzone_func(event: Event): - user_id = int(event.get_user_id()) - - if user_id != ap.config.data['campux_qq_admin_uin']: - await relogin_qzone.finish("无权限") - return - - async def qrcode_callback(content: bytes): - asyncio.create_task(ap.imbot.send_private_message( - ap.config.data['campux_qq_admin_uin'], - message=[ - message.MessageSegment.text("请使用QQ扫描以下二维码以登录QQ空间:"), - message.MessageSegment.image(content) - ] - )) - - try: - - await fdelay() - - await ap.social.platform_api.relogin(qrcode_callback) - except Exception as e: - if isinstance(e, nonebot.exception.FinishedException): - return - traceback.print_exc() - await relogin_qzone.finish(str(e)) - @any_message.handle() async def any_message_func(event: Event): if not can_send_help_message(int(event.get_user_id())): @@ -114,22 +84,28 @@ async def any_message_func(event: Event): async def is_group(event: Event): return type(event) == ob11_event.GroupMessageEvent +async def is_admin_group(event: Event): + return int(event.group_id) == int(ap.config.data['campux_review_qq_group_id']) + async def is_review_allow(event: Event): if type(event) == ob11_event.PrivateMessageEvent: return False return ap.config.data['campux_qq_group_review'] and int(event.group_id) == int(ap.config.data['campux_review_qq_group_id']) # #通过 [id] -approve_post = on_command("通过", rule=to_me() & is_group & is_review_allow, priority=10, block=True) +approve_post = on_command("通过", rule=to_me() & is_group & is_admin_group & is_review_allow, priority=10, block=True) # #拒绝 <原因> [id] -reject_post = on_command("拒绝", rule=to_me() & is_group & is_review_allow, priority=10, block=True) +reject_post = on_command("拒绝", rule=to_me() & is_group & is_admin_group & is_review_allow, priority=10, block=True) # 重发 -resend_post = on_command("重发", rule=to_me() & is_group, priority=10, block=True) +resend_post = on_command("重发", rule=to_me() & is_group & is_admin_group, priority=10, block=True) + +# 登录 QQ 空间 +login_qzone = on_command("登录", rule=to_me() & is_group & is_admin_group, priority=10, block=True) # 其他命令,发帮助信息 -any_message_group = on_regex(r".*", rule=to_me() & is_group & is_review_allow, priority=100, block=True) +any_message_group = on_regex(r".*", rule=to_me() & is_group & is_admin_group & is_review_allow, priority=100, block=True) @approve_post.handle() @@ -236,6 +212,34 @@ async def resend_post_func(event: Event): traceback.print_exc() await resend_post.finish(str(e)) +@login_qzone.handle() +async def login_qzone_func(event: Event): + + async def qrcode_callback(content: bytes): + asyncio.create_task(ap.imbot.send_group_message( + ap.config.data['campux_review_qq_group_id'], + message=[ + message.MessageSegment.text("请使用本号 QQ 手机端扫描以下二维码以登录 QQ空间:"), + message.MessageSegment.image(content) + ] + )) + + try: + + await fdelay() + + await ap.social.platform_api.relogin(qrcode_callback) + + await ap.imbot.send_group_message( + ap.config.data['campux_review_qq_group_id'], + "登录流程完成。" + ) + except Exception as e: + if isinstance(e, nonebot.exception.FinishedException): + return + traceback.print_exc() + await login_qzone.finish(str(e)) + @any_message_group.handle() async def any_message_group_func(event: Event): await any_message_group.finish(ap.config.data['campux_review_help_message']) diff --git a/campux/mq/redis.py b/campux/mq/redis.py index 6469845..28802ce 100644 --- a/campux/mq/redis.py +++ b/campux/mq/redis.py @@ -157,9 +157,9 @@ async def check_publish_post(self, message: tuple): if len(self.relogin_notify_times) == 0 or now - self.relogin_notify_times[-1] > 120*60: self.relogin_notify_times.append(now) - asyncio.create_task(self.ap.imbot.send_private_message( - self.ap.config.data['campux_qq_admin_uin'], - "空间cookies失效,当前有稿件待发布,请尽快更新cookies。" + asyncio.create_task(self.ap.imbot.send_group_message( + self.ap.config.data['campux_review_qq_group_id'], + "空间cookies失效,当前有稿件待发布,请尽快重新登录 QQ 空间。" )) logger.warning("social模块未准备好,无法发布稿件。") diff --git a/campux/social/mgr.py b/campux/social/mgr.py index 7533913..4ef851f 100644 --- a/campux/social/mgr.py +++ b/campux/social/mgr.py @@ -58,9 +58,9 @@ async def schedule_task(self): nonebot.logger.info("QQ空间cookies已失效,发送通知。") - asyncio.create_task(self.ap.imbot.send_private_message( - self.ap.config.data['campux_qq_admin_uin'], - "QQ空间cookies已失效,请发送 #更新cookies 命令进行重新登录。" + asyncio.create_task(self.ap.imbot.send_group_message( + self.ap.config.data['campux_review_qq_group_id'], + "QQ空间cookies已失效,请 @ 并发送 #登录 命令进行重新登录。" )) self.current_invalid_cookies = self.platform_api.cookies diff --git a/campux/social/qzone/api.py b/campux/social/qzone/api.py index e477334..5c8a776 100644 --- a/campux/social/qzone/api.py +++ b/campux/social/qzone/api.py @@ -128,11 +128,6 @@ async def relogin(self, callback: callable): if 'uin' in self.cookies: self.uin = int(self.cookies['uin'][1:]) - asyncio.create_task(self.ap.imbot.send_private_message( - self.ap.config.data['campux_qq_admin_uin'], - "登录流程完成。" - )) - self.ap.cache.data['qzone_cookies'] = self.cookies self.ap.cache.save()