Skip to content

Commit

Permalink
feat: 更新cookies改为群内进行
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Sep 11, 2024
1 parent 1b47478 commit 0e6473b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
72 changes: 38 additions & 34 deletions campux/imbot/nbmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())):
Expand All @@ -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)

# 重发 <id>
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()
Expand Down Expand Up @@ -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'])
6 changes: 3 additions & 3 deletions campux/mq/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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模块未准备好,无法发布稿件。")
Expand Down
6 changes: 3 additions & 3 deletions campux/social/mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down