aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/utils/settings.py
blob: 0a1716fabd67bf1432ad1c79d21934d744516833 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This file is part of Senator Sperma
# Licensed under the GPL-3.0 License
# Copyright (C) 2026 Salmonik @ salmonik@tuta.io
# https://codeberg.org/salmonik/senator-sperma

import json
import logging
import aiofiles
import os

logger = logging.getLogger(__name__)

_settings = None
settings_file = "data/settings.json"

async def load_settings():
    global _settings
    global settings_file
    
    default_settings = {
        'volume': 1.0,
        'baseurl': 'http://localhost:8080'
    }
    
    try:
        if os.path.exists(settings_file):
            async with aiofiles.open(settings_file, 'r') as f:
                content = await f.read()
                if content.strip():
                    _settings = json.loads(content)
                    logger.info("Settings loaded into memory")
                else:
                    _settings = default_settings
                    await save_settings()
                    logger.warning("Empty settings.json, reset to defaults")
        else:
            _settings = default_settings
            await save_settings()
            logger.info("Created default settings.json")
    except (json.JSONDecodeError, Exception) as e:
        logger.error(f"Error loading settings: {e}")
        _settings = default_settings
    
    return _settings


async def save_settings():
    if _settings:
        async with aiofiles.open(settings_file, 'w') as f:
            await f.write(json.dumps(_settings, indent=2))


def get_settings():
    return _settings


def get_volume():
    return _settings.get('volume', 1.0) if _settings else 1.0


def get_baseurl():
    return _settings.get('baseurl', 'http://localhost:8080').rstrip('/') if _settings else 'http://localhost:8080'