""" Custom configuration class for the Funguy bot. """ # plugins/config.py import os import logging import simplematrixbotlib as botlib from dataclasses import dataclass @dataclass class FunguyConfig(botlib.Config): """ Custom configuration class for the Funguy bot. Extends the base Config class to provide additional configuration options. Args: config_file (str): Path to the configuration file. """ def __init__(self, config_file="funguy.conf"): super().__init__() # Load configuration from file self.load_toml(config_file) logging.info(f"Loaded configuration from {config_file}") _admin_user: str = "" _prefix: str = "" _config_file: str= "" # Define getters and setters for custom configuration options @property def admin_user(self): return self._admin_user @admin_user.setter def admin_user(self, value): self._admin_user = value @property def prefix(self): return self._prefix @prefix.setter def prefix(self, value): self._prefix = value @property def config_file(self): return self._config_file @config_file.setter def config_file(self, value): self._config_file = value # Method to load configuration from file def load_config(self, config_file): """ Load configuration options from a TOML file. Args: config_file (str): Path to the configuration file. Returns: None """ self.load_toml(config_file) logging.info(f"Loaded configuration from {config_file}") # Method to save configuration to file def save_config(self, config_file): """ Save configuration options to a TOML file. Args: config_file (str): Path to the configuration file. Returns: None """ self.save_toml(config_file) logging.info(f"Saved configuration to {config_file}") async def handle_command(room, message, bot, prefix, config): """ Function to handle commands related to bot configuration. Args: room (Room): The Matrix room where the command was invoked. message (RoomMessage): The message object containing the command. bot (Bot): The bot instance. PREFIX (str): The bot command prefix. config (FunguyConfig): The bot configuration instance. Returns: None """ # Check if the message matches the command pattern and is not from this bot match = botlib.MessageMatch(room, message, bot, prefix) if match.is_not_from_this_bot() and match.prefix() and match.command("set"): # If the command is 'set', check if it has exactly two arguments args = match.args() if len(args) != 2: await bot.api.send_text_message(room.room_id, "Usage: !set ") return option, value = args # Set the specified configuration option based on the provided value if option == "admin_user": config.admin_user = value await bot.api.send_text_message(room.room_id, f"Admin user set to {value}") elif option == "prefix": config.prefix = value await bot.api.send_text_message(room.room_id, f"Prefix set to {value}") else: await bot.api.send_text_message(room.room_id, "Invalid configuration option") # If the command is 'get', retrieve the value of the specified configuration option elif match.is_not_from_this_bot() and match.prefix() and match.command("get"): args = match.args() if len(args) != 1: await bot.api.send_text_message(room.room_id, "Usage: !get ") return option = args[0] if option == "admin_user": await bot.api.send_text_message(room.room_id, f"Admin user: {config.admin_user}") elif option == "prefix": await bot.api.send_text_message(room.room_id, f"Prefix: {config.prefix}") else: await bot.api.send_text_message(room.room_id, "Invalid configuration option") # If the command is 'saveconf', save the current configuration elif match.is_not_from_this_bot() and match.prefix() and match.command("saveconf"): config.save_config(config.config_file) await bot.api.send_text_message(room.room_id, "Configuration saved") # If the command is 'loadconf', load the saved configuration elif match.is_not_from_this_bot() and match.prefix() and match.command("loadconf"): config.load_config(config.config_file) await bot.api.send_text_message(room.room_id, "Configuration loaded") # If the command is 'show', display the current configuration elif match.is_not_from_this_bot() and match.prefix() and match.command("show"): admin_user = config.admin_user prefix = config.prefix await bot.api.send_text_message(room.room_id, f"Admin user: {admin_user}, Prefix: {prefix}") # If the command is 'reset', reset the configuration to default values elif match.is_not_from_this_bot() and match.prefix() and match.command("reset"): config.admin_user = "" config.prefix = "!" await bot.api.send_text_message(room.room_id, "Configuration reset")