Rehash function should work now

This commit is contained in:
Hash Borgir 2024-02-16 20:13:13 -07:00
parent 669fd361d4
commit e9a853c31a

107
funguy.py
View File

@ -6,82 +6,79 @@ import logging
import importlib import importlib
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
from dotenv import load_dotenv from dotenv import load_dotenv
import time
import sys import sys
from plugins.config import FunguyConfig from plugins.config import FunguyConfig
class FunguyBot:
def __init__(self):
# Setup logging # Setup logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
# Load plugins (defined before plugin load/reload functions)
PLUGINS_DIR = "plugins"
PLUGINS = {}
def load_plugins():
for plugin_file in os.listdir(PLUGINS_DIR):
if plugin_file.endswith(".py"):
plugin_name = os.path.splitext(plugin_file)[0]
try:
module = importlib.import_module(f"{PLUGINS_DIR}.{plugin_name}")
PLUGINS[plugin_name] = module
logging.info(f"Loaded plugin: {plugin_name}")
except Exception as e:
logging.error(f"Error loading plugin {plugin_name}: {e}")
def reload_plugins():
global PLUGINS
PLUGINS = {}
# Unload modules from sys.modules
for plugin_name in list(sys.modules.keys()):
if plugin_name.startswith(PLUGINS_DIR + "."):
del sys.modules[plugin_name]
load_plugins()
def rehash_config(config):
del config
config = FunguyConfig()
# Load environment variables from .env file # Load environment variables from .env file
load_dotenv() load_dotenv()
# Bot configuration settings
self.PLUGINS_DIR = "plugins"
self.PLUGINS = {}
self.config = FunguyConfig()
# Load plugins # Load plugins
load_plugins() self.load_plugins()
# Bot configuration settings # Bot configuration settings
MATRIX_URL = os.getenv("MATRIX_URL") self.MATRIX_URL = os.getenv("MATRIX_URL")
MATRIX_USER = os.getenv("MATRIX_USER") self.MATRIX_USER = os.getenv("MATRIX_USER")
MATRIX_PASS = os.getenv("MATRIX_PASS") self.MATRIX_PASS = os.getenv("MATRIX_PASS")
# Get credentials from env # Get credentials from env
creds = botlib.Creds(MATRIX_URL, MATRIX_USER, MATRIX_PASS) creds = botlib.Creds(self.MATRIX_URL, self.MATRIX_USER, self.MATRIX_PASS)
# Bot configuration # Bot initialization
config = FunguyConfig() self.bot = botlib.Bot(creds, self.config)
bot = botlib.Bot(creds, config)
@bot.listener.on_message_event def load_plugins(self):
async def handle_commands(room, message): for plugin_file in os.listdir(self.PLUGINS_DIR):
match = botlib.MessageMatch(room, message, bot, config.prefix) if plugin_file.endswith(".py"):
plugin_name = os.path.splitext(plugin_file)[0]
try:
module = importlib.import_module(f"{self.PLUGINS_DIR}.{plugin_name}")
self.PLUGINS[plugin_name] = module
logging.info(f"Loaded plugin: {plugin_name}")
except Exception as e:
logging.error(f"Error loading plugin {plugin_name}: {e}")
async def reload_plugins(self, room_id):
self.PLUGINS = {}
# Unload modules from sys.modules
for plugin_name in list(sys.modules.keys()):
if plugin_name.startswith(self.PLUGINS_DIR + "."):
del sys.modules[plugin_name]
self.load_plugins()
await self.bot.api.send_text_message(room_id, "Plugins reloaded successfully")
async def rehash_config(self, room_id):
del self.config
self.config = FunguyConfig()
await self.bot.api.send_text_message(room_id, "Config rehashed")
async def handle_commands(self, room, message):
match = botlib.MessageMatch(room, message, self.bot, self.config.prefix)
if match.is_not_from_this_bot() and match.prefix() and match.command("reload"): if match.is_not_from_this_bot() and match.prefix() and match.command("reload"):
if str(message.sender) == config.admin_user: if str(message.sender) == self.config.admin_user:
reload_plugins() await self.reload_plugins(room.room_id)
await bot.api.send_text_message(room.room_id, "Plugins reloaded successfully")
else: else:
await bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.") await self.bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.")
for plugin_name, plugin_module in PLUGINS.items():
await plugin_module.handle_command(room, message, bot, config.prefix, config)
for plugin_name, plugin_module in self.PLUGINS.items():
await plugin_module.handle_command(room, message, self.bot, self.config.prefix, self.config)
if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"): if match.is_not_from_this_bot() and match.prefix() and match.command("rehash"):
if str(message.sender) == config.admin_user: if str(message.sender) == self.config.admin_user:
rehash_config(config) await self.rehash_config(room.room_id)
await bot.api.send_text_message(room.room_id, "Config rehashed")
else: else:
await bot.api.send_text_message(room.room_id, "You are not authorized to reload plugins.") await self.bot.api.send_text_message(room.room_id, "You are not authorized to rehash config.")
if __name__ == "__main__":
bot = FunguyBot()
bot.run() bot.bot.run()