131 lines
4.2 KiB
Python
131 lines
4.2 KiB
Python
|
# 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("Loaded configuration from funguy.conf")
|
||
|
|
||
|
_admin_user: str = ""
|
||
|
_prefix: 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
|
||
|
|
||
|
# 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.__init__()
|
||
|
logging.info("Loaded configuration from funguy.conf")
|
||
|
|
||
|
# 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("Saved configuration to funguy.conf")
|
||
|
|
||
|
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
|
||
|
"""
|
||
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
||
|
if match.is_not_from_this_bot() and match.prefix() and match.command("set"):
|
||
|
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
|
||
|
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")
|
||
|
|
||
|
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")
|
||
|
|
||
|
elif match.is_not_from_this_bot() and match.prefix() and match.command("save"):
|
||
|
config.save_config("funguy.conf")
|
||
|
config.load_config("funguy.conf")
|
||
|
await bot.api.send_text_message(room.room_id, "Configuration saved & reloaded")
|
||
|
|
||
|
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}")
|
||
|
|
||
|
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")
|
||
|
|