import time import os import random import json DICTIONARY_PATH = "dictionary/" # Path to dictionary files def format_uptime(seconds: float) -> tuple[str, int]: """ Convert seconds into a human-readable string: - Example outputs: "32 minutes" "8 days, 4 hours" "1 year, 3 months" - Returns a tuple: (Human-readable string, total seconds) """ seconds = int(seconds) # Ensure integer seconds # Define time units units = [ ("year", 31536000), # 365 days ("month", 2592000), # 30 days ("day", 86400), # 24 hours ("hour", 3600), # 60 minutes ("minute", 60), ("second", 1) ] # Compute time breakdown time_values = [] for unit_name, unit_seconds in units: value, seconds = divmod(seconds, unit_seconds) if value > 0: time_values.append(f"{value} {unit_name}{'s' if value > 1 else ''}") # Auto pluralize # Return only the **two most significant** time units (e.g., "3 days, 4 hours") return (", ".join(time_values[:2]), seconds) if time_values else ("0 seconds", 0) def get_random_reply(dictionary_name: str, category: str, **variables) -> str: """ Fetches a random string from a given dictionary and category. Supports variable substitution using keyword arguments. :param dictionary_name: The name of the dictionary file (without .json) :param category: The category (key) inside the dictionary to fetch a response from :param variables: Keyword arguments to replace placeholders in the string :return: A formatted string with the variables replaced """ file_path = os.path.join(DICTIONARY_PATH, f"{dictionary_name}.json") # Ensure file exists if not os.path.exists(file_path): return f"[Error: Missing {dictionary_name}.json]" try: with open(file_path, "r", encoding="utf-8") as file: data = json.load(file) except json.JSONDecodeError: return f"[Error: Failed to load {dictionary_name}.json]" # Ensure category exists if category not in data or not isinstance(data[category], list): return f"[Error: No valid entries for {category} in {dictionary_name}.json]" # Select a random reply response = random.choice(data[category]) # Replace placeholders with provided variables return response.format(**variables)