FunguyBot/plugins/config.py

155 lines
5.2 KiB
Python
Raw Permalink Normal View History

"""
Custom configuration class for the Funguy bot.
"""
2024-02-14 05:39:17 +00:00
# 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}")
2024-02-14 05:39:17 +00:00
_admin_user: str = ""
_prefix: str = ""
_config_file: str= ""
2024-02-14 05:39:17 +00:00
# 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
2024-02-14 05:39:17 +00:00
# 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}")
2024-02-14 05:39:17 +00:00
# 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}")
2024-02-14 05:39:17 +00:00
async def handle_command(room, message, bot, prefix, config):
2024-02-14 05:39:17 +00:00
"""
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)
2024-02-14 05:39:17 +00:00
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
2024-02-14 05:39:17 +00:00
args = match.args()
if len(args) != 2:
await bot.api.send_text_message(room.room_id, "Usage: !set <config_option> <value>")
return
option, value = args
# Set the specified configuration option based on the provided value
2024-02-14 05:39:17 +00:00
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
2024-02-14 05:39:17 +00:00
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 <config_option>")
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")
2024-02-14 05:39:17 +00:00
# If the command is 'show', display the current configuration
2024-02-14 05:39:17 +00:00
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
2024-02-14 05:39:17 +00:00
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")