2024-03-01 03:18:58 +00:00
|
|
|
"""
|
|
|
|
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)
|
2024-02-14 07:12:40 +00:00
|
|
|
logging.info(f"Loaded configuration from {config_file}")
|
2024-02-14 05:39:17 +00:00
|
|
|
|
|
|
|
_admin_user: str = ""
|
|
|
|
_prefix: str = ""
|
2024-02-14 07:12:40 +00:00
|
|
|
_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
|
|
|
|
|
2024-02-14 07:12:40 +00:00
|
|
|
@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
|
|
|
|
"""
|
2024-02-14 07:12:40 +00:00
|
|
|
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)
|
2024-02-14 07:12:40 +00:00
|
|
|
logging.info(f"Saved configuration to {config_file}")
|
2024-02-14 05:39:17 +00:00
|
|
|
|
2024-02-14 07:12:40 +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
|
|
|
|
"""
|
2024-02-14 07:12:40 +00:00
|
|
|
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"):
|
|
|
|
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")
|
|
|
|
|
2024-02-14 07:12:40 +00:00
|
|
|
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")
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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")
|
|
|
|
|