def isUserLoggedIn(userId): try: isLogin = json.loads(apiGetUserPreview(userId, True))['isLogin'] logger.debug(f"用户 {userId} 是否登录: {isLogin}") return isLogin except Exception as e: logger.error(f"检查用户 {userId} 登录状态时出错: {e}") return False def convert_hhmm_to_timestamp(time_str: str) -> int: now = datetime.now() dt_obj = datetime.strptime(f"{now.year}-{now.month}-{now.day} {time_str}", "%Y-%m-%d %H:%M") return int(dt_obj.timestamp()) def logOut(userId, Timestamp): try: response = apiLogout(Timestamp, userId, True) if response and response.get('returnCode') == 1: logger.debug(f"已成功发送登出请求给用户 {userId},时间戳 {Timestamp}") return True return False except Exception as e: logger.error(f"使用时间戳 {Timestamp} 登出用户 {userId} 时发生错误: {e}") return False def isCorrectTimestamp(timestamp, userId): if not logOut(userId, timestamp): return False time.sleep(0.1) isLoggedOut = not isUserLoggedIn(userId) logger.debug(f"时间戳 {timestamp} 是否正确: {isLoggedOut}") return isLoggedOut def findTimestampInRange(start_ts: int, end_ts: int, userId: int): logger.info(f"开始在时间范围 [{start_ts}, {end_ts}] 内为用户 {userId} 搜索有效时间戳...") for ts in range(start_ts, end_ts + 1): if isCorrectTimestamp(ts, userId): logger.info(f"找到正确的时间戳: {ts}") return ts logger.error(f"在指定范围内未能找到用户 {userId} 的有效时间戳") return None async def handle_unban_command(bot: Bot, event: MessageEvent, args: Message = CommandArg()): arg_list = args.extract_plain_text().strip().split() if len(arg_list) != 2: await MessageUtils.build_message( "命令格式不正确喵~\n" "正确格式: /黑屋 <开始时间> <结束时间>\n" "例如: /黑屋 19:00 19:10\n" "注意:时间范围不能超过30分钟。" ).send(reply_to=True) return start_time_str, end_time_str = arg_list user_qq = str(event.user_id) if user_qq not in bind_data: await MessageUtils.build_message("zako~又不绑定账号吗").send(reply_to=True) return user_id = bind_data[user_qq] try: logger.info("开始解析时间参数...") start_timestamp = convert_hhmm_to_timestamp(start_time_str) end_timestamp = convert_hhmm_to_timestamp(end_time_str) logger.info(f"时间参数解析成功: 开始时间戳 {start_timestamp}, 结束时间戳 {end_timestamp}") if end_timestamp <= start_timestamp: await MessageUtils.build_message("结束时间必须晚于开始时间!").send(reply_to=True) return if (end_timestamp - start_timestamp) > 1800: # 30分钟 * 60秒 await MessageUtils.build_message("时间范围不能超过30分钟哦!").send(reply_to=True) return except ValueError: logger.error(f"无法解析时间格式: {start_time_str} 或 {end_time_str}") await MessageUtils.build_message("时间格式错误,请使用 HH:MM 格式!").send(reply_to=True) return except Exception as e: logger.error(f"解析时间时发生未知错误: {e}") await MessageUtils.build_message("解析时间时发生未知错误,请检查后台日志。").send(reply_to=True) return logger.info(f"正在检查用户 {user_id} 的登录状态...") if not isUserLoggedIn(user_id): await MessageUtils.build_message("zako~没进黑屋干什么解").send(reply_to=True) return await MessageUtils.build_message(f"收到!将在 {start_time_str} 到 {end_time_str} 的时间范围内尝试解黑屋,请稍候...").send(reply_to=True) try: start_process_time = time.time() result_timestamp = await asyncio.to_thread(findTimestampInRange, start_timestamp, end_timestamp, user_id) end_process_time = time.time() duration = end_process_time - start_process_time if result_timestamp is not None: human_readable_time = datetime.fromtimestamp(result_timestamp).strftime('%Y-%m-%d %H:%M:%S') final_message = f"解黑屋成功!\n找到的时间点: {human_readable_time}\n消耗时间: {duration:.2f}秒" else: final_message = "解黑屋失败,在指定时间段内未能找到有效的时间点。" await MessageUtils.build_message(final_message).send(reply_to=True) except Exception as e: logger.error(f"处理 /黑屋 命令时发生意外错误: {e}") await MessageUtils.build_message("执行过程中发生未知错误,请联系管理员。").send(reply_to=True) }