import time import os import random import json DICTIONARY_PATH = "dictionary/" # Path to dictionary files def format_uptime(seconds: float) -> str: """ Convert seconds into a human-readable string: e.g. 32 minutes, 8 days, 8 months, etc. Returns two values: 1. Human-readable format 2. Seconds since start """ # Convert float seconds to an integer seconds = int(seconds) # We'll break it down minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) # For months and years, let's approximate 30 days per month, # 365 days per year, etc. (just a rough approach) months, days = divmod(days, 30) years, months = divmod(months, 12) # Build a short string, only listing the largest units # If you want more detail, you can keep going if years > 0: return [f"{years} year(s), {months} month(s)", seconds] elif months > 0: return [f"{months} month(s), {days} day(s)", seconds] elif days > 0: return [f"{days} day(s), {hours} hour(s)", seconds] elif hours > 0: return [f"{hours} hour(s), {minutes} minute(s)", seconds] elif minutes > 0: return [f"{minutes} minute(s)", seconds] else: return [f"{seconds} second(s)", seconds] 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)