import os import json from typing import Dict, Any, Optional class Config: def __init__(self, config_file: str = 'config.json'): self.config_file = config_file self.data: Dict[str, Any] = {} self.load_config() def load_config(self): """Load configuration from file if it exists.""" if os.path.exists(self.config_file): try: with open(self.config_file, 'r', encoding='utf-8') as f: self.data = json.load(f) except (json.JSONDecodeError, FileNotFoundError): self.data = {} else: self.data = {} def save_config(self): """Save configuration to file if dirty.""" if self.dirty: try: with open(self.config_file, 'w', encoding='utf-8') as f: json.dump(self.data, f, indent=4, ensure_ascii=False) self.dirty = False except Exception as e: print(f"Erreur lors de la sauvegarde de la configuration: {e}") def get(self, key: str, default: Any = None) -> Any: """Get a configuration value.""" return self.data.get(key, default) def set(self, key: str, value: Any): """Set a configuration value and save.""" self.data[key] = value self.save_config() def get_welcome_channel(self, guild_id: int) -> Optional[int]: """Get welcome channel ID for a guild.""" return self.get('welcome_channels', {}).get(str(guild_id)) def set_welcome_channel(self, guild_id: int, channel_id: int): """Set welcome channel for a guild.""" welcome_channels = self.get('welcome_channels', {}) welcome_channels[str(guild_id)] = channel_id self.set('welcome_channels', welcome_channels) def get_goodbye_channel(self, guild_id: int) -> Optional[int]: """Get goodbye channel ID for a guild.""" return self.get('goodbye_channels', {}).get(str(guild_id)) def set_goodbye_channel(self, guild_id: int, channel_id: int): """Set goodbye channel for a guild.""" goodbye_channels = self.get('goodbye_channels', {}) goodbye_channels[str(guild_id)] = channel_id self.set('goodbye_channels', goodbye_channels) def get_mute_role(self, guild_id: int) -> Optional[str]: """Get mute role name for a guild.""" return self.get('mute_roles', {}).get(str(guild_id)) def set_mute_role(self, guild_id: int, role_name: str): """Set mute role for a guild.""" mute_roles = self.get('mute_roles', {}) mute_roles[str(guild_id)] = role_name self.set('mute_roles', mute_roles) self.save_config() # Force save for critical config def get_allowed_words(self) -> list: """Get list of allowed words.""" return self.get('allowed_words', []) def set_allowed_words(self, words: list): """Set allowed words.""" self.set('allowed_words', words) def get_mod_report_channel(self, guild_id: int) -> Optional[int]: """Get mod report channel ID for a guild.""" return self.get('mod_report_channels', {}).get(str(guild_id)) def set_mod_report_channel(self, guild_id: int, channel_id: int): """Set mod report channel for a guild.""" mod_channels = self.get('mod_report_channels', {}) mod_channels[str(guild_id)] = channel_id self.set('mod_report_channels', mod_channels) # Global config instance config = Config()